Struct rocket_auth::Users[][src]

pub struct Users { /* fields omitted */ }
Expand description

The Users struct is used to query users from the database, as well as to create, modify and delete them.

Implementations

It creates a Users instance by connecting it to a sqlite database. This method uses the sqlx crate. If the database does not yet exist it will return an Error. By default, sessions will be stored on a concurrent HashMap. In order to have persistent sessions see the method open_redis.

let users = Users::open_sqlite("database.db").await?;

rocket::build()
    .manage(users)
    .launch()
    .await;

Initializes the user table in the database. It won’t drop the table if it already exists. It is necessary to call it explicitly when casting the Users struct from an already established database connection and if the table hasn’t been created yet. If the table already exists then this step is not necessary.

let mut conn = SqlitePool::connect("database.db").await?;
let mut users: Users = conn.into();
users.open_redis("redis://127.0.0.1/")?;
users.create_table().await?;

Opens a redis connection. It allows for sessions to be stored persistently across different launches. Note that persistent sessions also require a secret_key to be set in the Rocket.toml configuration file.

let mut users = Users::open_sqlite("database.db").await?;
users.open_redis("redis://127.0.0.1/")?;

rocket::build()
    .manage(users)
    .launch();

It creates a Users instance by connecting it to a sqlite database. This method uses the rusqlite crate. If the database does not yet exist it will attempt to create it. By default, sessions will be stored on a concurrent HashMap. In order to have persistent sessions see the method open_redis.

let users = Users::open_rusqlite("database.db")?;

rocket::build()
    .manage(users)
    .launch()
    .await;

It creates a Users instance by connecting it to a postgres database. This method uses the sqlx crate.

let users = Users::open_postgres("postgres://postgres:password@localhost/test").await?;

rocket::build()
    .manage(users)
    .launch();

It creates a Users instance by connecting it to a mysql database. This method uses the sqlx crate.

let users = Users::open_mysql(DATABASE_URL).await?;

rocket::build()
    .manage(users)
    .launch();

It queries a user by their email.

#[get("/user-information/<email>")]
async fn user_information(email: String, users: &State<Users>) -> Result<String, Error> {
        
    let user = users.get_by_email(&email).await?;
    Ok(format!("{:?}", user))
}

It queries a user by their email.

 let user = users.get_by_id(3).await?;
 format!("{:?}", user);

Inserts a new user in the database. It will fail if the user already exists.

#[get("/create_admin/<email>/<password>")]
async fn create_admin(email: String, password: String, users: &State<Users>) -> Result<String, Error> {
    users.create_user(&email, &password, true).await?;
    Ok("User created successfully".into())
}

Deletes a user from de database. Note that this method won’t delete the session. To do that use Auth::delete.

#[get("/delete_user/<id>")]
async fn delete_user(id: i32, users: &State<Users>) -> Result<String> {
    users.delete(id).await?;
    Ok("The user has been deleted.")
}

Modifies a user in the database.

let mut user = users.get_by_id(4).await?;
user.set_email("new@email.com");
user.set_password("new password");
users.modify(&user).await?;

Trait Implementations

Additionally, Users can be created from a tuple, where the first element is a database connection, and the second is a redis connection.

let (db_client, connection) = tokio_postgres::connect(postgres_path, NoTls).await?;
let redis_client = redis::Client::open(redis_path)?;

let users: Users = (db_client, redis_client).into();
// we create the user table in the
// database if it does not exists.
users.create_table();

Performs the conversion.

A Users instance can also be created from a database connection.

let (client, connection) = tokio_postgres::connect("host=localhost user=postgres", NoTls).await?;
let users: Users = client.into();
// we create the user table in the
// database if it does not exists.
users.create_table();

Performs the conversion.

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.