Skip to content

Commit

Permalink
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -18,6 +18,9 @@ import App from './App'
import './index.css'

import { router } from './router'
import { attachTokenFromQuery } from './utils'

attachTokenFromQuery()

const app = createApp(App)

22 changes: 22 additions & 0 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Cookies from 'js-cookie'

import { router } from '~/router'

export const TokenKey = 'mx-token'

/**
@@ -22,3 +24,23 @@ export function setToken(token: string) {
export function removeToken() {
return Cookies.remove(TokenKey)
}

export const attachTokenFromQuery = () => {
const token = new URLSearchParams(window.location.search).get('token')
if (token) {
setToken(token)
router.isReady().then(() => {
const parsedUrl = new URL(window.location.href)
parsedUrl.searchParams.delete('token')
const query = {} as any
for (const [key, value] of parsedUrl.searchParams.entries()) {
query[key] = value
}

router.replace({
path: parsedUrl.pathname,
query,
})
})
}
}

0 comments on commit 791b63c

Please sign in to comment.