Skip to content

Commit

Permalink
Merge pull request #286 from quentinglorieux/main
Browse files Browse the repository at this point in the history
Implement loginWithLdap
  • Loading branch information
Intevel authored Feb 7, 2025
2 parents 809d25a + 949eda8 commit f7c6cfa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/runtime/composables/useDirectusAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRoute, useRuntimeConfig, navigateTo } from '#app'
import type { Ref } from 'vue'
import type {
DirectusAuthCredentials,
DirectusAuthLdapCredentials,
DirectusAuthResponse,
DirectusAcceptInvite,
DirectusInviteCreation,
Expand Down Expand Up @@ -97,6 +98,35 @@ export const useDirectusAuth = () => {
}
}

const loginWithLdap = async (
data: DirectusAuthLdapCredentials,
useStaticToken?: boolean
): Promise<DirectusAuthResponse> => {
removeTokens()

const response = await $fetch<{data: DirectusAuthResponse}>('/auth/login/ldap', {
baseURL: baseUrl,
body: data,
method: 'POST'
})

if (!response.data.access_token) { throw new Error('LDAP Login failed, please check your credentials.') }

// Calculate new expires date, bug fix https://github.com/Intevel/nuxt-directus/issues/157
const newExpires = (response.data.expires ?? 0) + new Date().getTime()

setAuthCookies(response.data.access_token, response.data.refresh_token, newExpires)

const user = await fetchUser()

return {
user: user.value,
access_token: response.data.access_token,
expires: newExpires,
refresh_token: response.data.refresh_token
}
}

const loginWithProvider = async (
provider: string,
redirectOnLogin?: string
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export interface DirectusAuthCredentials {
otp?: string;
}

export interface DirectusAuthLdapCredentials {
identifier: string;
password: string;
otp?: string;
}

export interface DirectusAuthResponse {
user: DirectusUser;
access_token: string;
Expand Down

0 comments on commit f7c6cfa

Please sign in to comment.