From 207612dff83401a9642f9cb0a63a5a6efcd5f590 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 10 Aug 2020 14:58:32 +0200 Subject: Continue without token on error in embed --- client/src/standalone/videos/embed.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'client/src/standalone/videos') 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 { return window.location.origin + '/api/v1/videos/' + id } - refreshFetch (url: string, options?: Object) { + refreshFetch (url: string, options?: RequestInit) { return fetch(url, options) .then((res: Response) => { if (res.status !== 401) return res @@ -85,8 +85,10 @@ export class PeerTubeEmbed { const refreshingTokenPromise = new Promise((resolve, reject) => { const clientId: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) const clientSecret: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET) + const headers = new Headers() headers.set('Content-Type', 'application/x-www-form-urlencoded') + const data = { refresh_token: this.userTokens.refreshToken, client_id: clientId, @@ -99,8 +101,7 @@ export class PeerTubeEmbed { headers, method: 'POST', body: objectToUrlEncoded(data) - }) - .then(res => res.json()) + }).then(res => res.json()) .then((obj: UserRefreshToken) => { this.userTokens.accessToken = obj.access_token this.userTokens.refreshToken = obj.refresh_token @@ -116,10 +117,7 @@ export class PeerTubeEmbed { }) return refreshingTokenPromise - .catch(() => { - // If refreshing fails, continue with original error - throw error - }) + .catch(() => this.removeTokensFromHeaders()) .then(() => fetch(url, { ...options, headers: this.headers @@ -694,6 +692,10 @@ export class PeerTubeEmbed { this.headers.set('Authorization', `${this.userTokens.tokenType} ${this.userTokens.accessToken}`) } + private removeTokensFromHeaders () { + this.headers.delete('Authorization') + } + private getResourceId () { const urlParts = window.location.pathname.split('/') return urlParts[ urlParts.length - 1 ] -- cgit v1.2.3