RUser

Struct RUser 

Source
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:

  1. actix-web extracts the token from the Authorization header
  2. Validates the token using RTokenManager
  3. If valid: creates an RUser instance and calls your handler
  4. 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: RTokenManager not registered in app_data

Fields§

§id: String

The user’s unique identifier.

This corresponds to the ID passed to RTokenManager::login().

§token: String

The authentication token.

Extracted from the Authorization request header.

Trait Implementations§

Source§

impl Debug for RUser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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:

  1. Retrieve Manager: Extracts RTokenManager from app_data
  2. Extract Token: Reads the Authorization header (supports Bearer prefix)
  3. Validate Token: Checks if the token exists in the manager’s storage
  4. Return Result:
    • Success: Creates RUser and calls the handler
    • Failure: Returns error response without calling the handler

§Error Responses

  • 500 Internal Server Error: RTokenManager not found in app_data or mutex poisoned
  • 401 Unauthorized: Token missing or invalid
Source§

type Error = Error

The associated error which can be returned.
Source§

type Future = Ready<Result<RUser, <RUser as FromRequest>::Error>>

Future that resolves to a Self. Read more
Source§

fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future

Create a Self from request parts asynchronously.
Source§

fn extract(req: &HttpRequest) -> Self::Future

Create a Self from request head asynchronously. Read more

Auto Trait Implementations§

§

impl Freeze for RUser

§

impl RefUnwindSafe for RUser

§

impl Send for RUser

§

impl Sync for RUser

§

impl Unpin for RUser

§

impl UnwindSafe for RUser

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more