2017-03-04 11:45:47 +01:00
|
|
|
// Do not use the barrel (dependency loop)
|
|
|
|
import { User } from '../../shared/users/user.model';
|
2016-08-09 21:45:21 +02:00
|
|
|
|
|
|
|
export class AuthUser extends User {
|
2016-07-20 16:24:18 +02:00
|
|
|
private static KEYS = {
|
2016-08-05 18:04:08 +02:00
|
|
|
ID: 'id',
|
|
|
|
ROLE: 'role',
|
2017-04-04 21:37:03 +02:00
|
|
|
USERNAME: 'username',
|
|
|
|
DISPLAY_NSFW: 'display_nsfw'
|
2016-07-20 16:24:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
tokens: Tokens;
|
|
|
|
|
|
|
|
static load() {
|
|
|
|
const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
|
|
|
|
if (usernameLocalStorage) {
|
2016-08-09 21:45:21 +02:00
|
|
|
return new AuthUser(
|
|
|
|
{
|
2016-12-11 21:50:51 +01:00
|
|
|
id: parseInt(localStorage.getItem(this.KEYS.ID)),
|
2016-08-09 21:45:21 +02:00
|
|
|
username: localStorage.getItem(this.KEYS.USERNAME),
|
2017-04-04 21:37:03 +02:00
|
|
|
role: localStorage.getItem(this.KEYS.ROLE),
|
|
|
|
displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true'
|
2016-08-09 21:45:21 +02:00
|
|
|
},
|
2016-08-05 18:04:08 +02:00
|
|
|
Tokens.load()
|
|
|
|
);
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static flush() {
|
|
|
|
localStorage.removeItem(this.KEYS.USERNAME);
|
2016-08-05 18:04:08 +02:00
|
|
|
localStorage.removeItem(this.KEYS.ID);
|
|
|
|
localStorage.removeItem(this.KEYS.ROLE);
|
2017-04-04 21:37:03 +02:00
|
|
|
localStorage.removeItem(this.KEYS.DISPLAY_NSFW);
|
2016-07-20 16:24:18 +02:00
|
|
|
Tokens.flush();
|
|
|
|
}
|
|
|
|
|
2017-04-04 21:37:03 +02:00
|
|
|
constructor(userHash: {
|
|
|
|
id: number,
|
|
|
|
username: string,
|
|
|
|
role: string,
|
|
|
|
displayNSFW: boolean
|
|
|
|
}, hashTokens: any) {
|
2016-08-09 21:45:21 +02:00
|
|
|
super(userHash);
|
|
|
|
this.tokens = new Tokens(hashTokens);
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getAccessToken() {
|
|
|
|
return this.tokens.access_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRefreshToken() {
|
|
|
|
return this.tokens.refresh_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTokenType() {
|
|
|
|
return this.tokens.token_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshTokens(access_token: string, refresh_token: string) {
|
|
|
|
this.tokens.access_token = access_token;
|
|
|
|
this.tokens.refresh_token = refresh_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
2016-12-11 21:50:51 +01:00
|
|
|
localStorage.setItem(AuthUser.KEYS.ID, this.id.toString());
|
2016-08-09 21:45:21 +02:00
|
|
|
localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
|
|
|
|
localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
|
2017-04-06 21:21:03 +02:00
|
|
|
localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW));
|
2016-07-20 16:24:18 +02:00
|
|
|
this.tokens.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 18:04:08 +02:00
|
|
|
// Private class only used by User
|
2016-07-20 16:24:18 +02:00
|
|
|
class Tokens {
|
|
|
|
private static KEYS = {
|
|
|
|
ACCESS_TOKEN: 'access_token',
|
|
|
|
REFRESH_TOKEN: 'refresh_token',
|
|
|
|
TOKEN_TYPE: 'token_type',
|
|
|
|
};
|
|
|
|
|
|
|
|
access_token: string;
|
|
|
|
refresh_token: string;
|
|
|
|
token_type: string;
|
|
|
|
|
|
|
|
static load() {
|
|
|
|
const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN);
|
|
|
|
const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN);
|
|
|
|
const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE);
|
|
|
|
|
|
|
|
if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
|
|
|
|
return new Tokens({
|
|
|
|
access_token: accessTokenLocalStorage,
|
|
|
|
refresh_token: refreshTokenLocalStorage,
|
|
|
|
token_type: tokenTypeLocalStorage
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static flush() {
|
|
|
|
localStorage.removeItem(this.KEYS.ACCESS_TOKEN);
|
|
|
|
localStorage.removeItem(this.KEYS.REFRESH_TOKEN);
|
|
|
|
localStorage.removeItem(this.KEYS.TOKEN_TYPE);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(hash?: any) {
|
|
|
|
if (hash) {
|
|
|
|
this.access_token = hash.access_token;
|
|
|
|
this.refresh_token = hash.refresh_token;
|
|
|
|
|
|
|
|
if (hash.token_type === 'bearer') {
|
|
|
|
this.token_type = 'Bearer';
|
|
|
|
} else {
|
|
|
|
this.token_type = hash.token_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
localStorage.setItem('access_token', this.access_token);
|
|
|
|
localStorage.setItem('refresh_token', this.refresh_token);
|
|
|
|
localStorage.setItem('token_type', this.token_type);
|
|
|
|
}
|
|
|
|
}
|