aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/standalone/videos/shared/auth-http.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/standalone/videos/shared/auth-http.ts')
-rw-r--r--client/src/standalone/videos/shared/auth-http.ts28
1 files changed, 14 insertions, 14 deletions
diff --git a/client/src/standalone/videos/shared/auth-http.ts b/client/src/standalone/videos/shared/auth-http.ts
index 0356ab8a6..43af5dff4 100644
--- a/client/src/standalone/videos/shared/auth-http.ts
+++ b/client/src/standalone/videos/shared/auth-http.ts
@@ -1,5 +1,5 @@
1import { HttpStatusCode, OAuth2ErrorCode, UserRefreshToken } from '../../../../../shared/models' 1import { HttpStatusCode, OAuth2ErrorCode, UserRefreshToken } from '../../../../../shared/models'
2import { objectToUrlEncoded, UserTokens } from '../../../root-helpers' 2import { OAuthUserTokens, objectToUrlEncoded } from '../../../root-helpers'
3import { peertubeLocalStorage } from '../../../root-helpers/peertube-web-storage' 3import { peertubeLocalStorage } from '../../../root-helpers/peertube-web-storage'
4 4
5export class AuthHTTP { 5export class AuthHTTP {
@@ -8,30 +8,30 @@ export class AuthHTTP {
8 CLIENT_SECRET: 'client_secret' 8 CLIENT_SECRET: 'client_secret'
9 } 9 }
10 10
11 private userTokens: UserTokens 11 private userOAuthTokens: OAuthUserTokens
12 12
13 private headers = new Headers() 13 private headers = new Headers()
14 14
15 constructor () { 15 constructor () {
16 this.userTokens = UserTokens.getUserTokens(peertubeLocalStorage) 16 this.userOAuthTokens = OAuthUserTokens.getUserTokens(peertubeLocalStorage)
17 17
18 if (this.userTokens) this.setHeadersFromTokens() 18 if (this.userOAuthTokens) this.setHeadersFromTokens()
19 } 19 }
20 20
21 fetch (url: string, { optionalAuth }: { optionalAuth: boolean }) { 21 fetch (url: string, { optionalAuth, method }: { optionalAuth: boolean, method?: string }) {
22 const refreshFetchOptions = optionalAuth 22 const refreshFetchOptions = optionalAuth
23 ? { headers: this.headers } 23 ? { headers: this.headers }
24 : {} 24 : {}
25 25
26 return this.refreshFetch(url.toString(), refreshFetchOptions) 26 return this.refreshFetch(url.toString(), { ...refreshFetchOptions, method })
27 } 27 }
28 28
29 getHeaderTokenValue () { 29 getHeaderTokenValue () {
30 return `${this.userTokens.tokenType} ${this.userTokens.accessToken}` 30 return `${this.userOAuthTokens.tokenType} ${this.userOAuthTokens.accessToken}`
31 } 31 }
32 32
33 isLoggedIn () { 33 isLoggedIn () {
34 return !!this.userTokens 34 return !!this.userOAuthTokens
35 } 35 }
36 36
37 private refreshFetch (url: string, options?: RequestInit) { 37 private refreshFetch (url: string, options?: RequestInit) {
@@ -47,7 +47,7 @@ export class AuthHTTP {
47 headers.set('Content-Type', 'application/x-www-form-urlencoded') 47 headers.set('Content-Type', 'application/x-www-form-urlencoded')
48 48
49 const data = { 49 const data = {
50 refresh_token: this.userTokens.refreshToken, 50 refresh_token: this.userOAuthTokens.refreshToken,
51 client_id: clientId, 51 client_id: clientId,
52 client_secret: clientSecret, 52 client_secret: clientSecret,
53 response_type: 'code', 53 response_type: 'code',
@@ -64,15 +64,15 @@ export class AuthHTTP {
64 return res.json() 64 return res.json()
65 }).then((obj: UserRefreshToken & { code?: OAuth2ErrorCode }) => { 65 }).then((obj: UserRefreshToken & { code?: OAuth2ErrorCode }) => {
66 if (!obj || obj.code === OAuth2ErrorCode.INVALID_GRANT) { 66 if (!obj || obj.code === OAuth2ErrorCode.INVALID_GRANT) {
67 UserTokens.flushLocalStorage(peertubeLocalStorage) 67 OAuthUserTokens.flushLocalStorage(peertubeLocalStorage)
68 this.removeTokensFromHeaders() 68 this.removeTokensFromHeaders()
69 69
70 return resolve() 70 return resolve()
71 } 71 }
72 72
73 this.userTokens.accessToken = obj.access_token 73 this.userOAuthTokens.accessToken = obj.access_token
74 this.userTokens.refreshToken = obj.refresh_token 74 this.userOAuthTokens.refreshToken = obj.refresh_token
75 UserTokens.saveToLocalStorage(peertubeLocalStorage, this.userTokens) 75 OAuthUserTokens.saveToLocalStorage(peertubeLocalStorage, this.userOAuthTokens)
76 76
77 this.setHeadersFromTokens() 77 this.setHeadersFromTokens()
78 78
@@ -84,7 +84,7 @@ export class AuthHTTP {
84 84
85 return refreshingTokenPromise 85 return refreshingTokenPromise
86 .catch(() => { 86 .catch(() => {
87 UserTokens.flushLocalStorage(peertubeLocalStorage) 87 OAuthUserTokens.flushLocalStorage(peertubeLocalStorage)
88 88
89 this.removeTokensFromHeaders() 89 this.removeTokensFromHeaders()
90 }).then(() => fetch(url, { 90 }).then(() => fetch(url, {