Skip to content

Commit

Permalink
feat: auth server integration
Browse files Browse the repository at this point in the history
  • Loading branch information
JingBh committed Feb 14, 2024
1 parent 59344b9 commit c433de3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/lib/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import QuestionIndex from '../pages/QuestionIndex.vue'
import QuestionShow from '../pages/QuestionShow.vue'
import UserIndex from '../pages/UserIndex.vue'
import UserLogin from '../pages/UserLogin.vue'
import UserLoginCallback from '../pages/UserLoginCallback.vue'

declare module 'vue-router' {
interface RouteMeta {
Expand Down Expand Up @@ -81,6 +82,13 @@ const routes: RouteRecordRaw[] = [
title: '登录'
}
},
{
path: '/login/callback',
component: UserLoginCallback,
meta: {
title: '登录'
}
},
{
path: '/questions',
component: QuestionIndex,
Expand Down
17 changes: 16 additions & 1 deletion src/pages/UserIndex.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { useAxios } from '@vueuse/integrations/useAxios'
import { showToast } from 'vant'
import { useAxiosInstance } from '../lib/axios'
import { useStore } from '../lib/store'
Expand All @@ -14,6 +15,20 @@ const { data: count } = useAxios<{
}>('questions/count', useAxiosInstance(), {
immediate: true
})
const redirect = (): void => {
useAxiosInstance().get<{
url: string
}>('token/redirect').then(({ data }) => {
location.href = data.url
}).catch((e) => {
console.error(e)
showToast({
type: 'fail',
message: '无法获取登录链接'
})
})
}
</script>

<template>
Expand Down Expand Up @@ -50,7 +65,7 @@ const { data: count } = useAxios<{
title="登录"
center
is-link
to="/login"
@click="redirect"
/>
</van-cell-group>

Expand Down
7 changes: 2 additions & 5 deletions src/pages/UserLogin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useRouter } from 'vue-router'
import { useAxiosInstance } from '../lib/axios'
import { useStore } from '../lib/store'
import type { TokenResponse } from '../types/login.ts'
const store = useStore()
Expand All @@ -27,11 +28,7 @@ const submit = (): void => {
return
}
useAxiosInstance().post<{
access_token: string
expires_in: number
token_type: 'Bearer'
}>('token', form, {
useAxiosInstance().post<TokenResponse>('token', form, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
Expand Down
55 changes: 55 additions & 0 deletions src/pages/UserLoginCallback.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<script setup lang="ts">
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { showToast, closeToast } from 'vant'
import { useAxiosInstance } from '../lib/axios.ts'
import { useStore } from '../lib/store.ts'
import type { TokenResponse } from '../types/login.ts'
const router = useRouter()
const store = useStore()
onMounted(() => {
showToast({
type: 'loading',
message: '请稍候...',
forbidClick: true,
duration: 0
})
useAxiosInstance().post<TokenResponse>('token/exchange', null, {
withCredentials: true
}).then(({ data }) => {
closeToast()
showToast({
type: 'success',
message: '登录成功',
duration: 1000
})
store.token = data.access_token
if (data.expires_in) {
store.tokenExpiry = Date.now() + data.expires_in * 1000
}
const url = sessionStorage.getItem('intendedUrl') ?? '/user'
sessionStorage.removeItem('intendedUrl')
router.replace(url)
}).catch((e) => {
console.error(e)
closeToast()
showToast({
type: 'fail',
message: '登录失败',
duration: 1000
})
router.replace('/user')
})
})
</script>
5 changes: 5 additions & 0 deletions src/types/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface TokenResponse {
access_token: string
expires_in: number
token_type: string
}

0 comments on commit c433de3

Please sign in to comment.