diff --git a/packages/frontendmu-nuxt/auth-utils/useAuth.ts b/packages/frontendmu-nuxt/auth-utils/useAuth.ts index ff1070d..5555b81 100644 --- a/packages/frontendmu-nuxt/auth-utils/useAuth.ts +++ b/packages/frontendmu-nuxt/auth-utils/useAuth.ts @@ -107,27 +107,31 @@ export default function useAuth(client: DirectusClient & AuthenticationClie async function loginWithSSO() { try { + const cookieValue = getCookieValue('directus_session_token') + console.log({ cookieValue }) const res = await fetch( 'https://directus.frontend.mu/auth/refresh', { method: 'POST', credentials: 'include', // this is required in order to send the refresh token cookie body: JSON.stringify({ - refresh_token: getCookieValue('directus_session_token'), - mode: 'cookie', + // refresh_token: cookieValue, + mode: 'session', }), }, ) const response: { data: AuthenticationData } = await res.json() + console.log({ response }) setCookie(response.data) + console.log('trying to set cookie to ', response.data) await getCurrentUser() setAuth(true) - if (!rawUser.value?.profile_picture) { - const picture = await cloudFunctionUpdateProfilePicture(rawUser.value?.id || '') - console.log(picture) - } + // if (!rawUser.value?.profile_picture) { + // const picture = await cloudFunctionUpdateProfilePicture(rawUser.value?.id || '') + // console.log(picture) + // } return response.data } @@ -146,6 +150,8 @@ export default function useAuth(client: DirectusClient & AuthenticationClie isLoading.value = false } + console.log('Setting auth to ', value) + isAuth.value = value } @@ -186,6 +192,10 @@ export default function useAuth(client: DirectusClient & AuthenticationClie if (!token) { throw new Error('User is not logged in') } + else { + console.log('User is logged in') + console.log({ token }) + } client = await client.with(staticToken(token)) diff --git a/packages/frontendmu-nuxt/utils/helpers.ts b/packages/frontendmu-nuxt/utils/helpers.ts index 1451e75..07c278c 100644 --- a/packages/frontendmu-nuxt/utils/helpers.ts +++ b/packages/frontendmu-nuxt/utils/helpers.ts @@ -1,4 +1,4 @@ -import type { User } from './types' +import type { GoogleInfo, User } from './types' export function random(list: string[] | number[]) { return list[Math.floor(Math.random() * list.length)] @@ -33,15 +33,43 @@ export function formatDate(date: string) { }) } -export function mapToValidUser(user: any): User { +interface UserFromDircetusRole { + name: 'sso_google' | 'Admin' + +} +interface UserFromDirectus { + full_name?: string + first_name?: string + last_name?: string + id?: string + email?: string + role?: UserFromDircetusRole + provider?: 'google' | 'default' | 'github' + current_occupation?: string + meal?: string + transport?: string + phone?: string + occupation?: string + created_at?: string + name?: string + github_username?: string + avatar_url?: string + profile_picture?: string + google?: GoogleInfo + external_identifier?: string +} + +export function mapToValidUser(user: UserFromDirectus): User { + console.log('trying to map this') + console.log({ user }) const full_name = user?.full_name ? user.full_name - : `${user.first_name} ${user.last_name ?? ''}`.trim() + : `${user?.first_name} ${user?.last_name ?? ''}`.trim() return { - id: user.id, + id: user?.id || '', full_name, - email: user.email, + email: user?.email || '', current_occupation: user?.current_occupation || '', meal: user?.meal || '', transport: user.transport || '', @@ -49,10 +77,10 @@ export function mapToValidUser(user: any): User { occupation: user.occupation || '', created_at: user?.created_at || '', github_username: user?.github_username || '', - avatar_url: user.avatar_url, + avatar_url: user?.avatar_url || '', profile_picture: user.profile_picture || '', - google: user.google, - role: user.role.name, + google: user?.google || null, + role: user?.role?.name, provider: user.provider, external_identifier: user.external_identifier, } diff --git a/packages/frontendmu-nuxt/utils/types.ts b/packages/frontendmu-nuxt/utils/types.ts index b41339e..fd04386 100644 --- a/packages/frontendmu-nuxt/utils/types.ts +++ b/packages/frontendmu-nuxt/utils/types.ts @@ -32,10 +32,10 @@ export interface User { phone: string | null created_at: string avatar_url: string | null - google?: GoogleInfo + google?: GoogleInfo | null github_username?: string profile_picture?: string | null - role: 'sso_google' | 'Admin' + role: 'sso_google' | 'Admin' | undefined provider?: string external_identifier?: string }