Files
knowai/auth/types.d.ts
tobegold574 6a81b7bb13
Some checks reported errors
continuous-integration/drone/push Build was killed
feat(image): 新建 knowai-core:1.0.0 镜像并完成推送
- 搭建 api、auth、utils 等逻辑模块
- 通过 tsc、eslint、vitest 测试验证

BREAKING CHANGE: 新镜像分支
2025-11-10 20:20:25 +08:00

86 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 认证模块类型定义
*/
import type { AxiosRequestConfig } from 'axios';
import type { User } from '@/types';
import type {
LoginRequest,
LoginResponse,
RegisterRequest,
RegisterResponse
} from '@/types/user';
// 事件类型定义
export type AuthEventType = 'login' | 'logout' | 'register' | 'session_expired' | 'session_authenticated' | 'session_logout';
// 事件监听器类型
export type AuthEventListener = (...args: unknown[]) => void;
// 存储适配器接口 - 仅用于非敏感数据存储不涉及session管理
export interface StorageAdapter {
/**
* 获取存储项
* @param key 存储键
* @returns 存储值或null
*/
getItem(key: string): string | null;
/**
* 设置存储项
* @param key 存储键
* @param value 存储值
*/
setItem(key: string, value: string): void;
/**
* 删除存储项
* @param key 存储键
*/
removeItem(key: string): void;
/**
* 清空所有存储项
*/
clear(): void;
/**
* 获取所有存储键
* @returns 存储键数组
*/
keys(): string[];
}
// 认证服务接口
export interface AuthService {
login(credentials: LoginRequest): Promise<LoginResponse>;
register(userData: RegisterRequest): Promise<RegisterResponse>;
logout(): Promise<void>;
getCurrentUser(): Promise<User>;
isAuthenticated(): Promise<boolean>; // 改为异步通过getUser验证
// 事件管理方法
on(event: AuthEventType, listener: AuthEventListener): void;
off(event: AuthEventType, listener: AuthEventListener): void;
// 会话管理方法
clearCache(): void;
}
// 会话管理器接口 - 仅作为状态查询器不主动管理session
export interface SessionManager {
// 通过getUser验证用户认证状态而不是直接访问cookie
isAuthenticated(): Promise<boolean>;
// 获取当前用户信息,幂等操作
getUserInfo(): Promise<User | null>;
// 清除本地缓存不影响服务器端session
clearCache(): void;
}
// 扩展 Axios 请求配置以支持认证选项
declare module 'axios' {
interface AxiosRequestConfig {
skipAuth?: boolean;
}
}