fix auth login logic

This commit is contained in:
2024-03-12 17:58:12 +08:00
parent 5bb583dfe9
commit 31d656b6a1
3 changed files with 19 additions and 14 deletions

View File

@@ -17,10 +17,10 @@ export const useAuthStore = defineStore('BiliAuth', () => {
token: string token: string
}[] }[]
>('Bili.Auth.Tokens', []) >('Bili.Auth.Tokens', [])
const biliToken = useStorage<string>('Bili.Auth.Selected', null) const currentToken = useStorage<string>('Bili.Auth.Selected', null)
const isLoading = ref(false) const isLoading = ref(false)
const isAuthed = computed(() => biliToken.value != null && biliToken.value.length > 0) const isAuthed = computed(() => currentToken.value != null && currentToken.value.length > 0)
async function setCurrentAuth(token: string) { async function setCurrentAuth(token: string) {
if (!token) { if (!token) {
@@ -28,7 +28,7 @@ export const useAuthStore = defineStore('BiliAuth', () => {
return return
} }
biliAuth.value = {} as BiliAuthModel biliAuth.value = {} as BiliAuthModel
biliToken.value = token currentToken.value = token
await getAuthInfo() await getAuthInfo()
} }
@@ -44,7 +44,7 @@ export const useAuthStore = defineStore('BiliAuth', () => {
if (index >= 0) { if (index >= 0) {
biliTokens.value[index] = { biliTokens.value[index] = {
id: biliAuth.value.id, id: biliAuth.value.id,
token: biliToken.value, token: currentToken.value,
name: biliAuth.value.name, name: biliAuth.value.name,
uId: biliAuth.value.userId, uId: biliAuth.value.userId,
} }
@@ -52,7 +52,7 @@ export const useAuthStore = defineStore('BiliAuth', () => {
} else { } else {
biliTokens.value.push({ biliTokens.value.push({
id: biliAuth.value.id, id: biliAuth.value.id,
token: biliToken.value, token: currentToken.value,
name: biliAuth.value.name, name: biliAuth.value.name,
uId: biliAuth.value.userId, uId: biliAuth.value.userId,
}) })
@@ -75,14 +75,14 @@ export const useAuthStore = defineStore('BiliAuth', () => {
function QueryBiliAuthGetAPI<T>(url: string, params?: any, headers?: [string, string][]) { function QueryBiliAuthGetAPI<T>(url: string, params?: any, headers?: [string, string][]) {
headers ??= [] headers ??= []
if (headers.find((h) => h[0] == 'Bili-Auth') == null) { if (headers.find((h) => h[0] == 'Bili-Auth') == null) {
headers.push(['Bili-Auth', biliToken.value ?? '']) headers.push(['Bili-Auth', currentToken.value ?? ''])
} }
return QueryGetAPI<T>(url, params, headers) return QueryGetAPI<T>(url, params, headers)
} }
function QueryBiliAuthPostAPI<T>(url: string, body?: unknown, headers?: [string, string][]) { function QueryBiliAuthPostAPI<T>(url: string, body?: unknown, headers?: [string, string][]) {
headers ??= [] headers ??= []
if (headers.find((h) => h[0] == 'Bili-Auth') == null) { if (headers.find((h) => h[0] == 'Bili-Auth') == null) {
headers.push(['Bili-Auth', biliToken.value ?? '']) headers.push(['Bili-Auth', currentToken.value ?? ''])
} }
return QueryPostAPI<T>(url, body, headers) return QueryPostAPI<T>(url, body, headers)
} }
@@ -123,7 +123,7 @@ export const useAuthStore = defineStore('BiliAuth', () => {
return { return {
biliAuth, biliAuth,
biliToken, biliToken: currentToken,
biliTokens, biliTokens,
isLoading, isLoading,
isAuthed, isAuthed,

View File

@@ -24,6 +24,7 @@ import {
} from 'naive-ui' } from 'naive-ui'
import { computed, onMounted, ref } from 'vue' import { computed, onMounted, ref } from 'vue'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { useAuthStore } from '@/store/useAuthStore'
type AuthStartModel = { type AuthStartModel = {
code: string code: string
@@ -35,11 +36,12 @@ type AuthStartModel = {
const message = useMessage() const message = useMessage()
const guidKey = useStorage('Bili.Auth.Key', uuidv4()) const guidKey = useStorage('Bili.Auth.Key', uuidv4())
const biliToken = useStorage<string>('Bili.Auth.Token', null) const currentToken = useStorage<string>('Bili.Auth.Selected', null)
const useAuth = useAuthStore()
const startModel = ref<AuthStartModel>() const startModel = ref<AuthStartModel>()
const currentStep = ref(biliToken.value ? 2 : 0) const currentStep = ref(currentToken.value ? 2 : 0)
const isStart = computed(() => { const isStart = computed(() => {
return currentStep.value > 0 return currentStep.value > 0
@@ -76,7 +78,9 @@ async function checkStatus() {
clearInterval(timer.value) clearInterval(timer.value)
message.success('认证成功') message.success('认证成功')
biliToken.value = data.data as string currentToken.value = data.data as string
useAuth.getAuthInfo()
currentStep.value = 2 currentStep.value = 2
return true return true
@@ -106,7 +110,7 @@ function copyCode() {
} }
onMounted(async () => { onMounted(async () => {
if (!biliToken.value) { if (!currentToken.value) {
if (await checkStatus()) { if (await checkStatus()) {
currentStep.value = 1 currentStep.value = 1
timer.value = setInterval(checkStatus, 5000) timer.value = setInterval(checkStatus, 5000)
@@ -182,7 +186,7 @@ onMounted(async () => {
<NText> 你的登陆链接为: </NText> <NText> 你的登陆链接为: </NText>
<NInputGroup> <NInputGroup>
<NInput <NInput
:value="`https://vtsuru.live/bili-user?auth=${biliToken}`" :value="`https://vtsuru.live/bili-user?auth=${currentToken}`"
type="textarea" type="textarea"
:allow-input="() => false" :allow-input="() => false"
/> />
@@ -195,7 +199,7 @@ onMounted(async () => {
() => { () => {
currentStep = 0 currentStep = 0
//@ts-ignore //@ts-ignore
biliToken = null currentToken = null
guidKey = uuidv4() guidKey = uuidv4()
} }
" "

View File

@@ -110,6 +110,7 @@ onMounted(async () => {
const route = useRoute() const route = useRoute()
if (route.query.auth) { if (route.query.auth) {
useAuth.biliToken = route.query.auth as string useAuth.biliToken = route.query.auth as string
console.log(route.query.auth)
} }
if (biliAuth.value?.id < 0) { if (biliAuth.value?.id < 0) {
isLoading.value = true isLoading.value = true