]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/auth/auth-user.model.ts
Remove ng2-completer
[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'
df98563e
C
3import { User } from '../../shared/users/user.model'
4
5export type TokenOptions = {
6 accessToken: string
7 refreshToken: string
8 tokenType: string
9}
10
11// Private class only used by User
12class 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}
7da18e44
C
64
65export class AuthUser extends User {
bd5c83a8 66 private static KEYS = {
629d8d6f
C
67 ID: 'id',
68 ROLE: 'role',
66dd264f 69 EMAIL: 'email',
92fb909c
C
70 USERNAME: 'username',
71 DISPLAY_NSFW: 'display_nsfw'
df98563e 72 }
bd5c83a8 73
df98563e 74 tokens: Tokens
bd5c83a8 75
df98563e
C
76 static load () {
77 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME)
bd5c83a8 78 if (usernameLocalStorage) {
7da18e44
C
79 return new AuthUser(
80 {
df98563e 81 id: parseInt(localStorage.getItem(this.KEYS.ID), 10),
7da18e44 82 username: localStorage.getItem(this.KEYS.USERNAME),
66dd264f 83 email: localStorage.getItem(this.KEYS.EMAIL),
46757b47 84 role: localStorage.getItem(this.KEYS.ROLE) as UserRole,
92fb909c 85 displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true'
7da18e44 86 },
629d8d6f 87 Tokens.load()
df98563e 88 )
bd5c83a8
C
89 }
90
df98563e 91 return null
bd5c83a8
C
92 }
93
df98563e
C
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()
bd5c83a8
C
100 }
101
df98563e 102 constructor (userHash: {
92fb909c
C
103 id: number,
104 username: string,
46757b47 105 role: UserRole,
66dd264f 106 email: string,
92fb909c 107 displayNSFW: boolean
df98563e
C
108 }, hashTokens: TokenOptions) {
109 super(userHash)
110 this.tokens = new Tokens(hashTokens)
bd5c83a8
C
111 }
112
df98563e
C
113 getAccessToken () {
114 return this.tokens.accessToken
bd5c83a8 115 }
bd5c83a8 116
df98563e
C
117 getRefreshToken () {
118 return this.tokens.refreshToken
bd5c83a8
C
119 }
120
df98563e
C
121 getTokenType () {
122 return this.tokens.tokenType
bd5c83a8
C
123 }
124
df98563e
C
125 refreshTokens (accessToken: string, refreshToken: string) {
126 this.tokens.accessToken = accessToken
127 this.tokens.refreshToken = refreshToken
bd5c83a8
C
128 }
129
df98563e
C
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()
bd5c83a8
C
136 }
137}