1 import { User } from '../users';
3 export class AuthUser extends User {
4 private static KEYS = {
13 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
14 if (usernameLocalStorage) {
17 id: parseInt(localStorage.getItem(this.KEYS.ID)),
18 username: localStorage.getItem(this.KEYS.USERNAME),
19 role: localStorage.getItem(this.KEYS.ROLE)
29 localStorage.removeItem(this.KEYS.USERNAME);
30 localStorage.removeItem(this.KEYS.ID);
31 localStorage.removeItem(this.KEYS.ROLE);
35 constructor(userHash: { id: number, username: string, role: string }, hashTokens: any) {
37 this.tokens = new Tokens(hashTokens);
41 return this.tokens.access_token;
45 return this.tokens.refresh_token;
49 return this.tokens.token_type;
52 refreshTokens(access_token: string, refresh_token: string) {
53 this.tokens.access_token = access_token;
54 this.tokens.refresh_token = refresh_token;
58 localStorage.setItem(AuthUser.KEYS.ID, this.id.toString());
59 localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
60 localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
65 // Private class only used by User
67 private static KEYS = {
68 ACCESS_TOKEN: 'access_token',
69 REFRESH_TOKEN: 'refresh_token',
70 TOKEN_TYPE: 'token_type',
74 refresh_token: string;
78 const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN);
79 const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN);
80 const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE);
82 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
84 access_token: accessTokenLocalStorage,
85 refresh_token: refreshTokenLocalStorage,
86 token_type: tokenTypeLocalStorage
94 localStorage.removeItem(this.KEYS.ACCESS_TOKEN);
95 localStorage.removeItem(this.KEYS.REFRESH_TOKEN);
96 localStorage.removeItem(this.KEYS.TOKEN_TYPE);
99 constructor(hash?: any) {
101 this.access_token = hash.access_token;
102 this.refresh_token = hash.refresh_token;
104 if (hash.token_type === 'bearer') {
105 this.token_type = 'Bearer';
107 this.token_type = hash.token_type;
113 localStorage.setItem('access_token', this.access_token);
114 localStorage.setItem('refresh_token', this.refresh_token);
115 localStorage.setItem('token_type', this.token_type);