aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers/users
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/root-helpers/users')
-rw-r--r--client/src/root-helpers/users/index.ts3
-rw-r--r--client/src/root-helpers/users/user-local-storage-keys.ts15
-rw-r--r--client/src/root-helpers/users/user-local-storage-manager.ts55
-rw-r--r--client/src/root-helpers/users/user-tokens.ts61
4 files changed, 134 insertions, 0 deletions
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 @@
1export * from './user-local-storage-keys'
2export * from './user-local-storage-manager'
3export * 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 @@
1export const UserLocalStorageKeys = {
2 ID: 'id',
3 ROLE: 'role',
4 EMAIL: 'email',
5 VIDEOS_HISTORY_ENABLED: 'videos-history-enabled',
6 USERNAME: 'username',
7 NSFW_POLICY: 'nsfw_policy',
8 WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled',
9 AUTO_PLAY_VIDEO: 'auto_play_video',
10 SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO: 'auto_play_next_video',
11 AUTO_PLAY_VIDEO_PLAYLIST: 'auto_play_video_playlist',
12 THEME: 'theme',
13 LAST_ACTIVE_THEME: 'last_active_theme',
14 VIDEO_LANGUAGES: 'video_languages'
15}
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 @@
1import { NSFWPolicyType, UserRole } from '@shared/models'
2import { peertubeLocalStorage } from '../peertube-web-storage'
3import { UserLocalStorageKeys } from './user-local-storage-keys'
4
5function getUserInfoFromLocalStorage () {
6 const usernameLocalStorage = peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME)
7
8 if (!usernameLocalStorage) return undefined
9
10 return {
11 id: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ID), 10),
12 username: peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME),
13 email: peertubeLocalStorage.getItem(UserLocalStorageKeys.EMAIL),
14 role: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole,
15 nsfwPolicy: peertubeLocalStorage.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType,
16 webTorrentEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) === 'true',
17 autoPlayVideo: peertubeLocalStorage.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true',
18 videosHistoryEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED) === 'true'
19 }
20}
21
22function flushUserInfoFromLocalStorage () {
23 peertubeLocalStorage.removeItem(UserLocalStorageKeys.ID)
24 peertubeLocalStorage.removeItem(UserLocalStorageKeys.USERNAME)
25 peertubeLocalStorage.removeItem(UserLocalStorageKeys.EMAIL)
26 peertubeLocalStorage.removeItem(UserLocalStorageKeys.ROLE)
27 peertubeLocalStorage.removeItem(UserLocalStorageKeys.NSFW_POLICY)
28 peertubeLocalStorage.removeItem(UserLocalStorageKeys.WEBTORRENT_ENABLED)
29 peertubeLocalStorage.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO)
30 peertubeLocalStorage.removeItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED)
31}
32
33function saveUserInfoIntoLocalStorage (info: {
34 id: number
35 username: string
36 email: string
37 role: UserRole
38 nsfwPolicy: NSFWPolicyType
39 webTorrentEnabled: boolean
40 autoPlayVideo: boolean
41}) {
42 peertubeLocalStorage.setItem(UserLocalStorageKeys.ID, info.id.toString())
43 peertubeLocalStorage.setItem(UserLocalStorageKeys.USERNAME, info.username)
44 peertubeLocalStorage.setItem(UserLocalStorageKeys.EMAIL, info.email)
45 peertubeLocalStorage.setItem(UserLocalStorageKeys.ROLE, info.role.toString())
46 peertubeLocalStorage.setItem(UserLocalStorageKeys.NSFW_POLICY, info.nsfwPolicy.toString())
47 peertubeLocalStorage.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, JSON.stringify(info.webTorrentEnabled))
48 peertubeLocalStorage.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, JSON.stringify(info.autoPlayVideo))
49}
50
51export {
52 getUserInfoFromLocalStorage,
53 saveUserInfoIntoLocalStorage,
54 flushUserInfoFromLocalStorage
55}
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 @@
1import { peertubeLocalStorage } from '../peertube-web-storage'
2
3export type TokenOptions = {
4 accessToken: string
5 refreshToken: string
6 tokenType: string
7}
8
9// Private class only used by User
10export class Tokens {
11 private static KEYS = {
12 ACCESS_TOKEN: 'access_token',
13 REFRESH_TOKEN: 'refresh_token',
14 TOKEN_TYPE: 'token_type'
15 }
16
17 accessToken: string
18 refreshToken: string
19 tokenType: string
20
21 static load () {
22 const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
23 const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
24 const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
25
26 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
27 return new Tokens({
28 accessToken: accessTokenLocalStorage,
29 refreshToken: refreshTokenLocalStorage,
30 tokenType: tokenTypeLocalStorage
31 })
32 }
33
34 return null
35 }
36
37 static flush () {
38 peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
39 peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
40 peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
41 }
42
43 constructor (hash?: TokenOptions) {
44 if (hash) {
45 this.accessToken = hash.accessToken
46 this.refreshToken = hash.refreshToken
47
48 if (hash.tokenType === 'bearer') {
49 this.tokenType = 'Bearer'
50 } else {
51 this.tokenType = hash.tokenType
52 }
53 }
54 }
55
56 save () {
57 peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
58 peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
59 peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
60 }
61}