]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/auth/auth-user.model.ts
Client: check token valitidy at startup
[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',
bd5c83a8
C
8 USERNAME: 'username'
9 };
10
bd5c83a8
C
11 tokens: Tokens;
12
13 static load() {
14 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
15 if (usernameLocalStorage) {
7da18e44
C
16 return new AuthUser(
17 {
feb4bdfd 18 id: parseInt(localStorage.getItem(this.KEYS.ID)),
7da18e44
C
19 username: localStorage.getItem(this.KEYS.USERNAME),
20 role: localStorage.getItem(this.KEYS.ROLE)
21 },
629d8d6f
C
22 Tokens.load()
23 );
bd5c83a8
C
24 }
25
26 return null;
27 }
28
29 static flush() {
30 localStorage.removeItem(this.KEYS.USERNAME);
629d8d6f
C
31 localStorage.removeItem(this.KEYS.ID);
32 localStorage.removeItem(this.KEYS.ROLE);
bd5c83a8
C
33 Tokens.flush();
34 }
35
feb4bdfd 36 constructor(userHash: { id: number, username: string, role: string }, hashTokens: any) {
7da18e44
C
37 super(userHash);
38 this.tokens = new Tokens(hashTokens);
bd5c83a8
C
39 }
40
41 getAccessToken() {
42 return this.tokens.access_token;
43 }
44
45 getRefreshToken() {
46 return this.tokens.refresh_token;
47 }
48
49 getTokenType() {
50 return this.tokens.token_type;
51 }
52
53 refreshTokens(access_token: string, refresh_token: string) {
54 this.tokens.access_token = access_token;
55 this.tokens.refresh_token = refresh_token;
56 }
57
58 save() {
feb4bdfd 59 localStorage.setItem(AuthUser.KEYS.ID, this.id.toString());
7da18e44
C
60 localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
61 localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
bd5c83a8
C
62 this.tokens.save();
63 }
64}
65
629d8d6f 66// Private class only used by User
bd5c83a8
C
67class Tokens {
68 private static KEYS = {
69 ACCESS_TOKEN: 'access_token',
70 REFRESH_TOKEN: 'refresh_token',
71 TOKEN_TYPE: 'token_type',
72 };
73
74 access_token: string;
75 refresh_token: string;
76 token_type: string;
77
78 static load() {
79 const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN);
80 const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN);
81 const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE);
82
83 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
84 return new Tokens({
85 access_token: accessTokenLocalStorage,
86 refresh_token: refreshTokenLocalStorage,
87 token_type: tokenTypeLocalStorage
88 });
89 }
90
91 return null;
92 }
93
94 static flush() {
95 localStorage.removeItem(this.KEYS.ACCESS_TOKEN);
96 localStorage.removeItem(this.KEYS.REFRESH_TOKEN);
97 localStorage.removeItem(this.KEYS.TOKEN_TYPE);
98 }
99
100 constructor(hash?: any) {
101 if (hash) {
102 this.access_token = hash.access_token;
103 this.refresh_token = hash.refresh_token;
104
105 if (hash.token_type === 'bearer') {
106 this.token_type = 'Bearer';
107 } else {
108 this.token_type = hash.token_type;
109 }
110 }
111 }
112
113 save() {
114 localStorage.setItem('access_token', this.access_token);
115 localStorage.setItem('refresh_token', this.refresh_token);
116 localStorage.setItem('token_type', this.token_type);
117 }
118}