| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <template>
- <h1>{{ text }}</h1>
- </template>
- <script setup lang="ts">
- import { ref, onMounted } from 'vue'
- import { withBase } from 'vitepress'
- import { createDiscreteApi } from 'naive-ui'
- import { useCasdoor } from "casdoor-vue-sdk"
- import { getOriginalUrl } from '../../plugins/SSE/stateMap'
- import { setWalineUserInfoFromToken } from '../../plugins/waline'
- const text = ref("Callback")
- // const { message } = createDiscreteApi(['message'])
- // message.success('Login success')
- // message.error(`Login failed: ${err}`)
- onMounted(() => {
- const url = new URL(window.location.href)
- const param = url.searchParams
- const code = param.get('code')
- const state = param.get('state')
- console.log("code: ", code);
- console.log("state: ", state);
- const sdk = useCasdoor()
- if (!sdk) {
- return
- }
- // sdk.signin("https://raspberrypi.dev", withBase("/api/signin")).then((token => {
- sdk.signin("", withBase("/api/signin")).then((token => {
- // 登录成功
- console.log("Login successful.")
- console.log(token)
- // 尝试提取用户信息
- setWalineUserInfoFromToken(token.access_token)
- // 尝试跳转登录前页面
- const originalUrl = getOriginalUrl(state)
- if (originalUrl) {
- window.location.href = originalUrl
- return
- }
- // 跳转回主页
- window.location.href = withBase("/")
- })).catch(err => {
- console.log(err)
- // 登录失败,需要重新登录,跳转到登录页面
- const sdk = useCasdoor()
- let signinUrl = "/"
- if (sdk) {
- signinUrl = sdk.getSigninUrl()
- }
- window.location.href = signinUrl.replace(/^https?:\/\/[^/]+/, '')
- })
-
- })
- </script>
|