Struct backend::utils::auth::Auth[][src]

pub struct Auth {
    pub id: i64,
}
Expand description

Usage of Auth

Example

use rocket::Request;
use rocket::{Build, Rocket};
use crate::models::error::*;
use crate::utils::auth::{self, Auth, ValidToken};
pub async fn init(rocket: Rocket<Build>) -> Rocket<Build> {
    rocket
        .mount(
            "/sample",
            routes![
                auth_name,
                auth_new,
            ],
        )
        .register(
            "/sample/auth/new",
            catchers![auth_new_bad_request, auth_new_unauthorized],
        )
}

#[get("/auth/<name>")]
async fn auth_name(auth: Result<Auth, ErrorResponse>, name: &str) -> String {
    if let Err(e) = auth {
        return format!("{:?}", e);
    }
    format!("Hello, {}!", name)
}

#[get("/auth/new/<name>")]
async fn auth_new(auth: Auth, name: &str) -> String {
    format!("Hello, {}, your id is {}!", name, auth.id)
}

#[catch(400)]
async fn auth_new_bad_request(request: &Request<'_>) -> String {
    let user_result = request
        .local_cache_async(async { auth::auth_token(request).await })
        .await;
    match user_result {
        Some(e) => match e {
            ValidToken::Invalid => "Invalid token".to_string(),
            ValidToken::Missing => "Missing token".to_string(),
            ValidToken::DatabaseErr => "DatabaseErr token".to_string(),
            ValidToken::Valid(id) => format!("User Id found: {}", id),
            ValidToken::Refresh(id) => format!("User Id found: {}", id),
        },
        None => "Valid token".to_string(),
    }
}

#[catch(401)]
async fn auth_new_unauthorized(request: &Request<'_>) -> String {
    let user_result = request
        .local_cache_async(async { auth::auth_token(request).await })
        .await;
    match user_result {
        Some(e) => match e {
            ValidToken::Invalid => "Invalid token".to_string(),
            ValidToken::Missing => "Missing token".to_string(),
            ValidToken::DatabaseErr => "DatabaseErr token".to_string(),
            ValidToken::Valid(id) => format!("User Id found: {}", id),
            ValidToken::Refresh(id) => format!("User Id found: {}", id),
        },
        None => "Valid token".to_string(),
    }
}

Fields

id: i64

Trait Implementations

The associated error to be returned if derivation fails.

Derives an instance of Self from the incoming request metadata. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

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

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

Performs the conversion.

Converts self into a collection.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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