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 moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);