PeerTube/client/src/app/core/auth/auth-user.model.ts

132 lines
3.4 KiB
TypeScript
Raw Normal View History

// Do not use the barrel (dependency loop)
2017-06-16 11:01:45 +02:00
import { UserRole } from '../../../../../shared/models/user.model'
import { User } from '../../shared/users/user.model';
2016-08-09 21:45:21 +02:00
export class AuthUser extends User {
private static KEYS = {
2016-08-05 18:04:08 +02:00
ID: 'id',
ROLE: 'role',
2017-06-11 11:29:03 +02:00
EMAIL: 'email',
2017-04-04 21:37:03 +02:00
USERNAME: 'username',
DISPLAY_NSFW: 'display_nsfw'
};
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-06-11 11:29:03 +02:00
email: localStorage.getItem(this.KEYS.EMAIL),
2017-06-16 11:01:45 +02:00
role: localStorage.getItem(this.KEYS.ROLE) as UserRole,
2017-04-04 21:37:03 +02:00
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()
);
}
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);
Tokens.flush();
}
2017-04-04 21:37:03 +02:00
constructor(userHash: {
id: number,
username: string,
2017-06-16 11:01:45 +02:00
role: UserRole,
2017-06-11 11:29:03 +02:00
email: string,
2017-04-04 21:37:03 +02:00
displayNSFW: boolean
}, hashTokens: any) {
2016-08-09 21:45:21 +02:00
super(userHash);
this.tokens = new Tokens(hashTokens);
}
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);
localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW));
this.tokens.save();
}
}
2016-08-05 18:04:08 +02:00
// Private class only used by User
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);
}
}