This commit is contained in:
root 2025-07-12 19:02:15 +08:00
parent 04fb72c14c
commit a2986c631b
1 changed files with 29 additions and 38 deletions

View File

@ -30,26 +30,26 @@ export interface UserProfile {
isActive: boolean; isActive: boolean;
} }
export interface AuthResponse { export interface AuthResponse<T> {
success: boolean; success: boolean;
message: string; message: string;
data?: any; data?: AuthResponseData<T>;
output?: string; output?: string;
error?: string; error?: string;
} }
export interface PythonCliAuthResponse { export interface AuthResponseData<T> {
success: boolean; success: boolean;
message: string; message: string;
data?: { data: T;
user?: UserProfile;
token?: string;
expires_at?: string;
};
output?: string;
error?: string;
} }
export type PythonCliAuthResponse = AuthResponse<{
user?: UserProfile;
token?: string;
expires_at?: string;
}>;
class PythonCliAuth { class PythonCliAuth {
private token: string | null = null; private token: string | null = null;
private user: UserProfile | null = null; private user: UserProfile | null = null;
@ -139,40 +139,31 @@ class PythonCliAuth {
display_name: credentials.displayName, display_name: credentials.displayName,
} }
}); });
console.log({ response })
if (!response.success) { if (!response.success) {
throw new Error(response.error || response.message || 'Registration failed'); throw new Error(response.error || response.message || 'Registration failed');
} }
const data = response.data;
// 解析输出中的用户信息和token // 解析输出中的用户信息和token
if (response.success && response.output) { if (data && data.success) {
// 尝试从输出中解析JSON const authData = data;
const jsonMatch = response.output.match(/\{[\s\S]*\}/); if (authData.success && authData.data?.user && authData.data?.token) {
if (jsonMatch) { this.token = authData.data.token;
try { this.user = {
const authData = JSON.parse(jsonMatch[0]); id: authData.data.user.id,
if (authData.success && authData.data?.user && authData.data?.token) { username: authData.data.user.username,
this.token = authData.data.token; displayName: authData.data.user.displayName,
this.user = { email: authData.data.user.email,
id: authData.data.user.id, avatarUrl: authData.data.user.avatarUrl,
username: authData.data.user.username, createTime: authData.data.user.createTime,
displayName: authData.data.user.display_name, updateTime: authData.data.user.updateTime,
email: authData.data.user.email, lastLogin: authData.data.user.lastLogin,
avatarUrl: authData.data.user.avatar_url, isActive: authData.data.user.isActive
createTime: authData.data.user.created_at, };
updateTime: authData.data.user.updated_at,
lastLogin: authData.data.user.last_login,
isActive: authData.data.user.is_active
};
// 保存到localStorage // 保存到localStorage
this.saveAuthToStorage(); this.saveAuthToStorage();
return this.user; return this.user;
}
} catch (parseError) {
console.error('Failed to parse auth response JSON:', parseError);
}
} }
} }