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