-
Notifications
You must be signed in to change notification settings - Fork 5
/
localAuth.ts
82 lines (73 loc) · 2.01 KB
/
localAuth.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { SubmitHandler } from '@redwoodjs/forms'
import { users } from 'src/lib/seeds'
// If you're integrating with an auth service provider you should delete this interface.
// Instead you should import the type from their auth client sdk.
export interface AuthClient {
login: () => User
logout: () => void
signup: () => User
getToken: () => string
getUserMetadata: () => User | null
}
export interface LoginEventInterface {
email: string
}
export interface LocalAuthClient {
login: (event: SubmitHandler<LoginEventInterface>) => void
logout: () => Promise<void>
signup: () => null
getToken: () => Promise<string>
getUserMetadata: () => Promise<User | null>
}
interface User {
name: string
email: string
role: string
agencyId: number
organizationId: number
createdAt: string
updatedAt: string
}
export const localAuthClient = {
logout: async () => {
localStorage.removeItem('local_auth_token')
window.location.href = '/login'
},
getToken: async () => {
return localStorage.getItem('local_auth_token')
},
getUserMetadata: async () => {
if (!localStorage.getItem('local_auth_token')) {
return null
}
const user: User | null = users.find(
(u) => u.email === localStorage.getItem('local_auth_token')
)
return user
},
login: async (event) => {
let token
if (event.user === 'manual') {
token = event.email
} else {
token = event.user
}
localStorage.setItem('local_auth_token', token)
window.location.href = '/'
},
signup: () => {
console.log('implemented via GraphQL directly from the user creation page')
return null
},
}
export function createLocalAuthImplementation(client: LocalAuthClient) {
return {
type: 'local',
client,
login: async (e: SubmitHandler<LoginEventInterface>) => client.login(e),
logout: async () => client.logout(),
signup: async () => client.signup(),
getToken: async () => client.getToken(),
getUserMetadata: async () => client.getUserMetadata(),
}
}