]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
e2a2d6c8
C
1// Do not use the barrel (dependency loop)
2import { User } from '../../shared/users/user.model';
7da18e44
C
3
4export class AuthUser extends User {
bd5c83a8 5 private static KEYS = {
629d8d6f
C
6 ID: 'id',
7 ROLE: 'role',
92fb909c
C
8 USERNAME: 'username',
9 DISPLAY_NSFW: 'display_nsfw'
bd5c83a8
C
10 };
11
bd5c83a8
C
12 tokens: Tokens;
13
14 static load() {
15 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
16 if (usernameLocalStorage) {
7da18e44
C
17 return new AuthUser(
18 {
feb4bdfd 19 id: parseInt(localStorage.getItem(this.KEYS.ID)),
7da18e44 20 username: localStorage.getItem(this.KEYS.USERNAME),
92fb909c
C
21 role: localStorage.getItem(this.KEYS.ROLE),
22 displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true'
7da18e44 23 },
629d8d6f
C
24 Tokens.load()
25 );
bd5c83a8
C
26 }
27
28 return null;
29 }
30
31 static flush() {
32 localStorage.removeItem(this.KEYS.USERNAME);
629d8d6f
C
33 localStorage.removeItem(this.KEYS.ID);
34 localStorage.removeItem(this.KEYS.ROLE);
92fb909c 35 localStorage.removeItem(this.KEYS.DISPLAY_NSFW);
bd5c83a8
C
36 Tokens.flush();
37 }
38
92fb909c
C
39 constructor(userHash: {
40 id: number,
41 username: string,
42 role: string,
43 displayNSFW: boolean
44 }, hashTokens: any) {
7da18e44
C
45 super(userHash);
46 this.tokens = new Tokens(hashTokens);
bd5c83a8
C
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() {
feb4bdfd 67 localStorage.setItem(AuthUser.KEYS.ID, this.id.toString());
7da18e44
C
68 localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
69 localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
af5e743b 70 localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW));
bd5c83a8
C
71 this.tokens.save();
72 }
73}
74
629d8d6f 75// Private class only used by User
bd5c83a8
C
76class 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}