]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/auth/auth-user.model.ts
Client: add ability for user to change nsfw settings
[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 USERNAME: 'username',
9 DISPLAY_NSFW: 'display_nsfw'
10 };
11
12 tokens: Tokens;
13
14 static load() {
15 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
16 if (usernameLocalStorage) {
17 return new AuthUser(
18 {
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'
23 },
24 Tokens.load()
25 );
26 }
27
28 return null;
29 }
30
31 static flush() {
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);
36 Tokens.flush();
37 }
38
39 constructor(userHash: {
40 id: number,
41 username: string,
42 role: string,
43 displayNSFW: boolean
44 }, hashTokens: any) {
45 super(userHash);
46 this.tokens = new Tokens(hashTokens);
47 }
48
49 getAccessToken() {
50 return this.tokens.access_token;
51 }
52
53 getRefreshToken() {
54 return this.tokens.refresh_token;
55 }
56
57 getTokenType() {
58 return this.tokens.token_type;
59 }
60
61 refreshTokens(access_token: string, refresh_token: string) {
62 this.tokens.access_token = access_token;
63 this.tokens.refresh_token = refresh_token;
64 }
65
66 save() {
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));
71 this.tokens.save();
72 }
73 }
74
75 // Private class only used by User
76 class Tokens {
77 private static KEYS = {
78 ACCESS_TOKEN: 'access_token',
79 REFRESH_TOKEN: 'refresh_token',
80 TOKEN_TYPE: 'token_type',
81 };
82
83 access_token: string;
84 refresh_token: string;
85 token_type: string;
86
87 static load() {
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);
91
92 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
93 return new Tokens({
94 access_token: accessTokenLocalStorage,
95 refresh_token: refreshTokenLocalStorage,
96 token_type: tokenTypeLocalStorage
97 });
98 }
99
100 return null;
101 }
102
103 static flush() {
104 localStorage.removeItem(this.KEYS.ACCESS_TOKEN);
105 localStorage.removeItem(this.KEYS.REFRESH_TOKEN);
106 localStorage.removeItem(this.KEYS.TOKEN_TYPE);
107 }
108
109 constructor(hash?: any) {
110 if (hash) {
111 this.access_token = hash.access_token;
112 this.refresh_token = hash.refresh_token;
113
114 if (hash.token_type === 'bearer') {
115 this.token_type = 'Bearer';
116 } else {
117 this.token_type = hash.token_type;
118 }
119 }
120 }
121
122 save() {
123 localStorage.setItem('access_token', this.access_token);
124 localStorage.setItem('refresh_token', this.refresh_token);
125 localStorage.setItem('token_type', this.token_type);
126 }
127 }