fix
This commit is contained in:
parent
b819337600
commit
ae8664db48
|
|
@ -31,6 +31,7 @@ export interface UserProfile {
|
|||
}
|
||||
|
||||
export interface AuthResponse<T> {
|
||||
// 代表命令行是否执行成功
|
||||
success: boolean;
|
||||
message: string;
|
||||
data?: AuthResponseData<T>;
|
||||
|
|
@ -39,6 +40,7 @@ export interface AuthResponse<T> {
|
|||
}
|
||||
|
||||
export interface AuthResponseData<T> {
|
||||
// 代表结果是否成功
|
||||
success: boolean;
|
||||
message: string;
|
||||
data: T;
|
||||
|
|
@ -81,45 +83,38 @@ class PythonCliAuth {
|
|||
}
|
||||
});
|
||||
|
||||
console.log({ response })
|
||||
|
||||
// 第一层:检查命令行是否执行成功
|
||||
if (!response.success) {
|
||||
throw new Error(response.error || response.message || 'Login failed');
|
||||
throw new Error(response.error || response.message || 'Command execution failed');
|
||||
}
|
||||
|
||||
// 解析输出中的用户信息和token
|
||||
if (response.success && response.output) {
|
||||
// 尝试从输出中解析JSON
|
||||
const jsonMatch = response.output.match(/\{[\s\S]*\}/);
|
||||
if (jsonMatch) {
|
||||
try {
|
||||
const authData = JSON.parse(jsonMatch[0]);
|
||||
if (authData.success && authData.data?.user && authData.data?.token) {
|
||||
this.token = authData.data.token;
|
||||
this.user = {
|
||||
id: authData.data.user.id,
|
||||
username: authData.data.user.username,
|
||||
displayName: authData.data.user.display_name,
|
||||
email: authData.data.user.email,
|
||||
avatarUrl: authData.data.user.avatar_url,
|
||||
createTime: authData.data.user.created_at,
|
||||
updateTime: authData.data.user.updated_at,
|
||||
lastLogin: authData.data.user.last_login,
|
||||
isActive: authData.data.user.is_active
|
||||
};
|
||||
const data = response.data;
|
||||
// 第二层:检查认证逻辑是否成功
|
||||
if (data && data.success) {
|
||||
const authData = data;
|
||||
if (authData.success && authData.data?.user && authData.data?.token) {
|
||||
this.token = authData.data.token;
|
||||
this.user = {
|
||||
id: authData.data.user.id,
|
||||
username: authData.data.user.username,
|
||||
displayName: authData.data.user.displayName,
|
||||
email: authData.data.user.email,
|
||||
avatarUrl: authData.data.user.avatarUrl,
|
||||
createTime: authData.data.user.createTime,
|
||||
updateTime: authData.data.user.updateTime,
|
||||
lastLogin: authData.data.user.lastLogin,
|
||||
isActive: authData.data.user.isActive
|
||||
};
|
||||
|
||||
// 保存到localStorage
|
||||
this.saveAuthToStorage();
|
||||
// 保存到localStorage
|
||||
this.saveAuthToStorage();
|
||||
|
||||
return this.user;
|
||||
}
|
||||
} catch (parseError) {
|
||||
console.error('Failed to parse auth response JSON:', parseError);
|
||||
}
|
||||
return this.user;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(response.error || response.message || 'Login failed');
|
||||
// 认证逻辑失败
|
||||
throw new Error(data?.message || 'Authentication failed');
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error);
|
||||
throw error;
|
||||
|
|
|
|||
Loading…
Reference in New Issue