-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAuthService.js
More file actions
81 lines (64 loc) · 1.69 KB
/
AuthService.js
File metadata and controls
81 lines (64 loc) · 1.69 KB
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
import { isTokenExpired, isEditor, isAdmin, sub } from './jwtHelper';
import reqwest from 'reqwest';
import Auth0 from 'auth0-js';
export default class AuthService {
constructor (clientID, domain) {
this.auth0 = new Auth0({ clientID, domain, responseType: 'token' });
this.login = this.login.bind(this);
this.signup = this.signup.bind(this);
}
request (url, method, options) {
let headers = {};
if (this.loggedIn()) {
headers['Authorization'] = this.getToken();
}
let reqParams = Object.assign({}, {
url,
method,
headers,
type: 'json',
contentType: 'application/json'
}, options || {});
return reqwest(reqParams);
}
login (params, onError) {
this.auth0.login(params, onError);
}
signup (params, onError) {
this.auth0.signup(params, onError);
}
parseHash (hash) {
const authResult = this.auth0.parseHash(hash);
if (authResult && authResult.idToken) {
this.setToken(authResult.idToken);
}
}
loggedIn () {
const token = this.getToken();
return !!token && !isTokenExpired(token) && this.allowedAccess(token);
}
loggedInNotEditor () {
const token = this.getToken();
return !!token && !isTokenExpired(token) && !this.allowedAccess(token);
}
allowedAccess (token) {
return isEditor(token) || isAdmin(token);
}
isAdmin () {
const token = this.getToken();
return isAdmin(token);
}
getSub () {
const token = this.getToken();
return sub(token);
}
setToken (idToken) {
localStorage.setItem('id_token', idToken);
}
getToken () {
return localStorage.getItem('id_token');
}
logout () {
localStorage.removeItem('id_token');
}
}