feat: api 鉴权

This commit is contained in:
Rewrite0
2023-05-25 00:44:45 +08:00
parent 731470934d
commit e5d6a7942e
10 changed files with 21 additions and 14 deletions

View File

@@ -1,4 +1,3 @@
import axios from 'axios';
import type { BangumiItem } from '#/bangumi';
export const apiBangumi = {

View File

@@ -1,5 +1,3 @@
import axios from 'axios';
export const apiCheck = {
/**
* 检测下载器

View File

@@ -1,4 +1,3 @@
import axios from 'axios';
import type { Config } from '#/config';
export const apiConfig = {

View File

@@ -1,4 +1,3 @@
import axios from 'axios';
import type { BangumiItem } from '#/bangumi';
interface Status {

View File

@@ -1,5 +1,3 @@
import axios from 'axios';
export const apiLog = {
async getLog() {
const { data } = await axios.get('api/v1/log');

View File

@@ -1,5 +1,3 @@
import axios from 'axios';
interface Success {
status: 'ok';
}

View File

@@ -15,6 +15,7 @@ declare global {
const assert: typeof import('vitest')['assert']
const asyncComputed: typeof import('@vueuse/core')['asyncComputed']
const autoResetRef: typeof import('@vueuse/core')['autoResetRef']
const axios: typeof import('./utils/axios')['axios']
const beforeAll: typeof import('vitest')['beforeAll']
const beforeEach: typeof import('vitest')['beforeEach']
const chai: typeof import('vitest')['chai']

View File

@@ -1,18 +1,18 @@
import type { User } from '#/auth';
export const useAuth = createSharedComposable(() => {
const token = useLocalStorage('token', '');
const auth = useLocalStorage('auth', '');
const user = reactive<User>({
username: '',
password: '',
});
const isLogin = computed(() => token.value !== '');
const isLogin = computed(() => auth.value !== '');
const login = async () => {
const res = await apiAuth.login(user.username, user.password);
token.value = res.access_token;
auth.value = `${res.token_type} ${res.access_token}`;
};
const logout = async () => {
@@ -29,7 +29,7 @@ export const useAuth = createSharedComposable(() => {
};
return {
token,
auth,
user,
isLogin,

View File

@@ -12,7 +12,13 @@ definePage({
<template>
<div flex="~ wrap" gap-12px>
<ab-container title="Log" w-660px grow>
<div rounded-10px border="1px solid black" overflow-auto p-10px>
<div
rounded-10px
border="1px solid black"
overflow-auto
p-10px
max-h-60vh
>
<pre text-main>{{ log }}</pre>
</div>
</ab-container>

9
src/utils/axios.ts Normal file
View File

@@ -0,0 +1,9 @@
import Axios from 'axios';
const { auth } = useAuth();
export const axios = Axios.create({
headers: {
Authorization: auth.value,
},
});