pub struct RUser {
pub id: String,
pub token: String,
}Expand description
Represents an authenticated user.
RUser is the key to r-token’s “parameter-as-authentication” pattern.
By implementing actix-web’s FromRequest trait, it enables automatic
authentication validation before your handler is called.
§How It Works
When you declare RUser as a handler parameter:
- actix-web extracts the token from the
Authorizationheader - Validates the token using
RTokenManager - If valid: creates an
RUserinstance and calls your handler - If invalid: returns 401 Unauthorized without calling your handler
§Type Safety Guarantee
If your handler receives an RUser parameter, the user is guaranteed
to be authenticated. No manual validation needed!
§Example
use actix_web::{get, HttpResponse};
use r_token::RUser;
#[get("/profile")]
async fn profile(user: RUser) -> impl actix_web::Responder {
// If we reach here, authentication succeeded
HttpResponse::Ok().body(format!("User ID: {}", user.id))
}§Error Responses
- 401 Unauthorized: Token missing, invalid, or expired
- 500 Internal Server Error:
RTokenManagernot registered in app_data
Fields§
§id: StringThe user’s unique identifier.
This corresponds to the ID passed to RTokenManager::login().
token: StringThe authentication token.
Extracted from the Authorization request header.
Trait Implementations§
Source§impl FromRequest for RUser
Implementation of actix-web’s FromRequest trait for automatic authentication.
impl FromRequest for RUser
Implementation of actix-web’s FromRequest trait for automatic authentication.
This implementation enables the “parameter-as-authentication” pattern.
§Validation Flow
When actix-web processes a request with an RUser parameter:
- Retrieve Manager: Extracts
RTokenManagerfrom app_data - Extract Token: Reads the
Authorizationheader (supportsBearerprefix) - Validate Token: Checks if the token exists in the manager’s storage
- Return Result:
- Success: Creates
RUserand calls the handler - Failure: Returns error response without calling the handler
- Success: Creates
§Error Responses
500 Internal Server Error:RTokenManagernot found in app_data or mutex poisoned401 Unauthorized: Token missing or invalid