完善登陆流程

This commit is contained in:
Rewrite0
2023-05-27 15:59:02 +08:00
parent 0e909cdf49
commit 2b02b62fbb
12 changed files with 228 additions and 26 deletions

View File

@@ -20,6 +20,7 @@
"@vueuse/core": "^8.9.4",
"axios": "^0.27.2",
"lodash": "^4.17.21",
"naive-ui": "^2.34.4",
"pinia": "^2.1.3",
"vue": "^3.3.4",
"vue-router": "^4.2.1"

147
pnpm-lock.yaml generated
View File

@@ -13,6 +13,9 @@ dependencies:
lodash:
specifier: ^4.17.21
version: 4.17.21
naive-ui:
specifier: ^2.34.4
version: 2.34.4(vue@3.3.4)
pinia:
specifier: ^2.1.3
version: 2.1.3(typescript@4.9.5)(vue@3.3.4)
@@ -1464,7 +1467,6 @@ packages:
engines: {node: '>=6.9.0'}
dependencies:
regenerator-runtime: 0.13.11
dev: true
/@babel/template@7.21.9:
resolution: {integrity: sha512-MK0X5k8NKOuWRamiEfc3KEJiHMTkGZNUjzMipqCGDDc6ijRl/B7RGSKVGncu4Ro/HdyzzY6cmoXuKI2Gffk7vQ==}
@@ -1508,11 +1510,31 @@ packages:
dev: true
optional: true
/@css-render/plugin-bem@0.15.12(css-render@0.15.12):
resolution: {integrity: sha512-Lq2jSOZn+wYQtsyaFj6QRz2EzAnd3iW5fZeHO1WSXQdVYwvwGX0ZiH3X2JQgtgYLT1yeGtrwrqJdNdMEUD2xTw==}
peerDependencies:
css-render: ~0.15.12
dependencies:
css-render: 0.15.12
dev: false
/@css-render/vue3-ssr@0.15.12(vue@3.3.4):
resolution: {integrity: sha512-AQLGhhaE0F+rwybRCkKUdzBdTEM/5PZBYy+fSYe1T9z9+yxMuV/k7ZRqa4M69X+EI1W8pa4kc9Iq2VjQkZx4rg==}
peerDependencies:
vue: ^3.0.11
dependencies:
vue: 3.3.4
dev: false
/@discoveryjs/json-ext@0.5.7:
resolution: {integrity: sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==}
engines: {node: '>=10.0.0'}
dev: true
/@emotion/hash@0.8.0:
resolution: {integrity: sha512-kBJtf7PH6aWwZ6fka3zQ0p6SBYzx4fl1LoZXE2RrnYST9Xljm7WfKJrU4g/Xr3Beg72MLrp1AWNUmuYJTL7Cow==}
dev: false
/@emotion/use-insertion-effect-with-fallbacks@1.0.1(react@18.2.0):
resolution: {integrity: sha512-jT/qyKZ9rzLErtrjGgdkMBn2OP8wl0G3sQlBb3YPryvKHsjvINUhVaPFfP+fpBcOkmrVOVEEHQFJ7nbj2TH2gw==}
peerDependencies:
@@ -1919,7 +1941,6 @@ packages:
/@juggle/resize-observer@3.4.0:
resolution: {integrity: sha512-dfLbk+PwWvFzSxwk3n5ySL0hfBog779o8h68wK/7/APo/7cgyWp5jcXockbxdk5kFRkbeXWm4Fbi9FrdN381sA==}
dev: true
/@mdx-js/react@2.3.0(react@18.2.0):
resolution: {integrity: sha512-zQH//gdOmuu7nt2oJR29vFhDv88oGPmVw6BggmrHeMI+xgEkp1B2dX9/bMBSYtK0dyLX/aOmesKS09g222K1/g==}
@@ -3002,9 +3023,18 @@ packages:
resolution: {integrity: sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==}
dev: true
/@types/katex@0.14.0:
resolution: {integrity: sha512-+2FW2CcT0K3P+JMR8YG846bmDwplKUTsWgT2ENwdQ1UdVfRk3GQrh6Mi4sTopy30gI8Uau5CEqHTDZ6YvWIUPA==}
dev: false
/@types/lodash-es@4.17.7:
resolution: {integrity: sha512-z0ptr6UI10VlU6l5MYhGwS4mC8DZyYer2mCoyysZtSF7p26zOX8UpbrV0YpNYLGS8K4PUFIyEr62IMFFjveSiQ==}
dependencies:
'@types/lodash': 4.14.194
dev: false
/@types/lodash@4.14.194:
resolution: {integrity: sha512-r22s9tAS7imvBt2lyHC9B8AGwWnXaYb1tY09oyLkXDs4vArpYJzw09nj8MLx5VfciBPGIb+ZwG0ssYnEPJxn/g==}
dev: true
/@types/mdast@3.0.11:
resolution: {integrity: sha512-Y/uImid8aAwrEA24/1tcRZwpxX3pIFTSilcNDKSPn+Y2iDywSEachzRuvgAYYLR3wpGXAsMbv5lvKLDZLeYPAw==}
@@ -3916,6 +3946,10 @@ packages:
resolution: {integrity: sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==}
dev: true
/async-validator@4.2.5:
resolution: {integrity: sha512-7HhHjtERjqlNbZtqNqy2rckN/SpOOlmDliet+lP7k+eKZEjPk3DgyeU9lIXLdeLz0uBbbVp+9Qdow9wJWgwwfg==}
dev: false
/async@3.2.4:
resolution: {integrity: sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ==}
dev: true
@@ -4476,6 +4510,13 @@ packages:
engines: {node: '>=8'}
dev: true
/css-render@0.15.12:
resolution: {integrity: sha512-eWzS66patiGkTTik+ipO9qNGZ+uNuGyTmnz6/+EJIiFg8+3yZRpnMwgFo8YdXhQRsiePzehnusrxVvugNjXzbw==}
dependencies:
'@emotion/hash': 0.8.0
csstype: 3.0.11
dev: false
/css-tree@2.3.1:
resolution: {integrity: sha512-6Fv1DV/TYw//QF5IzQdqsNDjx/wc8TrMBZsqjL9eW01tWb7R7k/mq+/VXfJCl7SoD5emsJop9cOByJZfs8hYIw==}
engines: {node: ^10 || ^12.20.0 || ^14.13.0 || >=15.0.0}
@@ -4490,9 +4531,28 @@ packages:
hasBin: true
dev: true
/csstype@3.0.11:
resolution: {integrity: sha512-sa6P2wJ+CAbgyy4KFssIb/JNMLxFvKF1pCYCSXS8ZMuqZnMsrxqI2E5sPyoTpxoPU/gVZMzr2zjOfg8GIZOMsw==}
dev: false
/csstype@3.1.2:
resolution: {integrity: sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==}
/date-fns-tz@1.3.8(date-fns@2.30.0):
resolution: {integrity: sha512-qwNXUFtMHTTU6CFSFjoJ80W8Fzzp24LntbjFFBgL/faqds4e5mo9mftoRLgr3Vi1trISsg4awSpYVsOQCRnapQ==}
peerDependencies:
date-fns: '>=2.0.0'
dependencies:
date-fns: 2.30.0
dev: false
/date-fns@2.30.0:
resolution: {integrity: sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==}
engines: {node: '>=0.11'}
dependencies:
'@babel/runtime': 7.21.5
dev: false
/date-time@3.1.0:
resolution: {integrity: sha512-uqCUKXE5q1PNBXjPqvwhwJf9SwMoAHBgWJ6DcrnS5o+W2JOiIILl0JEdVD8SGujrNS02GGxgwAg2PN2zONgtjg==}
engines: {node: '>=6'}
@@ -5379,6 +5439,10 @@ packages:
engines: {node: '>= 0.6'}
dev: true
/evtd@0.2.4:
resolution: {integrity: sha512-qaeGN5bx63s/AXgQo8gj6fBkxge+OoLddLniox5qtLAEY5HSnuSlISXVPxnSae1dWblvTh4/HoMIB+mbMsvZzw==}
dev: false
/execa@5.1.1:
resolution: {integrity: sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==}
engines: {node: '>=10'}
@@ -5960,6 +6024,11 @@ packages:
hasBin: true
dev: true
/highlight.js@11.8.0:
resolution: {integrity: sha512-MedQhoqVdr0U6SSnWPzfiadUcDHfN/Wzq25AkXiQv9oiOO/sG0S7XkvpFIqWBl9Yq1UYyYOOVORs5UW2XlPyzg==}
engines: {node: '>=12.0.0'}
dev: false
/hosted-git-info@2.8.9:
resolution: {integrity: sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==}
dev: true
@@ -6649,6 +6718,10 @@ packages:
p-locate: 5.0.0
dev: true
/lodash-es@4.17.21:
resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==}
dev: false
/lodash.debounce@4.0.8:
resolution: {integrity: sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==}
dev: true
@@ -6988,6 +7061,32 @@ packages:
resolution: {integrity: sha512-YVE1mIJ4VpUMqZObFndk9CJu6DBJR/GB13p3tXuNbwD4XExaI5EOuRl6BHeIDxIqXZVxSfAC+y6U1Z/IxCfKUg==}
dev: true
/naive-ui@2.34.4(vue@3.3.4):
resolution: {integrity: sha512-aPG8PDfhSzIzn/jSC9y3Jb3Pe2wHJ7F0cFV1EWlbImSrZECeUmoc+fIcOSWbizoztkKfaUAeKwYdMl09MKkj1g==}
peerDependencies:
vue: ^3.0.0
dependencies:
'@css-render/plugin-bem': 0.15.12(css-render@0.15.12)
'@css-render/vue3-ssr': 0.15.12(vue@3.3.4)
'@types/katex': 0.14.0
'@types/lodash': 4.14.194
'@types/lodash-es': 4.17.7
async-validator: 4.2.5
css-render: 0.15.12
date-fns: 2.30.0
date-fns-tz: 1.3.8(date-fns@2.30.0)
evtd: 0.2.4
highlight.js: 11.8.0
lodash: 4.17.21
lodash-es: 4.17.21
seemly: 0.3.6
treemate: 0.3.11
vdirs: 0.1.8(vue@3.3.4)
vooks: 0.2.12(vue@3.3.4)
vue: 3.3.4
vueuc: 0.4.51(vue@3.3.4)
dev: false
/nanoid@3.3.6:
resolution: {integrity: sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==}
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
@@ -7804,7 +7903,6 @@ packages:
/regenerator-runtime@0.13.11:
resolution: {integrity: sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==}
dev: true
/regenerator-transform@0.15.1:
resolution: {integrity: sha512-knzmNAcuyxV+gQCufkYcvOqX/qIIfHLv0u5x79kRxuGojfYVky1f15TzZEu2Avte8QGepvUNTnLskf8E6X6Vyg==}
@@ -7986,6 +8084,10 @@ packages:
resolution: {integrity: sha512-4AsO/FrViE/iDNEPaAQlb77tf0csuq27EsVpy6ett584EcRTp6pTDLoGWVxCD77y5iU5FauOvhsI4o1APwPoSQ==}
dev: true
/seemly@0.3.6:
resolution: {integrity: sha512-lEV5VB8BUKTo/AfktXJcy+JeXns26ylbMkIUco8CYREsQijuz4mrXres2Q+vMLdwkuLxJdIPQ8IlCIxLYm71Yw==}
dev: false
/semver@5.7.1:
resolution: {integrity: sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==}
hasBin: true
@@ -8472,6 +8574,10 @@ packages:
resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==}
dev: true
/treemate@0.3.11:
resolution: {integrity: sha512-M8RGFoKtZ8dF+iwJfAJTOH/SM4KluKOKRJpjCMhI8bG3qB74zrFoArKZ62ll0Fr3mqkMJiQOmWYkdYgDeITYQg==}
dev: false
/ts-dedent@2.2.0:
resolution: {integrity: sha512-q5W7tVM71e2xjHZTlgfTDoPF/SmqKG5hddq9SzR49CH2hayqRKJtQ4mtRlSxKaJlR/+9rEM+mnBHf7I2/BQcpQ==}
engines: {node: '>=6.10'}
@@ -8919,6 +9025,15 @@ packages:
engines: {node: '>= 0.8'}
dev: true
/vdirs@0.1.8(vue@3.3.4):
resolution: {integrity: sha512-H9V1zGRLQZg9b+GdMk8MXDN2Lva0zx72MPahDKc30v+DtwKjfyOSXWRIX4t2mhDubM1H09gPhWeth/BJWPHGUw==}
peerDependencies:
vue: ^3.0.11
dependencies:
evtd: 0.2.4
vue: 3.3.4
dev: false
/vite-node@0.30.1(@types/node@18.16.14)(sass@1.62.1):
resolution: {integrity: sha512-vTikpU/J7e6LU/8iM3dzBo8ZhEiKZEKRznEMm+mJh95XhWaPrJQraT/QsT2NWmuEf+zgAoMe64PKT7hfZ1Njmg==}
engines: {node: '>=v14.18.0'}
@@ -9045,6 +9160,15 @@ packages:
engines: {node: '>=0.10.0'}
dev: true
/vooks@0.2.12(vue@3.3.4):
resolution: {integrity: sha512-iox0I3RZzxtKlcgYaStQYKEzWWGAduMmq+jS7OrNdQo1FgGfPMubGL3uGHOU9n97NIvfFDBGnpSvkWyb/NSn/Q==}
peerDependencies:
vue: ^3.0.0
dependencies:
evtd: 0.2.4
vue: 3.3.4
dev: false
/vue-demi@0.14.5(vue@3.3.4):
resolution: {integrity: sha512-o9NUVpl/YlsGJ7t+xuqJKx8EBGf1quRhCiT6D/J0pfwmk9zUwYkC7yrF4SZCe6fETvSM3UNL2edcbYrSyc4QHA==}
engines: {node: '>=12'}
@@ -9139,6 +9263,21 @@ packages:
'@vue/server-renderer': 3.3.4(vue@3.3.4)
'@vue/shared': 3.3.4
/vueuc@0.4.51(vue@3.3.4):
resolution: {integrity: sha512-pLiMChM4f+W8czlIClGvGBYo656lc2Y0/mXFSCydcSmnCR1izlKPGMgiYBGjbY9FDkFG8a2HEVz7t0DNzBWbDw==}
peerDependencies:
vue: ^3.0.11
dependencies:
'@css-render/vue3-ssr': 0.15.12(vue@3.3.4)
'@juggle/resize-observer': 3.4.0
css-render: 0.15.12
evtd: 0.2.4
seemly: 0.3.6
vdirs: 0.1.8(vue@3.3.4)
vooks: 0.2.12(vue@3.3.4)
vue: 3.3.4
dev: false
/walker@1.0.8:
resolution: {integrity: sha512-ts/8E8l5b7kY0vlWLewOkDXMmPdLcVV4GmOQLyxuSswIJsweeFZtAsMF7k1Nszz+TYBQrlYRmzOnr398y1JemQ==}
dependencies:

View File

@@ -1,8 +1,12 @@
<script setup lang="ts"></script>
<script setup lang="ts">
import { NMessageProvider } from 'naive-ui';
</script>
<template>
<Suspense>
<RouterView></RouterView>
<n-message-provider>
<RouterView></RouterView>
</n-message-provider>
</Suspense>
</template>

View File

@@ -1,5 +1,4 @@
import axios from 'axios';
import type { Auth, Logout, Update } from '#/auth';
import type { LoginSuccess, Logout, Update } from '#/auth';
export const apiAuth = {
async login(username: string, password: string) {
@@ -8,17 +7,21 @@ export const apiAuth = {
password,
});
const { data } = await axios.post<Auth>('api/v1/auth/login', formData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
const { data } = await axios.post<LoginSuccess>(
'api/v1/auth/login',
formData,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);
return data;
},
async refresh() {
const { data } = await axios.get<Auth>('api/v1/auth/refresh_token');
const { data } = await axios.get<LoginSuccess>('api/v1/auth/refresh_token');
return data;
},

View File

@@ -205,6 +205,7 @@ declare global {
const useMediaQuery: typeof import('@vueuse/core')['useMediaQuery']
const useMemoize: typeof import('@vueuse/core')['useMemoize']
const useMemory: typeof import('@vueuse/core')['useMemory']
const useMessage: typeof import('./hooks/useMessage')['useMessage']
const useMounted: typeof import('@vueuse/core')['useMounted']
const useMouse: typeof import('@vueuse/core')['useMouse']
const useMouseInElement: typeof import('@vueuse/core')['useMouseInElement']

View File

@@ -1,7 +1,9 @@
import type { User } from '#/auth';
import type { ApiError } from '#/error';
export const useAuth = createSharedComposable(() => {
const auth = useSessionStorage('auth', '');
const message = useMessage();
const user = reactive<User>({
username: '',
@@ -11,16 +13,23 @@ export const useAuth = createSharedComposable(() => {
const isLogin = computed(() => auth.value !== '');
const login = async () => {
const res = await apiAuth.login(user.username, user.password);
auth.value = `${res.token_type} ${res.access_token}`;
try {
const res = await apiAuth.login(user.username, user.password);
auth.value = `${res.token_type} ${res.access_token}`;
user.username = '';
user.password = '';
} catch (err) {
const error = err as ApiError;
message.error(error.detail);
}
};
const logout = async () => {
apiAuth.logout();
};
const refresh = async () => {
const res = await apiAuth.refresh();
const refresh = () => {
apiAuth.refresh();
};
const update = async () => {

6
src/hooks/useMessage.ts Normal file
View File

@@ -0,0 +1,6 @@
import { createDiscreteApi } from 'naive-ui';
export const useMessage = createSharedComposable(() => {
const { message } = createDiscreteApi(['message']);
return message;
});

View File

@@ -22,7 +22,7 @@ definePage({
<ab-label label="Password">
<input
v-model="user.password"
type="text"
type="password"
placeholder="password"
ab-input
@keyup.enter="login"

View File

@@ -14,10 +14,13 @@ router.beforeEach((to) => {
return { name: 'Index' };
}
watchOnce(isLogin, (val) => {
watch(isLogin, (val) => {
if (to.path === '/login' && val) {
router.replace({ name: 'Index' });
}
if (to.path !== '/login' && !val) {
router.replace({ name: 'Login' });
}
});
});

View File

@@ -1,9 +1,35 @@
import Axios from 'axios';
import type { ApiError } from '#/error';
const { auth } = useAuth();
export const axios = Axios.create();
export const axios = Axios.create({
headers: {
Authorization: auth.value,
},
axios.interceptors.request.use((config) => {
const { auth } = useAuth();
if (auth.value !== '' && config.headers) {
config.headers.Authorization = auth.value;
}
return config;
});
axios.interceptors.response.use(
(res) => {
return res;
},
(err) => {
const status = err.response.status as ApiError['status'];
const detail = err.response.data.detail as ApiError['detail'];
const error = {
status,
detail,
};
if (error.detail === 'Not authenticated') {
const { auth } = useAuth();
auth.value = '';
}
return Promise.reject(error);
}
);

View File

@@ -1,4 +1,4 @@
export interface Auth {
export interface LoginSuccess {
access_token: string;
token_type: string;
expire: number;

10
types/error.ts Normal file
View File

@@ -0,0 +1,10 @@
export type AuthError = 'Not authenticated';
export type LoginError = 'Password error' | 'User not found';
export type ApiErrorMessage = AuthError | LoginError;
export type ApiError = {
status: 401 | 404 | 422;
detail: ApiErrorMessage;
};