mirror of https://github.com/Chocobozzz/PeerTube
Continue without token on error in embed
parent
41fafa9ef9
commit
207612dff8
|
@ -74,7 +74,7 @@ export class PeerTubeEmbed {
|
||||||
return window.location.origin + '/api/v1/videos/' + id
|
return window.location.origin + '/api/v1/videos/' + id
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshFetch (url: string, options?: Object) {
|
refreshFetch (url: string, options?: RequestInit) {
|
||||||
return fetch(url, options)
|
return fetch(url, options)
|
||||||
.then((res: Response) => {
|
.then((res: Response) => {
|
||||||
if (res.status !== 401) return res
|
if (res.status !== 401) return res
|
||||||
|
@ -85,8 +85,10 @@ export class PeerTubeEmbed {
|
||||||
const refreshingTokenPromise = new Promise((resolve, reject) => {
|
const refreshingTokenPromise = new Promise((resolve, reject) => {
|
||||||
const clientId: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID)
|
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 clientSecret: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET)
|
||||||
|
|
||||||
const headers = new Headers()
|
const headers = new Headers()
|
||||||
headers.set('Content-Type', 'application/x-www-form-urlencoded')
|
headers.set('Content-Type', 'application/x-www-form-urlencoded')
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
refresh_token: this.userTokens.refreshToken,
|
refresh_token: this.userTokens.refreshToken,
|
||||||
client_id: clientId,
|
client_id: clientId,
|
||||||
|
@ -99,8 +101,7 @@ export class PeerTubeEmbed {
|
||||||
headers,
|
headers,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: objectToUrlEncoded(data)
|
body: objectToUrlEncoded(data)
|
||||||
})
|
}).then(res => res.json())
|
||||||
.then(res => res.json())
|
|
||||||
.then((obj: UserRefreshToken) => {
|
.then((obj: UserRefreshToken) => {
|
||||||
this.userTokens.accessToken = obj.access_token
|
this.userTokens.accessToken = obj.access_token
|
||||||
this.userTokens.refreshToken = obj.refresh_token
|
this.userTokens.refreshToken = obj.refresh_token
|
||||||
|
@ -116,10 +117,7 @@ export class PeerTubeEmbed {
|
||||||
})
|
})
|
||||||
|
|
||||||
return refreshingTokenPromise
|
return refreshingTokenPromise
|
||||||
.catch(() => {
|
.catch(() => this.removeTokensFromHeaders())
|
||||||
// If refreshing fails, continue with original error
|
|
||||||
throw error
|
|
||||||
})
|
|
||||||
.then(() => fetch(url, {
|
.then(() => fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
headers: this.headers
|
headers: this.headers
|
||||||
|
@ -694,6 +692,10 @@ export class PeerTubeEmbed {
|
||||||
this.headers.set('Authorization', `${this.userTokens.tokenType} ${this.userTokens.accessToken}`)
|
this.headers.set('Authorization', `${this.userTokens.tokenType} ${this.userTokens.accessToken}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private removeTokensFromHeaders () {
|
||||||
|
this.headers.delete('Authorization')
|
||||||
|
}
|
||||||
|
|
||||||
private getResourceId () {
|
private getResourceId () {
|
||||||
const urlParts = window.location.pathname.split('/')
|
const urlParts = window.location.pathname.split('/')
|
||||||
return urlParts[ urlParts.length - 1 ]
|
return urlParts[ urlParts.length - 1 ]
|
||||||
|
|
Loading…
Reference in New Issue