login

Function login 

Source
pub async fn login(
    __arg0: State<Arc<AppState>>,
    __arg1: Json<LoginScheme>,
) -> Result<impl IntoResponse, (StatusCode, Json<Value>)>
Expand description

Authenticates a user and creates a JWT token for session management.

Verifies the provided username and password against stored credentials using Argon2 verification. On successful authentication, generates and encodes a Claims token via encode_token and sets it as an HTTP-only cookie. The token is valid for 1 hour.

§Arguments

  • State(data): Application state containing AppState for database access
  • request: LoginScheme containing login credentials (username, password)

§Returns

  • 200 OK with JSON containing token and filtered FilteredUser info
  • 400 Bad Request if username not found or password invalid
  • 500 Internal Server Error if database query fails

§Security Features

  • HTTP-only cookie prevents JavaScript access
  • SameSite=Lax protects against CSRF attacks
  • Password verification uses Argon2 with stored User hash
  • JWT token includes user ID and expiration timestamp via Claims encoded by encode_token

§Example Response

{
  "status": "success",
  "token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
  "user": {"id": 1, "first_name": "Admin", "last_name": "User", "username": "admin", "is_admin": true}
}