Skip to content

fix: explicitly use new token when retrying ssePost after refresh #10864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 24 additions & 33 deletions web/service/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ function requiredWebSSOLogin() {
globalThis.location.href = `/webapp-signin?redirect_url=${globalThis.location.pathname}`
}

function getAccessToken(isPublicAPI?: boolean) {
if (isPublicAPI) {
const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0]
const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
let accessTokenJson = { [sharedToken]: '' }
try {
accessTokenJson = JSON.parse(accessToken)
}
catch (e) {

}
return accessTokenJson[sharedToken]
}
else {
return localStorage.getItem('console_token') || ''
}
}

export function format(text: string) {
let res = text.trim()
if (res.startsWith('\n'))
Expand Down Expand Up @@ -295,22 +313,8 @@ const baseFetch = <T>(
getAbortController(abortController)
options.signal = abortController.signal
}
if (isPublicAPI) {
const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0]
const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
let accessTokenJson = { [sharedToken]: '' }
try {
accessTokenJson = JSON.parse(accessToken)
}
catch (e) {

}
options.headers.set('Authorization', `Bearer ${accessTokenJson[sharedToken]}`)
}
else {
const accessToken = localStorage.getItem('console_token') || ''
options.headers.set('Authorization', `Bearer ${accessToken}`)
}
const accessToken = getAccessToken(isPublicAPI)
options.headers.set('Authorization', `Bearer ${accessToken}`)

if (deleteContentType) {
options.headers.delete('Content-Type')
Expand Down Expand Up @@ -403,23 +407,7 @@ const baseFetch = <T>(

export const upload = (options: any, isPublicAPI?: boolean, url?: string, searchParams?: string): Promise<any> => {
const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
let token = ''
if (isPublicAPI) {
const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0]
const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
let accessTokenJson = { [sharedToken]: '' }
try {
accessTokenJson = JSON.parse(accessToken)
}
catch (e) {

}
token = accessTokenJson[sharedToken]
}
else {
const accessToken = localStorage.getItem('console_token') || ''
token = accessToken
}
const token = getAccessToken(isPublicAPI)
const defaultOptions = {
method: 'POST',
url: (url ? `${urlPrefix}${url}` : `${urlPrefix}/files/upload`) + (searchParams || ''),
Expand Down Expand Up @@ -505,6 +493,9 @@ export const ssePost = (
if (body)
options.body = JSON.stringify(body)

const accessToken = getAccessToken(isPublicAPI)
options.headers.set('Authorization', `Bearer ${accessToken}`)

globalThis.fetch(urlWithPrefix, options as RequestInit)
.then((res) => {
if (!/^(2|3)\d{2}$/.test(String(res.status))) {
Expand Down