Revert use of cookies, go back to using a JWT
This commit is contained in:
parent
871147f2d9
commit
03c83c084a
8 changed files with 80 additions and 44 deletions
|
@ -1,21 +1,37 @@
|
|||
import axios from './../helpers/axios';
|
||||
import isString from 'lodash/isString';
|
||||
import jwtDecode from 'jwt-decode';
|
||||
|
||||
export default class User {
|
||||
/**
|
||||
* Get a new user model by hitting the Panel API using the authentication token
|
||||
* provided. If no user can be retrieved null will be returned.
|
||||
* Get a new user model from the JWT.
|
||||
*
|
||||
* @return {User|null}
|
||||
* @return {User | null}
|
||||
*/
|
||||
static fromCookie() {
|
||||
axios.get('/api/client/account')
|
||||
.then(response => {
|
||||
return new User(response.data.attributes);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
return null;
|
||||
});
|
||||
static fromToken(token) {
|
||||
if (!isString(token)) {
|
||||
token = localStorage.getItem('token');
|
||||
}
|
||||
|
||||
if (!isString(token) || token.length < 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = jwtDecode(token);
|
||||
if (data.user) {
|
||||
return new User(data.user);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the JWT for the authenticated user.
|
||||
*
|
||||
* @returns {string | null}
|
||||
*/
|
||||
static getToken()
|
||||
{
|
||||
return localStorage.getItem('token');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue