Struct rocket_auth::Auth[][src]

pub struct Auth<'a> {
    pub users: State<'a, Users>,
    pub cookies: Cookies<'a>,
    pub session: Option<Session>,
}

The Auth guard allows to log in, log out, sign up, modify, and delete the currently (un)authenticated user. For more information see Auth. Because of rust’s ownership rules, you may not retrieve both rocket::http::Cookies and the Auth guard simultaneously. However, retrieveng cookies is not needed since Auth stores them in the public field Auth::cookies. A working example:

#![feature(proc_macro_hygiene, decl_macro)]
use rocket::{get, post, routes, request::Form};
use rocket_auth::{Users, Error, Auth, Signup, Login};

#[post("/signup", data="<form>")]
fn signup(form: Form<Signup>, mut auth: Auth) {
    // users are automatically logged in after signing up.
    auth.signup(&form);
}

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

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

fn main() -> Result<(), Error>{
    let users = Users::open_sqlite("mydb.db")?;

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

Fields

users: State<'a, Users>

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

cookies: Cookies<'a>session: Option<Session>

Implementations

impl<'a> Auth<'a>[src]

pub fn login(&mut self, form: &Login) -> Result<(), Error>[src]

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>, mut auth: Auth) {
    auth.login(&form);
}

pub fn login_for(&mut self, form: &Login, time: Duration) -> Result<(), Error>[src]

Logs a user in for the specified period of time.

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

pub fn signup(&mut self, form: &Signup) -> Result<(), Error>[src]

Creates a new user from a form or a json. The client will be authenticated automatically. Their session will be set to expire in a year. In order to customize the expiration date use signup_for.

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

pub fn signup_for(&mut self, form: &Signup, time: Duration) -> Result<(), Error>[src]

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>, mut auth: Auth) {
    let one_hour = Duration::from_secs(60 * 60);
    auth.signup_for(&form, one_hour);
}

pub fn is_auth(&self) -> bool[src]

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."
    }
}

pub fn get_user(&self) -> Option<User>[src]

It retrieves the current logged user.

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

pub fn logout(&mut self) -> Result<(), Error>[src]

Logs the currently authenticated user out.

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

pub fn delete(&mut self) -> Result<(), Error>[src]

Deletes the account of the currently authenticated user.

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

pub fn change_password(&self, password: &str) -> Result<(), Error>[src]

Changes the password of the currently authenticated user

auth.change_password("new password");

pub fn change_email(&self, email: String) -> Result<(), Error>[src]

Changes the email of the currently authenticated user

auth.change_password("new@email.com");

pub fn get_session(&self) -> Result<&Session, Error>[src]

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

users.get_session()?

Trait Implementations

impl<'a, 'r> FromRequest<'a, 'r> for Auth<'a>[src]

type Error = Error

The associated error to be returned if derivation fails.

Auto Trait Implementations

impl<'a> !RefUnwindSafe for Auth<'a>

impl<'a> !Send for Auth<'a>

impl<'a> !Sync for Auth<'a>

impl<'a> Unpin for Auth<'a>

impl<'a> !UnwindSafe for Auth<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, I> AsResult<T, I> for T where
    I: Input, 

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Erased for T

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> IntoCollection<T> for T

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Typeable for T where
    T: Any

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