-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwtHandler.js
39 lines (37 loc) · 1.57 KB
/
jwtHandler.js
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
import * as SecureStore from 'expo-secure-store';
import axios from 'axios'
export const setAuthAsHeaders = async (userId) => {
console.log("Setting auth as header")
let AUTH_TOKEN = await SecureStore.getItemAsync(userId + '-auth-web-token')
let REFRESH_TOKEN = await SecureStore.getItemAsync(userId + '-auth-refresh-token')
if (AUTH_TOKEN) {
console.log("auth token:")
console.log(AUTH_TOKEN)
axios.defaults.headers.common['auth-web-token'] = AUTH_TOKEN;
} else {
console.log("Error with getting auth token.")
}
if (REFRESH_TOKEN) {
console.log("refresh token:")
console.log(REFRESH_TOKEN)
axios.defaults.headers.common['auth-refresh-token'] = REFRESH_TOKEN;
} else {
console.log("Error with getting refresh token.")
}
}
export const storeJWT = async (tokens, userId) => {
if (tokens.webToken !== "") {
await SecureStore.setItemAsync(userId + '-auth-web-token', tokens.webToken)
console.log("Set: " + userId + '-auth-web-token'+ JSON.stringify(tokens.webToken))
}
if (tokens.refreshToken !== "") {
await SecureStore.setItemAsync(userId + "-auth-refresh-token", tokens.refreshToken)
console.log("Set: " + userId + "-auth-refresh-token" + JSON.stringify(tokens.refreshToken))
}
if (tokens.refreshTokenId !== "") {
await SecureStore.setItemAsync(userId + '-auth-refresh-token-id', tokens.refreshTokenId)
console.log('Set:', userId + '-auth-refresh-token-id', ':', tokens.refreshTokenId)
}
await setAuthAsHeaders(userId)
return
}