feat: implement user and admin login flow
This commit is contained in:
9
pages/admin.vue
Normal file
9
pages/admin.vue
Normal file
@ -0,0 +1,9 @@
|
||||
<template>
|
||||
<div class="p-8">
|
||||
<h1 class="text-2xl font-bold">后台管理</h1>
|
||||
<p class="mt-4">欢迎, 管理员!</p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
</script>
|
@ -113,8 +113,33 @@ function showLogin() {
|
||||
}
|
||||
|
||||
async function handleLogin() {
|
||||
// TODO: Implement login logic
|
||||
console.log('Logging in with role:', role.value, 'and credentials:', loginForm);
|
||||
try {
|
||||
if (role.value === 'user') {
|
||||
const response = await $fetch('/api/login/user', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
contact: loginForm.contact,
|
||||
password: loginForm.password
|
||||
}
|
||||
});
|
||||
message.value = response.message;
|
||||
if (response.customerId) {
|
||||
localStorage.setItem('customerId', response.customerId);
|
||||
await navigateTo('/user');
|
||||
}
|
||||
} else { // admin
|
||||
const response = await $fetch('/api/login/admin', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
password: loginForm.password
|
||||
}
|
||||
});
|
||||
message.value = response.message;
|
||||
await navigateTo('/admin');
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.value = error.data?.message || '登录失败';
|
||||
}
|
||||
}
|
||||
|
||||
async function handleRegister() {
|
||||
|
9
pages/user.vue
Normal file
9
pages/user.vue
Normal file
@ -0,0 +1,9 @@
|
||||
<template>
|
||||
<div class="p-8">
|
||||
<h1 class="text-2xl font-bold">用户中心</h1>
|
||||
<p class="mt-4">欢迎回来!</p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
</script>
|
21
server/api/login/admin.post.ts
Normal file
21
server/api/login/admin.post.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { defineEventHandler, readBody, setResponseStatus } from 'h3';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event);
|
||||
const { password } = body;
|
||||
|
||||
if (!password) {
|
||||
setResponseStatus(event, 400);
|
||||
return { message: '请填写密码' };
|
||||
}
|
||||
|
||||
// TODO: Replace with a more secure admin authentication method
|
||||
if (password === 'adminpassword') {
|
||||
return {
|
||||
message: '管理员登录成功!',
|
||||
};
|
||||
} else {
|
||||
setResponseStatus(event, 401);
|
||||
return { message: '密码错误' };
|
||||
}
|
||||
});
|
22
server/api/login/user.post.ts
Normal file
22
server/api/login/user.post.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { defineEventHandler, readBody, setResponseStatus } from 'h3';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event);
|
||||
const { contact, password } = body;
|
||||
|
||||
if (!contact || !password) {
|
||||
setResponseStatus(event, 400);
|
||||
return { message: '请填写手机号和密码' };
|
||||
}
|
||||
|
||||
// TODO: Replace with database user lookup and password verification
|
||||
if (contact === '1234567890' && password === 'password') {
|
||||
return {
|
||||
message: '登录成功!',
|
||||
customerId: 'dummy-customer-id-123',
|
||||
};
|
||||
} else {
|
||||
setResponseStatus(event, 401);
|
||||
return { message: '手机号或密码错误' };
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user