Skip to content

Commit f7c6cfa

Browse files
authored
Merge pull request #286 from quentinglorieux/main
Implement loginWithLdap
2 parents 809d25a + 949eda8 commit f7c6cfa

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/runtime/composables/useDirectusAuth.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useRoute, useRuntimeConfig, navigateTo } from '#app'
33
import type { Ref } from 'vue'
44
import type {
55
DirectusAuthCredentials,
6+
DirectusAuthLdapCredentials,
67
DirectusAuthResponse,
78
DirectusAcceptInvite,
89
DirectusInviteCreation,
@@ -97,6 +98,35 @@ export const useDirectusAuth = () => {
9798
}
9899
}
99100

101+
const loginWithLdap = async (
102+
data: DirectusAuthLdapCredentials,
103+
useStaticToken?: boolean
104+
): Promise<DirectusAuthResponse> => {
105+
removeTokens()
106+
107+
const response = await $fetch<{data: DirectusAuthResponse}>('/auth/login/ldap', {
108+
baseURL: baseUrl,
109+
body: data,
110+
method: 'POST'
111+
})
112+
113+
if (!response.data.access_token) { throw new Error('LDAP Login failed, please check your credentials.') }
114+
115+
// Calculate new expires date, bug fix https://github.com/Intevel/nuxt-directus/issues/157
116+
const newExpires = (response.data.expires ?? 0) + new Date().getTime()
117+
118+
setAuthCookies(response.data.access_token, response.data.refresh_token, newExpires)
119+
120+
const user = await fetchUser()
121+
122+
return {
123+
user: user.value,
124+
access_token: response.data.access_token,
125+
expires: newExpires,
126+
refresh_token: response.data.refresh_token
127+
}
128+
}
129+
100130
const loginWithProvider = async (
101131
provider: string,
102132
redirectOnLogin?: string

src/runtime/types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export interface DirectusAuthCredentials {
2828
otp?: string;
2929
}
3030

31+
export interface DirectusAuthLdapCredentials {
32+
identifier: string;
33+
password: string;
34+
otp?: string;
35+
}
36+
3137
export interface DirectusAuthResponse {
3238
user: DirectusUser;
3339
access_token: string;

0 commit comments

Comments
 (0)