Use typescript standard and lint all files
[oweals/peertube.git] / client / src / app / core / auth / auth-user.model.ts
1 // Do not use the barrel (dependency loop)
2 import { UserRole } from '../../../../../shared/models/user.model'
3 import { User } from '../../shared/users/user.model'
4
5 export type TokenOptions = {
6   accessToken: string
7   refreshToken: string
8   tokenType: string
9 }
10
11 // Private class only used by User
12 class Tokens {
13   private static KEYS = {
14     ACCESS_TOKEN: 'access_token',
15     REFRESH_TOKEN: 'refresh_token',
16     TOKEN_TYPE: 'token_type'
17   }
18
19   accessToken: string
20   refreshToken: string
21   tokenType: string
22
23   static load () {
24     const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN)
25     const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN)
26     const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE)
27
28     if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
29       return new Tokens({
30         accessToken: accessTokenLocalStorage,
31         refreshToken: refreshTokenLocalStorage,
32         tokenType: tokenTypeLocalStorage
33       })
34     }
35
36     return null
37   }
38
39   static flush () {
40     localStorage.removeItem(this.KEYS.ACCESS_TOKEN)
41     localStorage.removeItem(this.KEYS.REFRESH_TOKEN)
42     localStorage.removeItem(this.KEYS.TOKEN_TYPE)
43   }
44
45   constructor (hash?: TokenOptions) {
46     if (hash) {
47       this.accessToken = hash.accessToken
48       this.refreshToken = hash.refreshToken
49
50       if (hash.tokenType === 'bearer') {
51         this.tokenType = 'Bearer'
52       } else {
53         this.tokenType = hash.tokenType
54       }
55     }
56   }
57
58   save () {
59     localStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
60     localStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
61     localStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
62   }
63 }
64
65 export class AuthUser extends User {
66   private static KEYS = {
67     ID: 'id',
68     ROLE: 'role',
69     EMAIL: 'email',
70     USERNAME: 'username',
71     DISPLAY_NSFW: 'display_nsfw'
72   }
73
74   tokens: Tokens
75
76   static load () {
77     const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME)
78     if (usernameLocalStorage) {
79       return new AuthUser(
80         {
81           id: parseInt(localStorage.getItem(this.KEYS.ID), 10),
82           username: localStorage.getItem(this.KEYS.USERNAME),
83           email: localStorage.getItem(this.KEYS.EMAIL),
84           role: localStorage.getItem(this.KEYS.ROLE) as UserRole,
85           displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true'
86         },
87         Tokens.load()
88       )
89     }
90
91     return null
92   }
93
94   static flush () {
95     localStorage.removeItem(this.KEYS.USERNAME)
96     localStorage.removeItem(this.KEYS.ID)
97     localStorage.removeItem(this.KEYS.ROLE)
98     localStorage.removeItem(this.KEYS.DISPLAY_NSFW)
99     Tokens.flush()
100   }
101
102   constructor (userHash: {
103     id: number,
104     username: string,
105     role: UserRole,
106     email: string,
107     displayNSFW: boolean
108   }, hashTokens: TokenOptions) {
109     super(userHash)
110     this.tokens = new Tokens(hashTokens)
111   }
112
113   getAccessToken () {
114     return this.tokens.accessToken
115   }
116
117   getRefreshToken () {
118     return this.tokens.refreshToken
119   }
120
121   getTokenType () {
122     return this.tokens.tokenType
123   }
124
125   refreshTokens (accessToken: string, refreshToken: string) {
126     this.tokens.accessToken = accessToken
127     this.tokens.refreshToken = refreshToken
128   }
129
130   save () {
131     localStorage.setItem(AuthUser.KEYS.ID, this.id.toString())
132     localStorage.setItem(AuthUser.KEYS.USERNAME, this.username)
133     localStorage.setItem(AuthUser.KEYS.ROLE, this.role)
134     localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW))
135     this.tokens.save()
136   }
137 }