1 import { User } from '../users';
3 export class AuthUser extends User {
4 private static KEYS = {
16 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
17 if (usernameLocalStorage) {
20 id: localStorage.getItem(this.KEYS.ID),
21 username: localStorage.getItem(this.KEYS.USERNAME),
22 role: localStorage.getItem(this.KEYS.ROLE)
32 localStorage.removeItem(this.KEYS.USERNAME);
33 localStorage.removeItem(this.KEYS.ID);
34 localStorage.removeItem(this.KEYS.ROLE);
38 constructor(userHash: { id: string, username: string, role: string }, hashTokens: any) {
40 this.tokens = new Tokens(hashTokens);
44 return this.tokens.access_token;
48 return this.tokens.refresh_token;
52 return this.tokens.token_type;
55 refreshTokens(access_token: string, refresh_token: string) {
56 this.tokens.access_token = access_token;
57 this.tokens.refresh_token = refresh_token;
61 localStorage.setItem(AuthUser.KEYS.ID, this.id);
62 localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
63 localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
68 // Private class only used by User
70 private static KEYS = {
71 ACCESS_TOKEN: 'access_token',
72 REFRESH_TOKEN: 'refresh_token',
73 TOKEN_TYPE: 'token_type',
77 refresh_token: string;
81 const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN);
82 const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN);
83 const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE);
85 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
87 access_token: accessTokenLocalStorage,
88 refresh_token: refreshTokenLocalStorage,
89 token_type: tokenTypeLocalStorage
97 localStorage.removeItem(this.KEYS.ACCESS_TOKEN);
98 localStorage.removeItem(this.KEYS.REFRESH_TOKEN);
99 localStorage.removeItem(this.KEYS.TOKEN_TYPE);
102 constructor(hash?: any) {
104 this.access_token = hash.access_token;
105 this.refresh_token = hash.refresh_token;
107 if (hash.token_type === 'bearer') {
108 this.token_type = 'Bearer';
110 this.token_type = hash.token_type;
116 localStorage.setItem('access_token', this.access_token);
117 localStorage.setItem('refresh_token', this.refresh_token);
118 localStorage.setItem('token_type', this.token_type);