Skip to content

Commit ae392b8

Browse files
committed
Fixed some authentication issues
1 parent 4436482 commit ae392b8

File tree

5 files changed

+50
-310
lines changed

5 files changed

+50
-310
lines changed

backend/app/routes/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# TODO: ADD RATE LIMITING
1818
@router.post("/register", response_model=UserCreateResponse)
1919
async def register_user(user: UserCreateRequest, user_service: UserService = Depends(get_user_service)):
20-
allowed_Admins = ["umair.hundekar@uwblueprint.org", "umairmhundekar@gmail.com"]
20+
allowed_Admins = ["umair.hkar@gmail.com", "umairmhundekar@gmail.com"]
2121
if user.role == UserRole.ADMIN:
2222
if user.email not in allowed_Admins:
2323
raise HTTPException(status_code=403, detail="Access denied. Admin privileges required for admin portal")

frontend/src/APIClients/authAPIClient.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

frontend/src/pages/admin-login.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export default function AdminLogin() {
163163
marginTop: 6,
164164
cursor: 'pointer',
165165
}}
166-
onClick={() => router.push('/admin-reset-password')}
166+
onClick={() => router.push('/reset-password')}
167167
>
168168
Forgot Password?
169169
</span>

frontend/src/pages/admin-signup.tsx

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,23 @@ export default function AdminLoginPage() {
3838
signupMethod: SignUpMethod.PASSWORD,
3939
};
4040
const result = await register(userData);
41-
console.log('Admin registration success:', result);
42-
router.push(`/admin-verify?email=${encodeURIComponent(email)}&role=admin`);
43-
} catch (err: unknown) {
44-
console.error('Admin registration error:', err);
45-
if (
46-
err &&
47-
typeof err === 'object' &&
48-
'response' in err &&
49-
err.response &&
50-
typeof err.response === 'object' &&
51-
'data' in err.response &&
52-
err.response.data &&
53-
typeof err.response.data === 'object' &&
54-
'detail' in err.response.data
55-
) {
56-
setError((err.response.data as { detail: string }).detail || 'Registration failed');
41+
console.log("?", result)
42+
// Check if it's an admin privilege error
43+
if (!result.success && result.error && result.error.includes('Admin privileges required')) {
44+
setError('Access denied. Admin registration is restricted. Please contact an administrator.');
45+
return;
46+
}
47+
48+
// If successful (even if success is false, check if we got a user)
49+
if (result.user || result.success) {
50+
console.log('Admin registration success:', result);
51+
router.push(`/admin-verify?email=${encodeURIComponent(email)}&role=admin`);
5752
} else {
58-
setError('Registration failed');
53+
setError(result.error || 'Registration failed');
5954
}
55+
} catch (err: unknown) {
56+
console.error('Admin registration error:', err);
57+
setError('Registration failed');
6058
}
6159
};
6260

0 commit comments

Comments
 (0)