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
type Error = ErrorResponse
type Error = ErrorResponse
The associated error to be returned if derivation fails.
Auto Trait Implementations
impl RefUnwindSafe for Auth
impl UnwindSafe for Auth
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
Converts self
into a collection.
pub fn vzip(self) -> V
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