Skip to content

Commit

Permalink
fix: login update token logic
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Dec 10, 2023
1 parent 121c913 commit e3904cf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/constants/keys.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const enum EmitKeyMap {
EditDataUpdate = 'editDataUpdate',
}

export const SESSION_WITH_LOGIN = 'session-with-login'
3 changes: 3 additions & 0 deletions src/router/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import QProgress from 'qier-progress'
import { removeToken, setToken } from 'utils/auth'
import { checkIsInit } from 'utils/is-init'

import { SESSION_WITH_LOGIN } from '~/constants/keys'
import { getTokenIsUpstream } from '~/stores/user'

import { configs } from '../configs'
Expand Down Expand Up @@ -52,6 +53,8 @@ router.beforeEach(async (to) => {
if (!ok) {
return `/login?from=${encodeURI(to.fullPath)}`
} else {
const sessionWithLogin = sessionStorage.getItem(SESSION_WITH_LOGIN)
if (sessionWithLogin) return
// login with token only
if (loginWithTokenOnce || getTokenIsUpstream()) {
return
Expand Down
33 changes: 16 additions & 17 deletions src/views/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { UserModel } from '../../models/user'

import ParallaxButton from '~/components/button/parallax-button.vue'
import { PassKeyOutlineIcon } from '~/components/icons'
import { SESSION_WITH_LOGIN } from '~/constants/keys'
import { AuthnUtils } from '~/utils/authn'

import Avatar from '../../components/avatar/index.vue'
Expand Down Expand Up @@ -45,6 +46,15 @@ export const LoginView = defineComponent({
})
})

const postSuccessfulLogin = (token: string) => {
updateToken(token)
router.push(
route.query.from ? decodeURI(route.query.from as string) : '/dashboard',
)
sessionStorage.setItem(SESSION_WITH_LOGIN, '1')
toast.success('欢迎回来')
}

const { data: settings } = useSWRV('allow-password', async () => {
return RESTManager.api.user('allow-login').get<{
password: boolean
Expand All @@ -60,14 +70,8 @@ export const LoginView = defineComponent({
message.error('验证失败')
}
const token = res.token!
updateToken(token)

router.push(
route.query.from
? decodeURI(route.query.from as string)
: '/dashboard',
)
toast.success('欢迎回来')

postSuccessfulLogin(token)
})
}
watchEffect(() => {
Expand Down Expand Up @@ -98,14 +102,10 @@ export const LoginView = defineComponent({
password: password.value,
},
})
updateToken(res.token)

router.push(
route.query.from
? decodeURI(route.query.from as string)
: '/dashboard',
)
toast.success('欢迎回来')

if (res.token) {
postSuccessfulLogin(res.token)
}
} catch (e) {
toast.error('登录失败')
}
Expand All @@ -116,7 +116,6 @@ export const LoginView = defineComponent({
typeof settings.value === 'undefined' ||
settings.value?.password === true

const loginWithPassKey = settings.value?.passkey
return (
<div class={styles['r']}>
<div class="wrapper">
Expand Down

0 comments on commit e3904cf

Please sign in to comment.