Fix invalid refresh token in embed
This commit is contained in:
parent
b4c3c51dc8
commit
496d784d05
|
@ -79,9 +79,6 @@ export class PeerTubeEmbed {
|
||||||
.then((res: Response) => {
|
.then((res: Response) => {
|
||||||
if (res.status !== 401) return res
|
if (res.status !== 401) return res
|
||||||
|
|
||||||
// 401 unauthorized is not catch-ed, but then-ed
|
|
||||||
const error = res
|
|
||||||
|
|
||||||
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)
|
||||||
|
@ -101,24 +98,37 @@ export class PeerTubeEmbed {
|
||||||
headers,
|
headers,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: objectToUrlEncoded(data)
|
body: objectToUrlEncoded(data)
|
||||||
}).then(res => res.json())
|
}).then(res => {
|
||||||
.then((obj: UserRefreshToken) => {
|
if (res.status === 401) return undefined
|
||||||
this.userTokens.accessToken = obj.access_token
|
|
||||||
this.userTokens.refreshToken = obj.refresh_token
|
|
||||||
this.userTokens.save()
|
|
||||||
|
|
||||||
this.setHeadersFromTokens()
|
return res.json()
|
||||||
|
}).then((obj: UserRefreshToken & { code: 'invalid_grant'}) => {
|
||||||
|
if (!obj || obj.code === 'invalid_grant') {
|
||||||
|
Tokens.flush()
|
||||||
|
this.removeTokensFromHeaders()
|
||||||
|
|
||||||
resolve()
|
return resolve()
|
||||||
})
|
}
|
||||||
|
|
||||||
|
this.userTokens.accessToken = obj.access_token
|
||||||
|
this.userTokens.refreshToken = obj.refresh_token
|
||||||
|
this.userTokens.save()
|
||||||
|
|
||||||
|
this.setHeadersFromTokens()
|
||||||
|
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
.catch((refreshTokenError: any) => {
|
.catch((refreshTokenError: any) => {
|
||||||
reject(refreshTokenError)
|
reject(refreshTokenError)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return refreshingTokenPromise
|
return refreshingTokenPromise
|
||||||
.catch(() => this.removeTokensFromHeaders())
|
.catch(() => {
|
||||||
.then(() => fetch(url, {
|
Tokens.flush()
|
||||||
|
|
||||||
|
this.removeTokensFromHeaders()
|
||||||
|
}).then(() => fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
headers: this.headers
|
headers: this.headers
|
||||||
}))
|
}))
|
||||||
|
|
Loading…
Reference in New Issue