diff options
Diffstat (limited to 'client/src/standalone/videos')
-rw-r--r-- | client/src/standalone/videos/embed.ts | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index c927e900e..1dca84f72 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts | |||
@@ -74,7 +74,7 @@ export class PeerTubeEmbed { | |||
74 | return window.location.origin + '/api/v1/videos/' + id | 74 | return window.location.origin + '/api/v1/videos/' + id |
75 | } | 75 | } |
76 | 76 | ||
77 | refreshFetch (url: string, options?: Object) { | 77 | refreshFetch (url: string, options?: RequestInit) { |
78 | return fetch(url, options) | 78 | return fetch(url, options) |
79 | .then((res: Response) => { | 79 | .then((res: Response) => { |
80 | if (res.status !== 401) return res | 80 | if (res.status !== 401) return res |
@@ -85,8 +85,10 @@ export class PeerTubeEmbed { | |||
85 | const refreshingTokenPromise = new Promise((resolve, reject) => { | 85 | const refreshingTokenPromise = new Promise((resolve, reject) => { |
86 | const clientId: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) | 86 | const clientId: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) |
87 | const clientSecret: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET) | 87 | const clientSecret: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET) |
88 | |||
88 | const headers = new Headers() | 89 | const headers = new Headers() |
89 | headers.set('Content-Type', 'application/x-www-form-urlencoded') | 90 | headers.set('Content-Type', 'application/x-www-form-urlencoded') |
91 | |||
90 | const data = { | 92 | const data = { |
91 | refresh_token: this.userTokens.refreshToken, | 93 | refresh_token: this.userTokens.refreshToken, |
92 | client_id: clientId, | 94 | client_id: clientId, |
@@ -99,8 +101,7 @@ export class PeerTubeEmbed { | |||
99 | headers, | 101 | headers, |
100 | method: 'POST', | 102 | method: 'POST', |
101 | body: objectToUrlEncoded(data) | 103 | body: objectToUrlEncoded(data) |
102 | }) | 104 | }).then(res => res.json()) |
103 | .then(res => res.json()) | ||
104 | .then((obj: UserRefreshToken) => { | 105 | .then((obj: UserRefreshToken) => { |
105 | this.userTokens.accessToken = obj.access_token | 106 | this.userTokens.accessToken = obj.access_token |
106 | this.userTokens.refreshToken = obj.refresh_token | 107 | this.userTokens.refreshToken = obj.refresh_token |
@@ -116,10 +117,7 @@ export class PeerTubeEmbed { | |||
116 | }) | 117 | }) |
117 | 118 | ||
118 | return refreshingTokenPromise | 119 | return refreshingTokenPromise |
119 | .catch(() => { | 120 | .catch(() => this.removeTokensFromHeaders()) |
120 | // If refreshing fails, continue with original error | ||
121 | throw error | ||
122 | }) | ||
123 | .then(() => fetch(url, { | 121 | .then(() => fetch(url, { |
124 | ...options, | 122 | ...options, |
125 | headers: this.headers | 123 | headers: this.headers |
@@ -694,6 +692,10 @@ export class PeerTubeEmbed { | |||
694 | this.headers.set('Authorization', `${this.userTokens.tokenType} ${this.userTokens.accessToken}`) | 692 | this.headers.set('Authorization', `${this.userTokens.tokenType} ${this.userTokens.accessToken}`) |
695 | } | 693 | } |
696 | 694 | ||
695 | private removeTokensFromHeaders () { | ||
696 | this.headers.delete('Authorization') | ||
697 | } | ||
698 | |||
697 | private getResourceId () { | 699 | private getResourceId () { |
698 | const urlParts = window.location.pathname.split('/') | 700 | const urlParts = window.location.pathname.split('/') |
699 | return urlParts[ urlParts.length - 1 ] | 701 | return urlParts[ urlParts.length - 1 ] |