pub struct Auth;Expand description
Implementations§
Source§impl Auth
impl Auth
Sourcepub fn login(user_id: i64)
pub fn login(user_id: i64)
Log in a user by their ID
This sets the user ID in the session, making them authenticated.
§Security
This method regenerates the session ID to prevent session fixation attacks.
Sourcepub fn login_remember(user_id: i64, _remember_token: &str)
pub fn login_remember(user_id: i64, _remember_token: &str)
Log in a user with “remember me” functionality
This extends the session lifetime for persistent login.
§Arguments
user_id- The user’s IDremember_token- A secure token for remember me cookie
Sourcepub fn logout()
pub fn logout()
Log out the current user
Clears the authenticated user from the session.
§Security
This regenerates the CSRF token to prevent any cached tokens from being reused.
Sourcepub fn logout_and_invalidate()
pub fn logout_and_invalidate()
Log out and invalidate the entire session
Use this for complete session destruction (e.g., “logout everywhere”).
Sourcepub async fn logout_other_devices() -> Option<Result<u64, FrameworkError>>
pub async fn logout_other_devices() -> Option<Result<u64, FrameworkError>>
Log out all other sessions for the current user.
Destroys all sessions for the authenticated user except the current one. Use after password changes or security-sensitive operations.
Returns the number of destroyed sessions, or None if not authenticated.
Sourcepub async fn attempt<F, Fut>(
validator: F,
) -> Result<Option<i64>, FrameworkError>
pub async fn attempt<F, Fut>( validator: F, ) -> Result<Option<i64>, FrameworkError>
Attempt to authenticate with a validator function
The validator function should return the user ID if credentials are valid.
§Example
let user_id = Auth::attempt(async {
// Validate credentials
let user = User::find_by_email(&email).await?;
if user.verify_password(&password)? {
Ok(Some(user.id))
} else {
Ok(None)
}
}).await?;
if let Some(id) = user_id {
// Authentication successful
}Sourcepub async fn validate<F, Fut>(validator: F) -> Result<bool, FrameworkError>
pub async fn validate<F, Fut>(validator: F) -> Result<bool, FrameworkError>
Validate credentials without logging in
Useful for password confirmation dialogs.
Sourcepub async fn user() -> Result<Option<Arc<dyn Authenticatable>>, FrameworkError>
pub async fn user() -> Result<Option<Arc<dyn Authenticatable>>, FrameworkError>
Get the currently authenticated user
Returns None if not authenticated or if no UserProvider is registered.
§Example
use ferro_rs::Auth;
if let Some(user) = Auth::user().await? {
println!("Logged in as user {}", user.auth_identifier());
}§Errors
Returns an error if no UserProvider is registered in the container.
Make sure to register a UserProvider in your bootstrap.rs:
bind!(dyn UserProvider, DatabaseUserProvider);Sourcepub async fn user_as<T: Authenticatable + Clone>() -> Result<Option<T>, FrameworkError>
pub async fn user_as<T: Authenticatable + Clone>() -> Result<Option<T>, FrameworkError>
Get the authenticated user, cast to a concrete type
This is a convenience method that retrieves the user and downcasts it to your concrete User type.
§Example
use ferro_rs::Auth;
use ferro_rs::models::users::User;
if let Some(user) = Auth::user_as::<User>().await? {
println!("Welcome, user #{}!", user.id);
}§Type Parameters
T- The concrete user type that implementsAuthenticatableandClone
Auto Trait Implementations§
impl Freeze for Auth
impl RefUnwindSafe for Auth
impl Send for Auth
impl Sync for Auth
impl Unpin for Auth
impl UnsafeUnpin for Auth
impl UnwindSafe for Auth
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more