mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 19:59:50 +08:00
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { CONVERSATION_ID_INFO } from '../base/chat/constants'
|
|
import { fetchAccessToken } from '@/service/share'
|
|
|
|
export const checkOrSetAccessToken = async () => {
|
|
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) {
|
|
|
|
}
|
|
if (!accessTokenJson[sharedToken]) {
|
|
const res = await fetchAccessToken(sharedToken)
|
|
accessTokenJson[sharedToken] = res.access_token
|
|
localStorage.setItem('token', JSON.stringify(accessTokenJson))
|
|
}
|
|
}
|
|
|
|
export const setAccessToken = async (sharedToken: string, token: string) => {
|
|
const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
|
|
let accessTokenJson = { [sharedToken]: '' }
|
|
try {
|
|
accessTokenJson = JSON.parse(accessToken)
|
|
}
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
localStorage.removeItem(CONVERSATION_ID_INFO)
|
|
|
|
accessTokenJson[sharedToken] = token
|
|
localStorage.setItem('token', JSON.stringify(accessTokenJson))
|
|
}
|
|
|
|
export const removeAccessToken = () => {
|
|
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) {
|
|
|
|
}
|
|
|
|
localStorage.removeItem(CONVERSATION_ID_INFO)
|
|
|
|
delete accessTokenJson[sharedToken]
|
|
localStorage.setItem('token', JSON.stringify(accessTokenJson))
|
|
}
|