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