feat: 账号制管理员系统 — 默认管理员jinghui自动初始化, 管理后台账号登录, 用户列表/添加/删除/改密/升降角色/禁用启用, ADMIN_TOKEN保留为应急通道
This commit is contained in:
1 parent
0afbd42fdf
commit
aaa2eaa834
4 files changed
+397
-24
No files matched your search
@@ -6,6 +6,7 @@ const TOKEN_KEY = 'it-tools-auth';
|
||||
export interface AuthState {
|
||||
token: string;
|
||||
username: string;
|
||||
role?: string;
|
||||
}
|
||||
|
||||
function loadState(): AuthState | null {
|
||||
@@ -49,6 +50,7 @@ export async function apiFetch(path: string, init: RequestInit = {}): Promise<an
|
||||
export function useUserAuth() {
|
||||
const isLoggedIn = computed(() => state.value !== null);
|
||||
const username = computed(() => state.value?.username ?? '');
|
||||
const isAdmin = computed(() => state.value?.role === 'admin');
|
||||
|
||||
async function login(u: string, p: string, cfToken = '') {
|
||||
const { status, body } = await apiFetch('/user/login', {
|
||||
@@ -59,7 +61,7 @@ export function useUserAuth() {
|
||||
if (status !== 200) {
|
||||
throw new Error(body.message || '登录失败');
|
||||
}
|
||||
save({ token: body.data.token, username: body.data.username });
|
||||
save({ token: body.data.token, username: body.data.username, role: body.data.role });
|
||||
window.dispatchEvent(new CustomEvent('it-tools-auth-changed'));
|
||||
return body.data.username;
|
||||
}
|
||||
@@ -95,5 +97,5 @@ export function useUserAuth() {
|
||||
return status === 200 ? body.data : null;
|
||||
}
|
||||
|
||||
return { isLoggedIn, username, login, register, logout, cloudUpload };
|
||||
return { isLoggedIn, username, isAdmin, login, register, logout, cloudUpload };
|
||||
}
|
||||
Reference in new issue
Block a user