Skip to content

Commit

Permalink
Use the correct metric name when the user does not exist (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
meln1k authored Sep 4, 2024
1 parent b95c61c commit b2f00f0
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions fixbackend/auth/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from fastapi import APIRouter, Depends, HTTPException, Request, Response, status, Form
from fastapi_users.authentication import AuthenticationBackend, Strategy
from fastapi_users.exceptions import UserAlreadyExists, InvalidPasswordException
from fastapi_users.exceptions import UserAlreadyExists, InvalidPasswordException, UserNotExists
from fastapi_users.router import ErrorCode
from fastapi_users.router.oauth import generate_state_token
from httpx_oauth.clients.google import GoogleOAuth2
Expand Down Expand Up @@ -182,10 +182,14 @@ async def login(
user = await user_manager.authenticate(credentials)

if user is None or not user.is_active:
maybe_existing = await user_manager.get_by_email(credentials.username)
metric = FailedLoginAttempts
if maybe_existing:
metric = FailedLoginAttempts.labels(user_id=maybe_existing.id)
metric = FailedLoginAttempts.labels(user_id=None)
try:
maybe_existing = await user_manager.get_by_email(credentials.username)
if maybe_existing:
metric = FailedLoginAttempts.labels(user_id=maybe_existing.id)
except UserNotExists:
pass

metric.inc()
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
Expand Down

0 comments on commit b2f00f0

Please sign in to comment.