]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/auth/auth-user.model.ts
Fix client compilation
[github/Chocobozzz/PeerTube.git] / client / src / app / core / auth / auth-user.model.ts
1 // Do not use the barrel (dependency loop)
2 import { UserRole } from '../../../../../shared/models/user.model'
3 import { User } from '../../shared/users/user.model';
4
5 export class AuthUser extends User {
6 private static KEYS = {
7 ID: 'id',
8 ROLE: 'role',
9 EMAIL: 'email',
10 USERNAME: 'username',
11 DISPLAY_NSFW: 'display_nsfw'
12 };
13
14 tokens: Tokens;
15
16 static load() {
17 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
18 if (usernameLocalStorage) {
19 return new AuthUser(
20 {
21 id: parseInt(localStorage.getItem(this.KEYS.ID)),
22 username: localStorage.getItem(this.KEYS.USERNAME),
23 email: localStorage.getItem(this.KEYS.EMAIL),
24 role: localStorage.getItem(this.KEYS.ROLE) as UserRole,
25 displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true'
26 },
27 Tokens.load()
28 );
29 }
30
31 return null;
32 }
33
34 static flush() {
35 localStorage.removeItem(this.KEYS.USERNAME);
36 localStorage.removeItem(this.KEYS.ID);
37 localStorage.removeItem(this.KEYS.ROLE);
38 localStorage.removeItem(this.KEYS.DISPLAY_NSFW);
39 Tokens.flush();
40 }
41
42 constructor(userHash: {
43 id: number,
44 username: string,
45 role: UserRole,
46 email: string,
47 displayNSFW: boolean
48 }, hashTokens: any) {
49 super(userHash);
50 this.tokens = new Tokens(hashTokens);
51 }
52
53 getAccessToken() {
54 return this.tokens.access_token;
55 }
56
57 getRefreshToken() {
58 return this.tokens.refresh_token;
59 }
60
61 getTokenType() {
62 return this.tokens.token_type;
63 }
64
65 refreshTokens(access_token: string, refresh_token: string) {
66 this.tokens.access_token = access_token;
67 this.tokens.refresh_token = refresh_token;
68 }
69
70 save() {
71 localStorage.setItem(AuthUser.KEYS.ID, this.id.toString());
72 localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
73 localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
74 localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW));
75 this.tokens.save();
76 }
77 }
78
79 // Private class only used by User
80 class Tokens {
81 private static KEYS = {
82 ACCESS_TOKEN: 'access_token',
83 REFRESH_TOKEN: 'refresh_token',
84 TOKEN_TYPE: 'token_type',
85 };
86
87 access_token: string;
88 refresh_token: string;
89 token_type: string;
90
91 static load() {
92 const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN);
93 const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN);
94 const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE);
95
96 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
97 return new Tokens({
98 access_token: accessTokenLocalStorage,
99 refresh_token: refreshTokenLocalStorage,
100 token_type: tokenTypeLocalStorage
101 });
102 }
103
104 return null;
105 }
106
107 static flush() {
108 localStorage.removeItem(this.KEYS.ACCESS_TOKEN);
109 localStorage.removeItem(this.KEYS.REFRESH_TOKEN);
110 localStorage.removeItem(this.KEYS.TOKEN_TYPE);
111 }
112
113 constructor(hash?: any) {
114 if (hash) {
115 this.access_token = hash.access_token;
116 this.refresh_token = hash.refresh_token;
117
118 if (hash.token_type === 'bearer') {
119 this.token_type = 'Bearer';
120 } else {
121 this.token_type = hash.token_type;
122 }
123 }
124 }
125
126 save() {
127 localStorage.setItem('access_token', this.access_token);
128 localStorage.setItem('refresh_token', this.refresh_token);
129 localStorage.setItem('token_type', this.token_type);
130 }
131 }