From a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 6 Aug 2020 15:25:19 +0200 Subject: Cleanup tokens logic in embed --- client/src/root-helpers/index.ts | 3 +- client/src/root-helpers/pure-auth-user.model.ts | 123 --------------------- client/src/root-helpers/user-keys.ts | 15 --- client/src/root-helpers/users/index.ts | 3 + .../root-helpers/users/user-local-storage-keys.ts | 15 +++ .../users/user-local-storage-manager.ts | 55 +++++++++ client/src/root-helpers/users/user-tokens.ts | 61 ++++++++++ 7 files changed, 135 insertions(+), 140 deletions(-) delete mode 100644 client/src/root-helpers/pure-auth-user.model.ts delete mode 100644 client/src/root-helpers/user-keys.ts create mode 100644 client/src/root-helpers/users/index.ts create mode 100644 client/src/root-helpers/users/user-local-storage-keys.ts create mode 100644 client/src/root-helpers/users/user-local-storage-manager.ts create mode 100644 client/src/root-helpers/users/user-tokens.ts (limited to 'client/src/root-helpers') diff --git a/client/src/root-helpers/index.ts b/client/src/root-helpers/index.ts index 5ed4933f1..59468b31c 100644 --- a/client/src/root-helpers/index.ts +++ b/client/src/root-helpers/index.ts @@ -1,4 +1,3 @@ +export * from './users' export * from './peertube-web-storage' export * from './utils' -export * from './user-keys' -export * from './pure-auth-user.model' diff --git a/client/src/root-helpers/pure-auth-user.model.ts b/client/src/root-helpers/pure-auth-user.model.ts deleted file mode 100644 index 81226da01..000000000 --- a/client/src/root-helpers/pure-auth-user.model.ts +++ /dev/null @@ -1,123 +0,0 @@ -// pure version of auth-user, that doesn't import app packages -import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' -import { - MyUser as ServerMyUserModel, - MyUserSpecialPlaylist, - NSFWPolicyType, - UserRole -} from '@shared/models' -import { UserKeys } from '@root-helpers/user-keys' - -export type TokenOptions = { - accessToken: string - refreshToken: string - tokenType: string -} - -// Private class only used by User -export class Tokens { - private static KEYS = { - ACCESS_TOKEN: 'access_token', - REFRESH_TOKEN: 'refresh_token', - TOKEN_TYPE: 'token_type' - } - - accessToken: string - refreshToken: string - tokenType: string - - static load () { - const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN) - const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN) - const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE) - - if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { - return new Tokens({ - accessToken: accessTokenLocalStorage, - refreshToken: refreshTokenLocalStorage, - tokenType: tokenTypeLocalStorage - }) - } - - return null - } - - static flush () { - peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN) - peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN) - peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE) - } - - constructor (hash?: TokenOptions) { - if (hash) { - this.accessToken = hash.accessToken - this.refreshToken = hash.refreshToken - - if (hash.tokenType === 'bearer') { - this.tokenType = 'Bearer' - } else { - this.tokenType = hash.tokenType - } - } - } - - save () { - peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) - peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) - peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) - } -} - -export class PureAuthUser { - tokens: Tokens - specialPlaylists: MyUserSpecialPlaylist[] - - canSeeVideosLink = true - - static load () { - const usernameLocalStorage = peertubeLocalStorage.getItem(UserKeys.USERNAME) - if (usernameLocalStorage) { - return new PureAuthUser( - { - id: parseInt(peertubeLocalStorage.getItem(UserKeys.ID), 10), - username: peertubeLocalStorage.getItem(UserKeys.USERNAME), - email: peertubeLocalStorage.getItem(UserKeys.EMAIL), - role: parseInt(peertubeLocalStorage.getItem(UserKeys.ROLE), 10) as UserRole, - nsfwPolicy: peertubeLocalStorage.getItem(UserKeys.NSFW_POLICY) as NSFWPolicyType, - webTorrentEnabled: peertubeLocalStorage.getItem(UserKeys.WEBTORRENT_ENABLED) === 'true', - autoPlayVideo: peertubeLocalStorage.getItem(UserKeys.AUTO_PLAY_VIDEO) === 'true', - videosHistoryEnabled: peertubeLocalStorage.getItem(UserKeys.VIDEOS_HISTORY_ENABLED) === 'true' - }, - Tokens.load() - ) - } - - return null - } - - constructor (userHash: Partial, hashTokens: TokenOptions) { - this.tokens = new Tokens(hashTokens) - this.specialPlaylists = userHash.specialPlaylists - } - - getAccessToken () { - return this.tokens.accessToken - } - - getRefreshToken () { - return this.tokens.refreshToken - } - - getTokenType () { - return this.tokens.tokenType - } - - refreshTokens (accessToken: string, refreshToken: string) { - this.tokens.accessToken = accessToken - this.tokens.refreshToken = refreshToken - } - - save () { - this.tokens.save() - } -} diff --git a/client/src/root-helpers/user-keys.ts b/client/src/root-helpers/user-keys.ts deleted file mode 100644 index 897be8c43..000000000 --- a/client/src/root-helpers/user-keys.ts +++ /dev/null @@ -1,15 +0,0 @@ -export const UserKeys = { - ID: 'id', - ROLE: 'role', - EMAIL: 'email', - VIDEOS_HISTORY_ENABLED: 'videos-history-enabled', - USERNAME: 'username', - NSFW_POLICY: 'nsfw_policy', - WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled', - AUTO_PLAY_VIDEO: 'auto_play_video', - SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO: 'auto_play_next_video', - AUTO_PLAY_VIDEO_PLAYLIST: 'auto_play_video_playlist', - THEME: 'theme', - LAST_ACTIVE_THEME: 'last_active_theme', - VIDEO_LANGUAGES: 'video_languages' -} diff --git a/client/src/root-helpers/users/index.ts b/client/src/root-helpers/users/index.ts new file mode 100644 index 000000000..8fbaca9e3 --- /dev/null +++ b/client/src/root-helpers/users/index.ts @@ -0,0 +1,3 @@ +export * from './user-local-storage-keys' +export * from './user-local-storage-manager' +export * from './user-tokens' diff --git a/client/src/root-helpers/users/user-local-storage-keys.ts b/client/src/root-helpers/users/user-local-storage-keys.ts new file mode 100644 index 000000000..5f915899c --- /dev/null +++ b/client/src/root-helpers/users/user-local-storage-keys.ts @@ -0,0 +1,15 @@ +export const UserLocalStorageKeys = { + ID: 'id', + ROLE: 'role', + EMAIL: 'email', + VIDEOS_HISTORY_ENABLED: 'videos-history-enabled', + USERNAME: 'username', + NSFW_POLICY: 'nsfw_policy', + WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled', + AUTO_PLAY_VIDEO: 'auto_play_video', + SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO: 'auto_play_next_video', + AUTO_PLAY_VIDEO_PLAYLIST: 'auto_play_video_playlist', + THEME: 'theme', + LAST_ACTIVE_THEME: 'last_active_theme', + VIDEO_LANGUAGES: 'video_languages' +} diff --git a/client/src/root-helpers/users/user-local-storage-manager.ts b/client/src/root-helpers/users/user-local-storage-manager.ts new file mode 100644 index 000000000..c75cea127 --- /dev/null +++ b/client/src/root-helpers/users/user-local-storage-manager.ts @@ -0,0 +1,55 @@ +import { NSFWPolicyType, UserRole } from '@shared/models' +import { peertubeLocalStorage } from '../peertube-web-storage' +import { UserLocalStorageKeys } from './user-local-storage-keys' + +function getUserInfoFromLocalStorage () { + const usernameLocalStorage = peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME) + + if (!usernameLocalStorage) return undefined + + return { + id: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ID), 10), + username: peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME), + email: peertubeLocalStorage.getItem(UserLocalStorageKeys.EMAIL), + role: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole, + nsfwPolicy: peertubeLocalStorage.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType, + webTorrentEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) === 'true', + autoPlayVideo: peertubeLocalStorage.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true', + videosHistoryEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED) === 'true' + } +} + +function flushUserInfoFromLocalStorage () { + peertubeLocalStorage.removeItem(UserLocalStorageKeys.ID) + peertubeLocalStorage.removeItem(UserLocalStorageKeys.USERNAME) + peertubeLocalStorage.removeItem(UserLocalStorageKeys.EMAIL) + peertubeLocalStorage.removeItem(UserLocalStorageKeys.ROLE) + peertubeLocalStorage.removeItem(UserLocalStorageKeys.NSFW_POLICY) + peertubeLocalStorage.removeItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) + peertubeLocalStorage.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) + peertubeLocalStorage.removeItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED) +} + +function saveUserInfoIntoLocalStorage (info: { + id: number + username: string + email: string + role: UserRole + nsfwPolicy: NSFWPolicyType + webTorrentEnabled: boolean + autoPlayVideo: boolean +}) { + peertubeLocalStorage.setItem(UserLocalStorageKeys.ID, info.id.toString()) + peertubeLocalStorage.setItem(UserLocalStorageKeys.USERNAME, info.username) + peertubeLocalStorage.setItem(UserLocalStorageKeys.EMAIL, info.email) + peertubeLocalStorage.setItem(UserLocalStorageKeys.ROLE, info.role.toString()) + peertubeLocalStorage.setItem(UserLocalStorageKeys.NSFW_POLICY, info.nsfwPolicy.toString()) + peertubeLocalStorage.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, JSON.stringify(info.webTorrentEnabled)) + peertubeLocalStorage.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, JSON.stringify(info.autoPlayVideo)) +} + +export { + getUserInfoFromLocalStorage, + saveUserInfoIntoLocalStorage, + flushUserInfoFromLocalStorage +} diff --git a/client/src/root-helpers/users/user-tokens.ts b/client/src/root-helpers/users/user-tokens.ts new file mode 100644 index 000000000..d42e1c8f3 --- /dev/null +++ b/client/src/root-helpers/users/user-tokens.ts @@ -0,0 +1,61 @@ +import { peertubeLocalStorage } from '../peertube-web-storage' + +export type TokenOptions = { + accessToken: string + refreshToken: string + tokenType: string +} + +// Private class only used by User +export class Tokens { + private static KEYS = { + ACCESS_TOKEN: 'access_token', + REFRESH_TOKEN: 'refresh_token', + TOKEN_TYPE: 'token_type' + } + + accessToken: string + refreshToken: string + tokenType: string + + static load () { + const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN) + const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN) + const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE) + + if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { + return new Tokens({ + accessToken: accessTokenLocalStorage, + refreshToken: refreshTokenLocalStorage, + tokenType: tokenTypeLocalStorage + }) + } + + return null + } + + static flush () { + peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN) + peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN) + peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE) + } + + constructor (hash?: TokenOptions) { + if (hash) { + this.accessToken = hash.accessToken + this.refreshToken = hash.refreshToken + + if (hash.tokenType === 'bearer') { + this.tokenType = 'Bearer' + } else { + this.tokenType = hash.tokenType + } + } + } + + save () { + peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) + peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) + peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) + } +} -- cgit v1.2.3