@@ -30,7 +30,7 @@ export interface AuthResult {
3030 errorCode ?: string ;
3131}
3232
33- const login = async ( email : string , password : string ) : Promise < AuthResult > => {
33+ const login = async ( email : string , password : string , isAdminPortal : boolean = false ) : Promise < AuthResult > => {
3434 try {
3535 // Validate inputs
3636 if ( ! validateEmail ( email ) ) {
@@ -58,12 +58,29 @@ const login = async (email: string, password: string): Promise<AuthResult> => {
5858 // Attempt backend login
5959 try {
6060 const loginRequest : LoginRequest = { email, password } ;
61- const { data } = await baseAPIClient . post < AuthResponse > ( '/auth/login' , loginRequest , {
62- withCredentials : true ,
63- } ) ;
61+ const headers : any = { withCredentials : true } ;
62+
63+ // Add admin portal header if this is an admin login
64+ if ( isAdminPortal ) {
65+ headers . headers = { 'X-Admin-Portal' : 'true' } ;
66+ }
67+
68+ const { data } = await baseAPIClient . post < AuthResponse > ( '/auth/login' , loginRequest , headers ) ;
6469 localStorage . setItem ( AUTHENTICATED_USER_KEY , JSON . stringify ( data ) ) ;
6570 return { success : true , user : { ...data . user , ...data } } ;
66- } catch {
71+ } catch ( error ) {
72+ // Handle admin privilege errors specifically
73+ if ( error && typeof error === 'object' && 'response' in error ) {
74+ const response = ( error as { response ?: { status ?: number ; data ?: { detail ?: string } } } ) . response ;
75+ if ( response ?. status === 403 && isAdminPortal ) {
76+ return {
77+ success : false ,
78+ error : 'Access denied. You do not have admin privileges. Please contact an administrator.' ,
79+ errorCode : 'auth/insufficient-privileges' ,
80+ } ;
81+ }
82+ }
83+
6784 // Backend login failure is not critical since Firebase auth succeeded
6885 return {
6986 success : true ,
@@ -192,22 +209,19 @@ export const register = async ({
192209 } else {
193210 console . warn ( '[REGISTER] Failed to send email verification after registration' ) ;
194211 }
212+
213+ // Return success with user info - don't try to login since email isn't verified yet
214+ return {
215+ success : true ,
216+ user : { email : user . email , uid : user . uid } as unknown as AuthenticatedUser ,
217+ } ;
195218 } catch ( firebaseError ) {
196219 console . error ( '[REGISTER] Firebase sign-in failed:' , firebaseError ) ;
197220 // Continue with registration even if Firebase sign-in fails
198221 // The user can still verify their email later
199- }
200-
201- // Try backend login but don't fail if it doesn't work
202- try {
203- const loginResult = await login ( email , password ) ;
204- return loginResult ;
205- } catch ( loginError ) {
206- console . warn ( '[REGISTER] Backend login failed, but registration was successful:' , loginError ) ;
207- // Return success even if backend login fails, since Firebase user was created
208222 return {
209223 success : true ,
210- user : { email, uid : auth . currentUser ?. uid || 'unknown' } as unknown as AuthenticatedUser ,
224+ user : { email, uid : 'unknown' } as unknown as AuthenticatedUser ,
211225 } ;
212226 }
213227 } catch ( error ) {
@@ -223,6 +237,10 @@ export const register = async ({
223237 } else if ( response ?. status === 400 ) {
224238 const detail = response ?. data ?. detail || 'Invalid registration data' ;
225239 return { success : false , error : detail } ;
240+ } else if ( response ?. status === 403 ) {
241+ // Handle admin privilege errors
242+ const detail = response ?. data ?. detail || 'Access denied' ;
243+ return { success : false , error : detail } ;
226244 }
227245 }
228246
0 commit comments