aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/root-helpers')
-rw-r--r--client/src/root-helpers/index.ts4
-rw-r--r--client/src/root-helpers/peertube-web-storage.ts81
-rw-r--r--client/src/root-helpers/pure-auth-user.model.ts123
-rw-r--r--client/src/root-helpers/user-keys.ts15
-rw-r--r--client/src/root-helpers/utils.ts12
5 files changed, 235 insertions, 0 deletions
diff --git a/client/src/root-helpers/index.ts b/client/src/root-helpers/index.ts
new file mode 100644
index 000000000..5ed4933f1
--- /dev/null
+++ b/client/src/root-helpers/index.ts
@@ -0,0 +1,4 @@
1export * from './peertube-web-storage'
2export * from './utils'
3export * from './user-keys'
4export * from './pure-auth-user.model'
diff --git a/client/src/root-helpers/peertube-web-storage.ts b/client/src/root-helpers/peertube-web-storage.ts
new file mode 100644
index 000000000..0db1301bd
--- /dev/null
+++ b/client/src/root-helpers/peertube-web-storage.ts
@@ -0,0 +1,81 @@
1// Thanks: https://github.com/capaj/localstorage-polyfill
2
3const valuesMap = new Map()
4
5function proxify (instance: MemoryStorage) {
6 return new Proxy(instance, {
7 set: function (obj, prop: string | number, value) {
8 if (MemoryStorage.prototype.hasOwnProperty(prop)) {
9 instance[prop] = value
10 } else {
11 instance.setItem(prop, value)
12 }
13 return true
14 },
15 get: function (target, name: string | number) {
16 if (MemoryStorage.prototype.hasOwnProperty(name)) {
17 return instance[name]
18 }
19 if (valuesMap.has(name)) {
20 return instance.getItem(name)
21 }
22 }
23 })
24}
25
26class MemoryStorage {
27 [key: string]: any
28 [index: number]: string
29
30 getItem (key: any) {
31 const stringKey = String(key)
32 if (valuesMap.has(key)) {
33 return String(valuesMap.get(stringKey))
34 }
35
36 return null
37 }
38
39 setItem (key: any, val: any) {
40 valuesMap.set(String(key), String(val))
41 }
42
43 removeItem (key: any) {
44 valuesMap.delete(key)
45 }
46
47 clear () {
48 valuesMap.clear()
49 }
50
51 key (i: any) {
52 if (arguments.length === 0) {
53 throw new TypeError('Failed to execute "key" on "Storage": 1 argument required, but only 0 present.')
54 }
55
56 const arr = Array.from(valuesMap.keys())
57 return arr[i]
58 }
59
60 get length () {
61 return valuesMap.size
62 }
63}
64
65let peertubeLocalStorage: Storage
66let peertubeSessionStorage: Storage
67try {
68 peertubeLocalStorage = localStorage
69 peertubeSessionStorage = sessionStorage
70} catch (err) {
71 const instanceLocalStorage = new MemoryStorage()
72 const instanceSessionStorage = new MemoryStorage()
73
74 peertubeLocalStorage = proxify(instanceLocalStorage)
75 peertubeSessionStorage = proxify(instanceSessionStorage)
76}
77
78export {
79 peertubeLocalStorage,
80 peertubeSessionStorage
81}
diff --git a/client/src/root-helpers/pure-auth-user.model.ts b/client/src/root-helpers/pure-auth-user.model.ts
new file mode 100644
index 000000000..81226da01
--- /dev/null
+++ b/client/src/root-helpers/pure-auth-user.model.ts
@@ -0,0 +1,123 @@
1// pure version of auth-user, that doesn't import app packages
2import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
3import {
4 MyUser as ServerMyUserModel,
5 MyUserSpecialPlaylist,
6 NSFWPolicyType,
7 UserRole
8} from '@shared/models'
9import { UserKeys } from '@root-helpers/user-keys'
10
11export type TokenOptions = {
12 accessToken: string
13 refreshToken: string
14 tokenType: string
15}
16
17// Private class only used by User
18export class Tokens {
19 private static KEYS = {
20 ACCESS_TOKEN: 'access_token',
21 REFRESH_TOKEN: 'refresh_token',
22 TOKEN_TYPE: 'token_type'
23 }
24
25 accessToken: string
26 refreshToken: string
27 tokenType: string
28
29 static load () {
30 const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
31 const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
32 const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
33
34 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
35 return new Tokens({
36 accessToken: accessTokenLocalStorage,
37 refreshToken: refreshTokenLocalStorage,
38 tokenType: tokenTypeLocalStorage
39 })
40 }
41
42 return null
43 }
44
45 static flush () {
46 peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
47 peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
48 peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
49 }
50
51 constructor (hash?: TokenOptions) {
52 if (hash) {
53 this.accessToken = hash.accessToken
54 this.refreshToken = hash.refreshToken
55
56 if (hash.tokenType === 'bearer') {
57 this.tokenType = 'Bearer'
58 } else {
59 this.tokenType = hash.tokenType
60 }
61 }
62 }
63
64 save () {
65 peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
66 peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
67 peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
68 }
69}
70
71export class PureAuthUser {
72 tokens: Tokens
73 specialPlaylists: MyUserSpecialPlaylist[]
74
75 canSeeVideosLink = true
76
77 static load () {
78 const usernameLocalStorage = peertubeLocalStorage.getItem(UserKeys.USERNAME)
79 if (usernameLocalStorage) {
80 return new PureAuthUser(
81 {
82 id: parseInt(peertubeLocalStorage.getItem(UserKeys.ID), 10),
83 username: peertubeLocalStorage.getItem(UserKeys.USERNAME),
84 email: peertubeLocalStorage.getItem(UserKeys.EMAIL),
85 role: parseInt(peertubeLocalStorage.getItem(UserKeys.ROLE), 10) as UserRole,
86 nsfwPolicy: peertubeLocalStorage.getItem(UserKeys.NSFW_POLICY) as NSFWPolicyType,
87 webTorrentEnabled: peertubeLocalStorage.getItem(UserKeys.WEBTORRENT_ENABLED) === 'true',
88 autoPlayVideo: peertubeLocalStorage.getItem(UserKeys.AUTO_PLAY_VIDEO) === 'true',
89 videosHistoryEnabled: peertubeLocalStorage.getItem(UserKeys.VIDEOS_HISTORY_ENABLED) === 'true'
90 },
91 Tokens.load()
92 )
93 }
94
95 return null
96 }
97
98 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {
99 this.tokens = new Tokens(hashTokens)
100 this.specialPlaylists = userHash.specialPlaylists
101 }
102
103 getAccessToken () {
104 return this.tokens.accessToken
105 }
106
107 getRefreshToken () {
108 return this.tokens.refreshToken
109 }
110
111 getTokenType () {
112 return this.tokens.tokenType
113 }
114
115 refreshTokens (accessToken: string, refreshToken: string) {
116 this.tokens.accessToken = accessToken
117 this.tokens.refreshToken = refreshToken
118 }
119
120 save () {
121 this.tokens.save()
122 }
123}
diff --git a/client/src/root-helpers/user-keys.ts b/client/src/root-helpers/user-keys.ts
new file mode 100644
index 000000000..897be8c43
--- /dev/null
+++ b/client/src/root-helpers/user-keys.ts
@@ -0,0 +1,15 @@
1export const UserKeys = {
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/utils.ts b/client/src/root-helpers/utils.ts
new file mode 100644
index 000000000..acfb565a3
--- /dev/null
+++ b/client/src/root-helpers/utils.ts
@@ -0,0 +1,12 @@
1function objectToUrlEncoded (obj: any) {
2 const str: string[] = []
3 for (const key of Object.keys(obj)) {
4 str.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
5 }
6
7 return str.join('&')
8}
9
10export {
11 objectToUrlEncoded
12}