Skip to content

feat: auth debug #261

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

Closed
wants to merge 8 commits into from
Closed
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
22 changes: 16 additions & 6 deletions packages/frontendmu-nuxt/auth-utils/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,31 @@ export default function useAuth(client: DirectusClient<any> & 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
}
Expand All @@ -146,6 +150,8 @@ export default function useAuth(client: DirectusClient<any> & AuthenticationClie
isLoading.value = false
}

console.log('Setting auth to ', value)

isAuth.value = value
}

Expand Down Expand Up @@ -186,6 +192,10 @@ export default function useAuth(client: DirectusClient<any> & 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))

Expand Down
44 changes: 36 additions & 8 deletions packages/frontendmu-nuxt/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -33,26 +33,54 @@ 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 || '',
phone: user.phone || '',
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,
}
Expand Down
4 changes: 2 additions & 2 deletions packages/frontendmu-nuxt/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down