Struct rocket_auth::prelude::Auth[][src]

pub struct Auth<'a> {
    pub users: &'a State<Users>,
    pub cookies: &'a CookieJar<'a>,
    pub session: Option<Session>,
}
Expand description

The Auth guard allows to log in, log out, sign up, modify, and delete the currently (un)authenticated user. For more information see Auth. A working example:


use rocket::{*, form::Form};
use rocket_auth::{Users, Error, Auth, Signup, Login};

#[post("/signup", data="<form>")]
async fn signup(form: Form<Signup>, auth: Auth<'_>) {
    auth.signup(&form).await;
    auth.login(&form.into());
}

#[post("/login", data="<form>")]
fn login(form: Form<Login>, auth: Auth) {
    auth.login(&form);
}

#[get("/logout")]
fn logout(auth: Auth) {
    auth.logout();
}
#[tokio::main]
async fn main() -> Result<(), Error>{
    let users = Users::open_sqlite("mydb.db").await?;

    rocket::build()
        .mount("/", routes![signup, login, logout])
        .manage(users)
        .launch()
        .await;
    Ok(())
}

Fields

users: &'a State<Users>

Auth includes in its fields a Users instance. Therefore, it is not necessary to retrieve Users when using this guard.

cookies: &'a CookieJar<'a>session: Option<Session>

Implementations

Logs in the user through a parsed form or json. The session is set to expire in one year by default. For a custom expiration date use Auth::login_for.

#[post("/login", data="<form>")]
fn login(form: Form<Login>, auth: Auth) {
    auth.login(&form);
}

Logs a user in for the specified period of time.

#[post("/login", data="<form>")]
fn login(form: Form<Login>, auth: Auth) {
    let one_hour = Duration::from_secs(60 * 60);
    auth.login_for(&form, one_hour);
}

Creates a new user from a form or a json. The user will not be authenticated by default. In order to authenticate the user, cast the signup form to a login form or use signup_for.

#[post("/signup", data="<form>")]
async fn signup(form: Form<Signup>, auth: Auth<'_>) -> Result<&'static str, Error>{
    auth.signup(&form).await?;
    auth.login(&form.into()).await?;
    Ok("Logged in")
}

Creates a new user from a form or a json. The session will last the specified period of time.

#[post("/signup", data="<form>")]
fn signup_for(form: Form<Signup>, auth: Auth) {
    let one_hour = Duration::from_secs(60 * 60);
    auth.signup_for(&form, one_hour);
}

It allows to know if the current client is authenticated or not.

#[get("/am-I-authenticated")]
fn is_auth(auth: Auth<'_>) -> &'static str {
    if auth.is_auth() {
        "Yes you are."
    } else {
        "nope."
    }
}

It retrieves the current logged user.

#[get("/display-me")]
async fn display_me(auth: Auth<'_>) -> String {
    format!("{:?}", auth.get_user().await)
}

Logs the currently authenticated user out.

#[get("/logout")]
fn logout(auth: Auth)  {
    auth.logout();
}

Deletes the account of the currently authenticated user.

#[get("/delete-my-account")]
fn delete(auth: Auth)  {
    auth.delete();
}

Changes the password of the currently authenticated user

    auth.change_password("new password");

Changes the email of the currently authenticated user

auth.change_email("new@email.com".into());

This method is useful when the function returns a Result type. It is intended to be used primarily with the ? operator.

auth.get_session()?;

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

Converts self into T using Into<T>. Read more

Converts self into a target type. Read more

Causes self to use its Binary implementation when Debug-formatted.

Causes self to use its Display implementation when Debug-formatted. Read more

Causes self to use its LowerExp implementation when Debug-formatted. Read more

Causes self to use its LowerHex implementation when Debug-formatted. Read more

Causes self to use its Octal implementation when Debug-formatted.

Causes self to use its Pointer implementation when Debug-formatted. Read more

Causes self to use its UpperExp implementation when Debug-formatted. Read more

Causes self to use its UpperHex implementation when Debug-formatted. 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.

Pipes by value. This is generally the method you want to use. Read more

Borrows self and passes that borrow into the pipe function. Read more

Mutably borrows self and passes that borrow into the pipe function. Read more

Borrows self, then passes self.borrow() into the pipe function. Read more

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more

Borrows self, then passes self.as_ref() into the pipe function.

Mutably borrows self, then passes self.as_mut() into the pipe function. Read more

Borrows self, then passes self.deref() into the pipe function.

Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more

Pipes a value into a function that cannot ordinarily be called in suffix position. Read more

Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more

Pipes a dereference into a function that cannot normally be called in suffix position. Read more

Pipes a mutable dereference into a function that cannot normally be called in suffix position. Read more

Pipes a reference into a function that cannot ordinarily be called in suffix position. Read more

Pipes a mutable reference into a function that cannot ordinarily be called in suffix position. Read more

Should always be Self

Immutable access to a value. Read more

Mutable access to a value. Read more

Immutable access to the Borrow<B> of a value. Read more

Mutable access to the BorrowMut<B> of a value. Read more

Immutable access to the AsRef<R> view of a value. Read more

Mutable access to the AsMut<R> view of a value. Read more

Immutable access to the Deref::Target of a value. Read more

Mutable access to the Deref::Target of a value. Read more

Calls .tap() only in debug builds, and is erased in release builds.

Calls .tap_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more

Provides immutable access for inspection. Read more

Calls tap in debug builds, and does nothing in release builds.

Provides mutable access for modification. Read more

Calls tap_mut in debug builds, and does nothing in release builds.

Provides immutable access to the reference for inspection.

Calls tap_ref in debug builds, and does nothing in release builds.

Provides mutable access to the reference for modification.

Calls tap_ref_mut in debug builds, and does nothing in release builds.

Provides immutable access to the borrow for inspection. Read more

Calls tap_borrow in debug builds, and does nothing in release builds.

Provides mutable access to the borrow for modification.

Calls tap_borrow_mut in debug builds, and does nothing in release builds. Read more

Immutably dereferences self for inspection.

Calls tap_deref in debug builds, and does nothing in release builds.

Mutably dereferences self for modification.

Calls tap_deref_mut in debug builds, and does nothing in release builds. Read more

Attempts to convert self into T using TryInto<T>. Read more

Attempts to convert self into a target type. Read more

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.