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