-
Notifications
You must be signed in to change notification settings - Fork 5
/
passageAuth.ts
62 lines (57 loc) · 1.52 KB
/
passageAuth.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
PassageUser,
PassageUserInfo,
} from '@passageidentity/passage-elements/passage-user'
export interface AuthResult {
redirect_url: string
auth_token: string
refresh_token?: string
refresh_token_expiration?: number
}
export interface PassageAuthClient {
login: (event: AuthResult) => void
logout: () => Promise<void>
signup: () => null
getToken: () => Promise<string>
getUserMetadata: () => Promise<PassageUserInfo | null>
}
async function getPassageUser(): Promise<PassageUser | null> {
if (localStorage.getItem('psg_auth_token')) {
return new PassageUser()
} else {
return null
}
}
export const passageAuthClient = {
logout: async () => {
const user = await getPassageUser()
user?.signOut()
window.location.href = '/login'
},
getToken: async () => {
const user = await getPassageUser()
return user?.getAuthToken()
},
getUserMetadata: async () => {
const user = await getPassageUser()
return user?.userInfo()
},
login: (event) => {
localStorage.setItem('psg_auth_token', event.auth_token)
window.location.href = event.redirect_url
},
signup: () => {
throw Error('Not implemented')
},
}
export function createPassageAuthImplementation(client: PassageAuthClient) {
return {
type: 'passage',
client,
login: async (e: AuthResult) => client.login(e),
logout: async () => client.logout(),
signup: async () => client.signup(),
getToken: async () => client.getToken(),
getUserMetadata: async () => client.getUserMetadata(),
}
}