X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fauth%2Fauth-user.model.ts;h=4155aea1927a1f131e5208d6a583378d049dd365;hb=d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb;hp=5d61954d6105e2777d2d9d5c42ca13bb3f05920c;hpb=e2a2d6c86c7ca39074fdff3b545947d1d58dc008;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 5d61954d6..4155aea19 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts @@ -1,118 +1,139 @@ // Do not use the barrel (dependency loop) -import { User } from '../../shared/users/user.model'; +import { UserRole } from '../../../../../shared/models/users/user-role.type' +import { User } from '../../shared/users/user.model' -export class AuthUser extends User { +export type TokenOptions = { + accessToken: string + refreshToken: string + tokenType: string +} + +// Private class only used by User +class Tokens { private static KEYS = { - ID: 'id', - ROLE: 'role', - USERNAME: 'username' - }; + ACCESS_TOKEN: 'access_token', + REFRESH_TOKEN: 'refresh_token', + TOKEN_TYPE: 'token_type' + } - tokens: Tokens; + accessToken: string + refreshToken: string + tokenType: string - static load() { - const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); - if (usernameLocalStorage) { - return new AuthUser( - { - id: parseInt(localStorage.getItem(this.KEYS.ID)), - username: localStorage.getItem(this.KEYS.USERNAME), - role: localStorage.getItem(this.KEYS.ROLE) - }, - Tokens.load() - ); - } + static load () { + const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN) + const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN) + const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE) - return null; - } + if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { + return new Tokens({ + accessToken: accessTokenLocalStorage, + refreshToken: refreshTokenLocalStorage, + tokenType: tokenTypeLocalStorage + }) + } - static flush() { - localStorage.removeItem(this.KEYS.USERNAME); - localStorage.removeItem(this.KEYS.ID); - localStorage.removeItem(this.KEYS.ROLE); - Tokens.flush(); + return null } - constructor(userHash: { id: number, username: string, role: string }, hashTokens: any) { - super(userHash); - this.tokens = new Tokens(hashTokens); + static flush () { + localStorage.removeItem(this.KEYS.ACCESS_TOKEN) + localStorage.removeItem(this.KEYS.REFRESH_TOKEN) + localStorage.removeItem(this.KEYS.TOKEN_TYPE) } - getAccessToken() { - return this.tokens.access_token; - } + constructor (hash?: TokenOptions) { + if (hash) { + this.accessToken = hash.accessToken + this.refreshToken = hash.refreshToken - getRefreshToken() { - return this.tokens.refresh_token; + if (hash.tokenType === 'bearer') { + this.tokenType = 'Bearer' + } else { + this.tokenType = hash.tokenType + } + } } - getTokenType() { - return this.tokens.token_type; + save () { + localStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) + localStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) + localStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) } +} - refreshTokens(access_token: string, refresh_token: string) { - this.tokens.access_token = access_token; - this.tokens.refresh_token = refresh_token; +export class AuthUser extends User { + private static KEYS = { + ID: 'id', + ROLE: 'role', + EMAIL: 'email', + USERNAME: 'username', + DISPLAY_NSFW: 'display_nsfw' } - save() { - localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()); - localStorage.setItem(AuthUser.KEYS.USERNAME, this.username); - localStorage.setItem(AuthUser.KEYS.ROLE, this.role); - this.tokens.save(); - } -} + tokens: Tokens -// Private class only used by User -class Tokens { - private static KEYS = { - ACCESS_TOKEN: 'access_token', - REFRESH_TOKEN: 'refresh_token', - TOKEN_TYPE: 'token_type', - }; + static load () { + const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME) + if (usernameLocalStorage) { + return new AuthUser( + { + id: parseInt(localStorage.getItem(this.KEYS.ID), 10), + username: localStorage.getItem(this.KEYS.USERNAME), + email: localStorage.getItem(this.KEYS.EMAIL), + role: localStorage.getItem(this.KEYS.ROLE) as UserRole, + displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true' + }, + Tokens.load() + ) + } - access_token: string; - refresh_token: string; - token_type: string; + return null + } - static load() { - const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN); - const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN); - const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE); + static flush () { + localStorage.removeItem(this.KEYS.USERNAME) + localStorage.removeItem(this.KEYS.ID) + localStorage.removeItem(this.KEYS.ROLE) + localStorage.removeItem(this.KEYS.DISPLAY_NSFW) + localStorage.removeItem(this.KEYS.EMAIL) + Tokens.flush() + } - if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { - return new Tokens({ - access_token: accessTokenLocalStorage, - refresh_token: refreshTokenLocalStorage, - token_type: tokenTypeLocalStorage - }); - } + constructor (userHash: { + id: number, + username: string, + role: UserRole, + email: string, + displayNSFW: boolean + }, hashTokens: TokenOptions) { + super(userHash) + this.tokens = new Tokens(hashTokens) + } - return null; + getAccessToken () { + return this.tokens.accessToken } - static flush() { - localStorage.removeItem(this.KEYS.ACCESS_TOKEN); - localStorage.removeItem(this.KEYS.REFRESH_TOKEN); - localStorage.removeItem(this.KEYS.TOKEN_TYPE); + getRefreshToken () { + return this.tokens.refreshToken } - constructor(hash?: any) { - if (hash) { - this.access_token = hash.access_token; - this.refresh_token = hash.refresh_token; + getTokenType () { + return this.tokens.tokenType + } - if (hash.token_type === 'bearer') { - this.token_type = 'Bearer'; - } else { - this.token_type = hash.token_type; - } - } + refreshTokens (accessToken: string, refreshToken: string) { + this.tokens.accessToken = accessToken + this.tokens.refreshToken = refreshToken } - save() { - localStorage.setItem('access_token', this.access_token); - localStorage.setItem('refresh_token', this.refresh_token); - localStorage.setItem('token_type', this.token_type); + save () { + localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) + localStorage.setItem(AuthUser.KEYS.USERNAME, this.username) + localStorage.setItem(AuthUser.KEYS.EMAIL, this.email) + localStorage.setItem(AuthUser.KEYS.ROLE, this.role) + localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)) + this.tokens.save() } }