|
1 | 1 | """
|
2 | 2 | ┌──────────────────────────────────────────────────────────────────────────────┐
|
3 | 3 | │ @author: Davidson Gomes │
|
4 |
| -│ @file: run_seeders.py │ |
| 4 | +│ @file: auth_routes.py │ |
5 | 5 | │ Developed by: Davidson Gomes │
|
6 | 6 | │ Creation date: May 13, 2025 │
|
7 | 7 | │ Contact: contato@evolution-api.com │
|
@@ -191,14 +191,36 @@ async def login_for_access_token(form_data: UserLogin, db: Session = Depends(get
|
191 | 191 | Raises:
|
192 | 192 | HTTPException: If credentials are invalid
|
193 | 193 | """
|
194 |
| - user = authenticate_user(db, form_data.email, form_data.password) |
| 194 | + user, reason = authenticate_user(db, form_data.email, form_data.password) |
195 | 195 | if not user:
|
196 |
| - logger.warning(f"Login attempt with invalid credentials: {form_data.email}") |
197 |
| - raise HTTPException( |
198 |
| - status_code=status.HTTP_401_UNAUTHORIZED, |
199 |
| - detail="Invalid email or password", |
200 |
| - headers={"WWW-Authenticate": "Bearer"}, |
201 |
| - ) |
| 196 | + if reason == "user_not_found" or reason == "invalid_password": |
| 197 | + logger.warning(f"Login attempt with invalid credentials: {form_data.email}") |
| 198 | + raise HTTPException( |
| 199 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 200 | + detail="Invalid email or password", |
| 201 | + headers={"WWW-Authenticate": "Bearer"}, |
| 202 | + ) |
| 203 | + elif reason == "email_not_verified": |
| 204 | + logger.warning(f"Login attempt with unverified email: {form_data.email}") |
| 205 | + raise HTTPException( |
| 206 | + status_code=status.HTTP_403_FORBIDDEN, |
| 207 | + detail="Email not verified", |
| 208 | + ) |
| 209 | + elif reason == "inactive_user": |
| 210 | + logger.warning(f"Login attempt with inactive user: {form_data.email}") |
| 211 | + raise HTTPException( |
| 212 | + status_code=status.HTTP_403_FORBIDDEN, |
| 213 | + detail="User account is inactive", |
| 214 | + ) |
| 215 | + else: |
| 216 | + logger.warning( |
| 217 | + f"Login attempt failed for {form_data.email} (reason: {reason})" |
| 218 | + ) |
| 219 | + raise HTTPException( |
| 220 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 221 | + detail="Invalid email or password", |
| 222 | + headers={"WWW-Authenticate": "Bearer"}, |
| 223 | + ) |
202 | 224 |
|
203 | 225 | access_token = create_access_token(user)
|
204 | 226 | logger.info(f"Login successful for user: {user.email}")
|
|
0 commit comments