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 = {
9 DISPLAY_NSFW: 'display_nsfw'
15 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
16 if (usernameLocalStorage) {
19 id: parseInt(localStorage.getItem(this.KEYS.ID)),
20 username: localStorage.getItem(this.KEYS.USERNAME),
21 role: localStorage.getItem(this.KEYS.ROLE),
22 displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true'
32 localStorage.removeItem(this.KEYS.USERNAME);
33 localStorage.removeItem(this.KEYS.ID);
34 localStorage.removeItem(this.KEYS.ROLE);
35 localStorage.removeItem(this.KEYS.DISPLAY_NSFW);
39 constructor(userHash: {
46 this.tokens = new Tokens(hashTokens);
50 return this.tokens.access_token;
54 return this.tokens.refresh_token;
58 return this.tokens.token_type;
61 refreshTokens(access_token: string, refresh_token: string) {
62 this.tokens.access_token = access_token;
63 this.tokens.refresh_token = refresh_token;
67 localStorage.setItem(AuthUser.KEYS.ID, this.id.toString());
68 localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
69 localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
70 localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW);
75 // Private class only used by User
77 private static KEYS = {
78 ACCESS_TOKEN: 'access_token',
79 REFRESH_TOKEN: 'refresh_token',
80 TOKEN_TYPE: 'token_type',
84 refresh_token: string;
88 const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN);
89 const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN);
90 const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE);
92 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
94 access_token: accessTokenLocalStorage,
95 refresh_token: refreshTokenLocalStorage,
96 token_type: tokenTypeLocalStorage
104 localStorage.removeItem(this.KEYS.ACCESS_TOKEN);
105 localStorage.removeItem(this.KEYS.REFRESH_TOKEN);
106 localStorage.removeItem(this.KEYS.TOKEN_TYPE);
109 constructor(hash?: any) {
111 this.access_token = hash.access_token;
112 this.refresh_token = hash.refresh_token;
114 if (hash.token_type === 'bearer') {
115 this.token_type = 'Bearer';
117 this.token_type = hash.token_type;
123 localStorage.setItem('access_token', this.access_token);
124 localStorage.setItem('refresh_token', this.refresh_token);
125 localStorage.setItem('token_type', this.token_type);