Fix: Super admin login API path
The login process for super admins was incorrectly using the regular user authentication path. This commit ensures super admins use the correct authentication path
This commit is contained in:
@@ -11,19 +11,38 @@ export interface AuthUser {
|
|||||||
email: string;
|
email: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
avatar?: string;
|
avatar?: string;
|
||||||
|
role?: string; // Added role property
|
||||||
}
|
}
|
||||||
|
|
||||||
export const authService = {
|
export const authService = {
|
||||||
async login({ email, password }: LoginCredentials): Promise<AuthUser> {
|
async login({ email, password }: LoginCredentials): Promise<AuthUser> {
|
||||||
try {
|
try {
|
||||||
const authData = await pb.collection('users').authWithPassword(email, password);
|
// First try to login as a regular admin user
|
||||||
|
try {
|
||||||
return {
|
console.log("Attempting to login as admin user");
|
||||||
id: authData.record.id,
|
const authData = await pb.collection('users').authWithPassword(email, password);
|
||||||
email: authData.record.email,
|
|
||||||
name: authData.record.full_name || authData.record.name,
|
return {
|
||||||
avatar: authData.record.avatar
|
id: authData.record.id,
|
||||||
};
|
email: authData.record.email,
|
||||||
|
name: authData.record.full_name || authData.record.name,
|
||||||
|
avatar: authData.record.avatar,
|
||||||
|
role: authData.record.role || "admin"
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Failed to login as admin, trying as superadmin", error);
|
||||||
|
|
||||||
|
// If regular user login fails, try superadmin
|
||||||
|
const authData = await pb.collection('_superusers').authWithPassword(email, password);
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: authData.record.id,
|
||||||
|
email: authData.record.email,
|
||||||
|
name: authData.record.full_name || authData.record.name,
|
||||||
|
avatar: authData.record.avatar,
|
||||||
|
role: "superadmin"
|
||||||
|
};
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Login failed:', error);
|
console.error('Login failed:', error);
|
||||||
throw new Error('Authentication failed. Please check your credentials.');
|
throw new Error('Authentication failed. Please check your credentials.');
|
||||||
@@ -46,13 +65,17 @@ export const authService = {
|
|||||||
// Log the full user data for debugging
|
// Log the full user data for debugging
|
||||||
console.log("Raw user data from authStore:", userData);
|
console.log("Raw user data from authStore:", userData);
|
||||||
|
|
||||||
|
// Determine if this is a superadmin by checking the collection name
|
||||||
|
const isSuperAdmin = userData.collectionName === '_superusers';
|
||||||
|
|
||||||
// The avatar will be kept as is - either the full path from Pocketbase
|
// The avatar will be kept as is - either the full path from Pocketbase
|
||||||
// or we'll handle display in the UI components
|
// or we'll handle display in the UI components
|
||||||
return {
|
return {
|
||||||
id: userData.id,
|
id: userData.id,
|
||||||
email: userData.email,
|
email: userData.email,
|
||||||
name: userData.full_name || userData.name,
|
name: userData.full_name || userData.name,
|
||||||
avatar: userData.avatar
|
avatar: userData.avatar,
|
||||||
|
role: isSuperAdmin ? "superadmin" : (userData.role || "admin")
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user