Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement loginWithLdap #286

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Intevel marked this conversation as resolved.
Show resolved Hide resolved
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