misc_pterodactyl-panel/resources/assets/scripts/models/user.js

72 lines
1.5 KiB
JavaScript
Raw Normal View History

import isString from 'lodash/isString';
import jwtDecode from 'jwt-decode';
2018-06-06 06:42:34 +00:00
export default class User {
/**
* Get a new user model from the JWT.
*
* @return {User | null}
*/
static fromToken(token) {
if (!isString(token)) {
token = this.getToken();
}
if (!isString(token) || token.length < 1) {
return null;
}
try {
const data = jwtDecode(token);
if (data.user) {
return new User(data.user);
}
} catch (ex) {}
return null;
}
/**
* Return the JWT for the authenticated user.
*
* @returns {string | null}
*/
static getToken() {
return localStorage.getItem('token');
}
/**
* Create a new user model.
*
* @param {Boolean} admin
* @param {String} username
* @param {String} email
* @param {String} first_name
* @param {String} last_name
* @param {String} language
*/
constructor({
admin,
username,
email,
first_name,
last_name,
language,
}) {
this.admin = admin;
this.username = username;
this.email = email;
this.name = `${first_name} ${last_name}`;
this.first_name = first_name;
this.last_name = last_name;
this.language = language;
}
/**
* Returns the JWT belonging to the current user.
*/
getJWT() {
return jwtDecode(User.getToken());
}
}