mirror of
https://github.com/KwiTsukasa/kt-template-online-playground.git
synced 2026-05-27 16:45:45 +08:00
fix(playground): 接收后台授权回跳
This commit is contained in:
parent
0e271980c3
commit
2ba66ea8c5
@ -17,6 +17,9 @@ const ACCESS_CODES_KEY = 'kt-admin-access-codes'
|
|||||||
const USER_INFO_KEY = 'kt-admin-user-info'
|
const USER_INFO_KEY = 'kt-admin-user-info'
|
||||||
const LOGIN_REDIRECT_MARK_KEY = 'kt-admin-login-redirect-at'
|
const LOGIN_REDIRECT_MARK_KEY = 'kt-admin-login-redirect-at'
|
||||||
const LOGIN_REDIRECT_COOLDOWN = 10 * 1000
|
const LOGIN_REDIRECT_COOLDOWN = 10 * 1000
|
||||||
|
const AUTH_TRANSFER_TOKEN_KEY = 'ktAccessToken'
|
||||||
|
const AUTH_TRANSFER_CODES_KEY = 'ktAccessCodes'
|
||||||
|
const AUTH_TRANSFER_USER_KEY = 'ktUserInfo'
|
||||||
|
|
||||||
let refreshPromise: Promise<string | null> | null = null
|
let refreshPromise: Promise<string | null> | null = null
|
||||||
let redirectingToAdminLogin = false
|
let redirectingToAdminLogin = false
|
||||||
@ -77,6 +80,48 @@ export function persistAuthData({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseAuthTransferJson<T>(value: string | null) {
|
||||||
|
if (!value) return undefined
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(value) as T
|
||||||
|
} catch {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeAuthTransferParams(url: URL) {
|
||||||
|
url.searchParams.delete(AUTH_TRANSFER_TOKEN_KEY)
|
||||||
|
url.searchParams.delete(AUTH_TRANSFER_CODES_KEY)
|
||||||
|
url.searchParams.delete(AUTH_TRANSFER_USER_KEY)
|
||||||
|
window.history.replaceState(
|
||||||
|
window.history.state,
|
||||||
|
document.title,
|
||||||
|
`${url.pathname}${url.search}${url.hash}`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function consumeAdminAuthRedirect() {
|
||||||
|
const url = new URL(window.location.href)
|
||||||
|
const accessToken = url.searchParams.get(AUTH_TRANSFER_TOKEN_KEY)
|
||||||
|
|
||||||
|
if (!accessToken) return null
|
||||||
|
|
||||||
|
persistAuthData({
|
||||||
|
accessCodes: parseAuthTransferJson<string[]>(
|
||||||
|
url.searchParams.get(AUTH_TRANSFER_CODES_KEY),
|
||||||
|
),
|
||||||
|
accessToken,
|
||||||
|
userInfo: parseAuthTransferJson<unknown>(
|
||||||
|
url.searchParams.get(AUTH_TRANSFER_USER_KEY),
|
||||||
|
),
|
||||||
|
})
|
||||||
|
clearAdminLoginRedirectMark()
|
||||||
|
removeAuthTransferParams(url)
|
||||||
|
|
||||||
|
return accessToken
|
||||||
|
}
|
||||||
|
|
||||||
function buildAdminLoginUrl(redirect: string) {
|
function buildAdminLoginUrl(redirect: string) {
|
||||||
const loginUrl = new URL(getAdminLogin())
|
const loginUrl = new URL(getAdminLogin())
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import axios, { type AxiosRequestConfig } from 'axios'
|
|||||||
import {
|
import {
|
||||||
clearAdminLoginRedirectMark,
|
clearAdminLoginRedirectMark,
|
||||||
clearPersistedAuth,
|
clearPersistedAuth,
|
||||||
|
consumeAdminAuthRedirect,
|
||||||
getStoredAccessToken,
|
getStoredAccessToken,
|
||||||
redirectToAdminLogin,
|
redirectToAdminLogin,
|
||||||
refreshPersistedAuth,
|
refreshPersistedAuth,
|
||||||
@ -38,7 +39,7 @@ export function getApiUrl(url: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
request.interceptors.request.use(async (config) => {
|
request.interceptors.request.use(async (config) => {
|
||||||
const accessToken = getStoredAccessToken()
|
const accessToken = consumeAdminAuthRedirect() || getStoredAccessToken()
|
||||||
|
|
||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
config.headers.Authorization = `Bearer ${accessToken}`
|
config.headers.Authorization = `Bearer ${accessToken}`
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
/* eslint-disable @typescript-eslint/prefer-ts-expect-error */
|
/* eslint-disable @typescript-eslint/prefer-ts-expect-error */
|
||||||
import { createApp, h, ref, watchEffect } from 'vue'
|
import { createApp, h, ref, watchEffect } from 'vue'
|
||||||
import { type OutputModes, Repl, useStore, useVueImportMap } from '../src'
|
import { type OutputModes, Repl, useStore, useVueImportMap } from '../src'
|
||||||
|
import { consumeAdminAuthRedirect } from '../src/api/auth'
|
||||||
import PlaygroundHeader from '../src/PlaygroundHeader.vue'
|
import PlaygroundHeader from '../src/PlaygroundHeader.vue'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import MonacoEditor from '../src/editor/MonacoEditor.vue'
|
import MonacoEditor from '../src/editor/MonacoEditor.vue'
|
||||||
@ -9,6 +10,7 @@ import CodeMirrorEditor from '../src/editor/CodeMirrorEditor.vue'
|
|||||||
|
|
||||||
const window = globalThis.window as any
|
const window = globalThis.window as any
|
||||||
window.process = { env: {} }
|
window.process = { env: {} }
|
||||||
|
consumeAdminAuthRedirect()
|
||||||
|
|
||||||
const App = {
|
const App = {
|
||||||
setup() {
|
setup() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user