-
Notifications
You must be signed in to change notification settings - Fork 22
/
authProvider.ts
59 lines (53 loc) · 1.63 KB
/
authProvider.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
import { AuthProvider } from '../admiral'
import _ from './request'
export const tokenStorageKey = 'df_admin_session_token'
export const storage = {
get: (name: string) => {
return localStorage.getItem(name)
},
set: (name: string, value: string) => {
return localStorage.setItem(name, value)
},
remove: (name: string) => {
return localStorage.removeItem(name)
},
}
const authProvider = (apiUrl: string): AuthProvider => ({
login: ({ username, password }) => {
const url = `${apiUrl}/login`
return _.post(url)({ data: { username, password } }).then(({ data }) => {
storage.set(tokenStorageKey, data.token)
})
},
checkAuth: () => {
const token = storage.get(tokenStorageKey)
if (!!token) {
return Promise.resolve()
} else {
return Promise.reject()
}
},
logout: () => {
const url = `${apiUrl}/logout`
return _.post(url)().then(() => {
storage.remove(tokenStorageKey)
})
},
getIdentity: () => {
const url = `${apiUrl}/getIdentity`
return _.get(url)()
},
oauthLogin: (provider: string) => {
const url = `${apiUrl}/auth/social-login/${provider}`
return _.get(url)().then(({ redirect }) => {
return { redirect }
})
},
oauthCallback: (provider: string, data: string) => {
const url = `${apiUrl}/auth/social-login/${provider}/callback`
return _.post(url)({ data }).then(({ data }) => {
storage.set(tokenStorageKey, data.token)
})
},
})
export default authProvider