feat: login

This commit is contained in:
Rewrite0
2023-05-25 00:18:10 +08:00
parent 9b72b45be2
commit 731470934d
9 changed files with 31 additions and 17 deletions

View File

@@ -3,18 +3,16 @@ import type { Auth, Logout, Update } from '#/auth';
export const apiAuth = {
async login(username: string, password: string) {
const { data } = await axios.post<Auth>(
'api/v1/auth/login',
{
username,
password,
const formData = new URLSearchParams({
username,
password,
});
const { data } = await axios.post<Auth>('api/v1/auth/login', formData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);
});
return data;
},

View File

@@ -12,7 +12,7 @@ export const useAuth = createSharedComposable(() => {
const login = async () => {
const res = await apiAuth.login(user.username, user.password);
console.log(res);
token.value = res.access_token;
};
const logout = async () => {

View File

@@ -1,4 +1,9 @@
<script lang="ts" setup></script>
<script lang="ts" setup>
definePage({
name: 'Index',
redirect: '/bangumi',
});
</script>
<template>
<div layout-container>

View File

@@ -2,7 +2,7 @@
const { data } = storeToRefs(useBangumiStore());
const { getAll } = useBangumiStore();
onBeforeMount(() => getAll());
getAll();
definePage({
name: 'Bangumi List',

View File

@@ -1,7 +1,7 @@
<script lang="ts" setup>
const { getConfig, setConfig } = useConfigStore();
onBeforeMount(() => getConfig());
getConfig();
definePage({
name: 'Config',

View File

@@ -25,6 +25,7 @@ definePage({
type="text"
placeholder="password"
ab-input
@keyup.enter="login"
/>
</ab-label>

View File

@@ -39,7 +39,7 @@ import type {
declare module 'vue-router/auto/routes' {
export interface RouteNamedMap {
'/': RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>>,
'Index': RouteRecordInfo<'Index', '/', Record<never, never>, Record<never, never>>,
'Bangumi List': RouteRecordInfo<'Bangumi List', '/bangumi', Record<never, never>, Record<never, never>>,
'Calendar': RouteRecordInfo<'Calendar', '/calendar', Record<never, never>, Record<never, never>>,
'Config': RouteRecordInfo<'Config', '/config', Record<never, never>, Record<never, never>>,

View File

@@ -9,6 +9,16 @@ router.beforeEach((to) => {
if (!isLogin.value && to.path !== '/login') {
return { name: 'Login' };
}
if (isLogin.value && to.path === '/login') {
return { name: 'Index' };
}
watchOnce(isLogin, (val) => {
if (to.path === '/login' && val) {
router.replace({ name: 'Index' });
}
});
});
export { router };

View File

@@ -28,7 +28,7 @@ const items = [
id: 1,
icon: Home,
label: 'HomePage',
path: '/',
path: '/bangumi',
},
{
id: 2,