]>
git.r.bdr.sh - rbdr/dasein/blob - app/services/auth.js
32db32189f00e7cdce5e6456b5caf6ea053b93fe
1 import Axios
from 'axios';
5 /* global localStorage */
7 internals
.kInitiateLoginRoute
= '/api/auth/login';
8 internals
.kHandleCallbackRoute
= '/api/auth/callback';
10 export default internals
.AuthService
= class AuthService
{
14 this.authenticated
= false;
16 // Bootstrap from local storage.
17 if (localStorage
.hasOwnProperty('user') && localStorage
.hasOwnProperty('token')
18 && localStorage
.hasOwnProperty('expiresAt')) {
20 if (parseInt(localStorage
.getItem('expiresAt')) > Date
.now()) {
21 this.expiresAt
= localStorage
.getItem('expiresAt');
22 this.user
= JSON
.parse(localStorage
.getItem('user'));
23 this.token
= localStorage
.getItem('token');
24 this.authenticated
= true;
29 // Initiates the login process. Resolves to an object that contains the URL
30 // to redirect to to continue processing.
33 return Axios
.get(internals
.kInitiateLoginRoute
).then((response
) => {
39 // Posts the oAuthToken and verifier to the API to get back a user object
41 getUserObject(oAuthToken
, oAuthVerifier
) {
43 return Axios
.post(internals
.kHandleCallbackRoute
, {
46 }).then((response
) => {
52 // Logs in the user by setting the user and token
53 login(user
, token
, expiresAt
) {
55 localStorage
.setItem('user', JSON
.stringify(user
));
56 localStorage
.setItem('token', token
);
57 localStorage
.setItem('expiresAt', expiresAt
);
60 this.expiresAt
= expiresAt
;
61 this.authenticated
= true;
64 // Logs a user out by removing the user and token
67 localStorage
.removeItem('user');
68 localStorage
.removeItem('token');
69 localStorage
.removeItem('expiresAt');
72 delete this.expiresAt
;
73 this.authenticated
= false;