1 // Do not use the barrel (dependency loop)
2 import { User } from '../../shared/users/user.model';
4 export class AuthUser extends User {
5 private static KEYS = {
10 DISPLAY_NSFW: 'display_nsfw'
16 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
17 if (usernameLocalStorage) {
20 id: parseInt(localStorage.getItem(this.KEYS.ID)),
21 username: localStorage.getItem(this.KEYS.USERNAME),
22 email: localStorage.getItem(this.KEYS.EMAIL),
23 role: localStorage.getItem(this.KEYS.ROLE),
24 displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true'
34 localStorage.removeItem(this.KEYS.USERNAME);
35 localStorage.removeItem(this.KEYS.ID);
36 localStorage.removeItem(this.KEYS.ROLE);
37 localStorage.removeItem(this.KEYS.DISPLAY_NSFW);
41 constructor(userHash: {
49 this.tokens = new Tokens(hashTokens);
53 return this.tokens.access_token;
57 return this.tokens.refresh_token;
61 return this.tokens.token_type;
64 refreshTokens(access_token: string, refresh_token: string) {
65 this.tokens.access_token = access_token;
66 this.tokens.refresh_token = refresh_token;
70 localStorage.setItem(AuthUser.KEYS.ID, this.id.toString());
71 localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
72 localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
73 localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW));
78 // Private class only used by User
80 private static KEYS = {
81 ACCESS_TOKEN: 'access_token',
82 REFRESH_TOKEN: 'refresh_token',
83 TOKEN_TYPE: 'token_type',
87 refresh_token: string;
91 const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN);
92 const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN);
93 const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE);
95 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
97 access_token: accessTokenLocalStorage,
98 refresh_token: refreshTokenLocalStorage,
99 token_type: tokenTypeLocalStorage
107 localStorage.removeItem(this.KEYS.ACCESS_TOKEN);
108 localStorage.removeItem(this.KEYS.REFRESH_TOKEN);
109 localStorage.removeItem(this.KEYS.TOKEN_TYPE);
112 constructor(hash?: any) {
114 this.access_token = hash.access_token;
115 this.refresh_token = hash.refresh_token;
117 if (hash.token_type === 'bearer') {
118 this.token_type = 'Bearer';
120 this.token_type = hash.token_type;
126 localStorage.setItem('access_token', this.access_token);
127 localStorage.setItem('refresh_token', this.refresh_token);
128 localStorage.setItem('token_type', this.token_type);