feat: implement user and admin login flow

This commit is contained in:
2025-06-18 16:03:28 +08:00
parent 0d98d4f0c2
commit 6652953b1a
5 changed files with 88 additions and 2 deletions

9
pages/admin.vue Normal file
View 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>

View File

@ -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
View 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>

View 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: '密码错误' };
}
});

View 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: '手机号或密码错误' };
}
});