-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseUser.ts
More file actions
43 lines (38 loc) · 1.45 KB
/
useUser.ts
File metadata and controls
43 lines (38 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useState } from '#imports'
import type { Ref } from 'vue'
export interface TlUser {
loggedIn: boolean
id: string
name: string
email: string
roles: string[]
/**
* Opaque key-value map plumbed through from the backend's user context
* (e.g. tlserver's WithExternalData via the gatekeeper middleware).
* Schema is deployment-specific; consumers cast or parse as needed.
*/
externalData: Record<string, unknown>
hasRole: (v: string) => boolean
}
// auth0-nuxt populates useState('auth0_user') with OIDC claims
const useAuth0User = () => useState<Record<string, any> | undefined>('auth0_user')
// State keys for roles/id fetched from the GraphQL `me` endpoint
const useRoles = (): Ref<string[]> => useState<string[]>('tlv2_user_roles', () => [])
const useGraphqlId = (): Ref<string> => useState<string>('tlv2_user_id', () => '')
export const useUser = (): TlUser => {
const auth0User = useAuth0User()
const roles = useRoles()
const graphqlId = useGraphqlId()
const loggedIn = !!auth0User.value
return {
loggedIn,
id: graphqlId.value || auth0User.value?.tlv2_id || auth0User.value?.sub || '',
name: auth0User.value?.name || auth0User.value?.tlv2_name || '',
email: auth0User.value?.email || auth0User.value?.tlv2_email || '',
roles: roles.value,
externalData: (auth0User.value?.tlv2_external_data || {}) as Record<string, unknown>,
hasRole (v: string): boolean {
return roles.value.includes(v)
}
}
}