This repository has been archived on 2021-03-10. You can view files and clone it, but cannot push or open issues or pull requests.
perktree/frontend/src/controllers/auth.controller.js

97 lines
2.1 KiB
JavaScript

/*
* auth.controller.js
* Copyright (C) 2019 pavle <pavle.portic@tilda.center>
*
* Distributed under terms of the BSD-3-Clause license.
*/
import * as _ from 'lodash';
import AuthApi from '../apis/auth.api';
import router from '../router';
import store from '../store';
export default class AuthController {
static setLocalStorageToken(data) {
if (data.refresh) {
localStorage.setItem('refresh', data.refresh);
}
if (data.access) {
localStorage.setItem('access', data.access);
}
}
static getLocalStorageToken() {
const tokens = {
access: localStorage.getItem('access'),
refresh: localStorage.getItem('refresh'),
};
return tokens;
}
static getLocalStorageRefresh() {
return localStorage.getItem('access');
}
static clearLocalStorageToken() {
localStorage.removeItem('refresh');
localStorage.removeItem('access');
}
static setupToken() {
const access = AuthController.getLocalStorageToken().access;
if (access) {
AuthApi.setAuthHeader(access);
store.commit('login');
}
}
static login(data) {
return AuthApi.login(data).then((response) => {
AuthController.setLocalStorageToken(response.data);
AuthController.setupToken(response.data);
});
}
static logout() {
AuthController.clearLocalStorageToken();
store.commit('logout');
AuthApi.setAuthHeader(null);
}
static changePassword(data) {
return AuthApi.changePassword(data);
}
static verifyToken() {
const token = AuthController.getLocalStorageToken().access;
if (token === null) {
router.push('login');
}
return AuthApi.verifyToken({ token });
}
static refreshToken() {
if (!AuthController.getLocalStorageRefresh()) {
return Promise.reject(new Error('No token'));
}
const refresh = {
refresh: AuthController.getLocalStorageToken().refresh,
};
_.delay(AuthController.refreshToken, 240000);
return AuthApi.refreshToken(refresh).then((response) => {
AuthController.setLocalStorageToken(response.data);
AuthController.setupToken();
});
}
static getAuthStatus() {
const token = AuthController.getLocalStorageToken().access;
return Boolean(token);
}
}