- 新增用户登录页面与API端点(/api/auth/login、/api/auth/logout、/api/auth/me) - 引入基于密码哈希的认证机制,支持默认管理员账号登录 - 创建访问控制模块(lib/access-control.ts),实现基于角色的权限验证 - 在现有API端点中集成权限检查,限制非管理员用户只能访问其所属项目数据 - 更新应用布局,要求用户登录后才能访问主界面 - 扩展用户表结构,增加用户名和密码哈希字段 - 调整项目、KR、目标、会议记录等资源的查询逻辑,根据用户权限过滤数据 - 改进管理页面(指标、模板)的删除操作,使用确认对话框替代直接确认 - 优化“我的KR”页面,为管理员提供全员KR概览视图 - 修复.gitignore文件中的拼写错误(.trae → .travis) - 添加数据库迁移脚本,支持任务直接关联项目、禁用RLS、添加求助字段等 - 配置Next.js以支持独立输出和子路径部署
129 lines
3.7 KiB
TypeScript
129 lines
3.7 KiB
TypeScript
'use client';
|
|
|
|
import { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react';
|
|
import type { User } from '@/types/database';
|
|
|
|
interface UserContextType {
|
|
currentUser: User | null;
|
|
users: User[];
|
|
isLoading: boolean;
|
|
isInitialized: boolean;
|
|
isAuthenticated: boolean;
|
|
setCurrentUserId: (id: string) => void;
|
|
logout: () => void;
|
|
refreshUsers: () => Promise<void>;
|
|
refreshMe: () => Promise<User | null>;
|
|
}
|
|
|
|
const UserContext = createContext<UserContextType | undefined>(undefined);
|
|
|
|
export function UserProvider({ children }: { children: ReactNode }) {
|
|
const [currentUser, setCurrentUser] = useState<User | null>(null);
|
|
const [users, setUsers] = useState<User[]>([]);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const [isInitialized, setIsInitialized] = useState(false);
|
|
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
|
|
const refreshUsers = useCallback(async () => {
|
|
try {
|
|
const res = await fetch('/api/users');
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
setUsers(data);
|
|
return data;
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to fetch users:', error);
|
|
}
|
|
return [];
|
|
}, []);
|
|
|
|
const refreshMe = useCallback(async () => {
|
|
try {
|
|
const meRes = await fetch('/api/auth/me', { cache: 'no-store' });
|
|
const meData = await meRes.json().catch(() => ({}));
|
|
const me = meData?.user as User | null | undefined;
|
|
|
|
if (me && me.id) {
|
|
setCurrentUser(me);
|
|
setIsAuthenticated(true);
|
|
localStorage.setItem('okrAuthUserId', me.id);
|
|
localStorage.setItem('currentUserId', me.id);
|
|
await refreshUsers();
|
|
return me;
|
|
}
|
|
|
|
localStorage.removeItem('okrAuthUserId');
|
|
localStorage.removeItem('currentUserId');
|
|
setCurrentUser(null);
|
|
setIsAuthenticated(false);
|
|
setUsers([]);
|
|
return null;
|
|
} catch (error) {
|
|
console.error('Failed to refresh auth:', error);
|
|
localStorage.removeItem('okrAuthUserId');
|
|
localStorage.removeItem('currentUserId');
|
|
setCurrentUser(null);
|
|
setIsAuthenticated(false);
|
|
setUsers([]);
|
|
return null;
|
|
}
|
|
}, [refreshUsers]);
|
|
|
|
useEffect(() => {
|
|
async function init() {
|
|
setIsLoading(true);
|
|
try {
|
|
await refreshMe();
|
|
} catch (error) {
|
|
console.error('Failed to initialize auth:', error);
|
|
localStorage.removeItem('okrAuthUserId');
|
|
localStorage.removeItem('currentUserId');
|
|
setCurrentUser(null);
|
|
setIsAuthenticated(false);
|
|
setUsers([]);
|
|
} finally {
|
|
setIsInitialized(true);
|
|
setIsLoading(false);
|
|
}
|
|
}
|
|
|
|
init();
|
|
}, [refreshMe]);
|
|
|
|
const setCurrentUserId = useCallback((id: string) => {
|
|
const authUserId = localStorage.getItem('okrAuthUserId');
|
|
if (!authUserId) return;
|
|
if (authUserId !== id) return;
|
|
const user = users.find(u => u.id === id);
|
|
if (user) {
|
|
setCurrentUser(user);
|
|
localStorage.setItem('currentUserId', id);
|
|
setIsAuthenticated(true);
|
|
}
|
|
}, [users]);
|
|
|
|
const logout = useCallback(() => {
|
|
localStorage.removeItem('okrAuthUserId');
|
|
localStorage.removeItem('currentUserId');
|
|
setCurrentUser(null);
|
|
setIsAuthenticated(false);
|
|
setUsers([]);
|
|
fetch('/api/auth/logout', { method: 'POST' }).catch(() => {});
|
|
}, []);
|
|
|
|
return (
|
|
<UserContext.Provider value={{ currentUser, users, isLoading, isInitialized, isAuthenticated, setCurrentUserId, logout, refreshUsers, refreshMe }}>
|
|
{children}
|
|
</UserContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useUser() {
|
|
const context = useContext(UserContext);
|
|
if (context === undefined) {
|
|
throw new Error('useUser must be used within a UserProvider');
|
|
}
|
|
return context;
|
|
}
|