feat: 实现用户登录认证与权限控制系统
- 新增用户登录页面与API端点(/api/auth/login、/api/auth/logout、/api/auth/me) - 引入基于密码哈希的认证机制,支持默认管理员账号登录 - 创建访问控制模块(lib/access-control.ts),实现基于角色的权限验证 - 在现有API端点中集成权限检查,限制非管理员用户只能访问其所属项目数据 - 更新应用布局,要求用户登录后才能访问主界面 - 扩展用户表结构,增加用户名和密码哈希字段 - 调整项目、KR、目标、会议记录等资源的查询逻辑,根据用户权限过滤数据 - 改进管理页面(指标、模板)的删除操作,使用确认对话框替代直接确认 - 优化“我的KR”页面,为管理员提供全员KR概览视图 - 修复.gitignore文件中的拼写错误(.trae → .travis) - 添加数据库迁移脚本,支持任务直接关联项目、禁用RLS、添加求助字段等 - 配置Next.js以支持独立输出和子路径部署
This commit is contained in:
117
app/api/auth/login/route.ts
Normal file
117
app/api/auth/login/route.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
export const dynamic = 'force-dynamic';
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { supabase } from '@/lib/supabase';
|
||||
import { hashPassword, verifyPassword } from '@/lib/password';
|
||||
|
||||
const DEFAULT_OWNER_USERNAME = 'baicaimiao';
|
||||
const DEFAULT_OWNER_PASSWORD = 'baicaimiao123';
|
||||
const DEFAULT_OWNER_NAME = '兜爸';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const body = await request.json().catch(() => ({}));
|
||||
const username = typeof body.username === 'string' ? body.username.trim() : '';
|
||||
const password = typeof body.password === 'string' ? body.password : '';
|
||||
|
||||
if (!username || !password) {
|
||||
return NextResponse.json({ error: '请输入账号和密码' }, { status: 400 });
|
||||
}
|
||||
|
||||
const { data: userByUsername, error: usernameError } = await supabase
|
||||
.from('users')
|
||||
.select('id,name,role,avatar_url,username,password_hash,created_at,updated_at')
|
||||
.eq('username', username)
|
||||
.maybeSingle();
|
||||
|
||||
if (usernameError) {
|
||||
return NextResponse.json({ error: usernameError.message }, { status: 500 });
|
||||
}
|
||||
|
||||
if (userByUsername?.password_hash) {
|
||||
const ok = verifyPassword(password, userByUsername.password_hash);
|
||||
if (!ok) {
|
||||
return NextResponse.json({ error: '账号或密码错误' }, { status: 401 });
|
||||
}
|
||||
const { password_hash, ...safeUser } = userByUsername as any;
|
||||
const res = NextResponse.json({ user: safeUser });
|
||||
res.cookies.set('okr_user_id', safeUser.id, {
|
||||
httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
path: '/',
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
if (username === DEFAULT_OWNER_USERNAME && password === DEFAULT_OWNER_PASSWORD) {
|
||||
const { data: ownerUser, error: ownerError } = await supabase
|
||||
.from('users')
|
||||
.select('id,name,role,avatar_url,username,password_hash,created_at,updated_at')
|
||||
.eq('role', 'owner')
|
||||
.order('created_at', { ascending: true })
|
||||
.limit(1)
|
||||
.maybeSingle();
|
||||
|
||||
if (ownerError) {
|
||||
return NextResponse.json({ error: ownerError.message }, { status: 500 });
|
||||
}
|
||||
|
||||
const passwordHash = hashPassword(DEFAULT_OWNER_PASSWORD);
|
||||
|
||||
if (ownerUser) {
|
||||
const { data: updated, error: updateError } = await supabase
|
||||
.from('users')
|
||||
.update({
|
||||
name: DEFAULT_OWNER_NAME,
|
||||
username: DEFAULT_OWNER_USERNAME,
|
||||
password_hash: passwordHash,
|
||||
role: 'owner',
|
||||
updated_at: new Date().toISOString(),
|
||||
})
|
||||
.eq('id', ownerUser.id)
|
||||
.select('id,name,role,avatar_url,username,created_at,updated_at')
|
||||
.single();
|
||||
|
||||
if (updateError) {
|
||||
return NextResponse.json({ error: updateError.message }, { status: 500 });
|
||||
}
|
||||
|
||||
const res = NextResponse.json({ user: updated });
|
||||
res.cookies.set('okr_user_id', updated.id, {
|
||||
httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
path: '/',
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
const { data: created, error: createError } = await supabase
|
||||
.from('users')
|
||||
.insert({
|
||||
name: DEFAULT_OWNER_NAME,
|
||||
role: 'owner',
|
||||
avatar_url: null,
|
||||
username: DEFAULT_OWNER_USERNAME,
|
||||
password_hash: passwordHash,
|
||||
})
|
||||
.select('id,name,role,avatar_url,username,created_at,updated_at')
|
||||
.single();
|
||||
|
||||
if (createError) {
|
||||
return NextResponse.json({ error: createError.message }, { status: 500 });
|
||||
}
|
||||
|
||||
const res = NextResponse.json({ user: created });
|
||||
res.cookies.set('okr_user_id', created.id, {
|
||||
httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
path: '/',
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: '账号或密码错误' }, { status: 401 });
|
||||
}
|
||||
Reference in New Issue
Block a user