-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.js
48 lines (37 loc) · 1.08 KB
/
utils.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
40
41
42
43
44
45
46
47
48
import jwtDecode from "jwt-decode";
const TOKEN_KEY = 'OSS_PUPPY_TOKEN';
export function isUserLoggedIn() {
if (process.browser) {
const token = localStorage.getItem(TOKEN_KEY);
return token && token.length > 0;
} else {
return false;
}
}
export function logoutUser() {
localStorage.removeItem(TOKEN_KEY);
}
export function getLoggedInUser() {
if (process.browser) {
const token = localStorage.getItem(TOKEN_KEY);
return jwtDecode(token);
} else {
return {};
}
}
export function getUserFromToken(token) {
return jwtDecode(token);
}
export function setJwtToken(token) {
localStorage.setItem(TOKEN_KEY, token);
}
export function getJwtToken() {
return localStorage.getItem(TOKEN_KEY);
}
export function getGHRedirectUrl() {
const scope = ['repo', 'read:user'].join(',');
const redirectURI = `https://${process.env.NEXT_PUBLIC_BACKEND_HOST}/auth/github`;
const clientId = process.env.NEXT_PUBLIC_GH_CLIENT_ID;
const ghURL = `https://github.com/login/oauth/authorize?client_id=${clientId}&redirectURI=${redirectURI}&scope=${scope}`;
return ghURL;
}