Skip to main content

ferro_rs/auth/
authenticatable.rs

1//! Authenticatable trait for models that can be authenticated
2//!
3//! Implement this trait on your User model to enable `Auth::user()`.
4
5use std::any::Any;
6
7/// Trait for models that can be authenticated
8///
9/// Implement this trait on your User model to enable `Auth::user()`.
10///
11/// # Example
12///
13/// ```rust,ignore
14/// use ferro_rs::auth::Authenticatable;
15/// use std::any::Any;
16///
17/// impl Authenticatable for User {
18///     fn auth_identifier(&self) -> i64 {
19///         self.id as i64
20///     }
21///
22///     fn as_any(&self) -> &dyn Any {
23///         self
24///     }
25/// }
26/// ```
27pub trait Authenticatable: Send + Sync + 'static {
28    /// Get the unique identifier for the user (typically the primary key)
29    fn auth_identifier(&self) -> i64;
30
31    /// Get the name of the identifier column (e.g., "id")
32    ///
33    /// Override this if your user model uses a different column name.
34    fn auth_identifier_name(&self) -> &'static str {
35        "id"
36    }
37
38    /// Allow downcasting to concrete type
39    ///
40    /// This is used by `Auth::user_as::<T>()` to cast the trait object
41    /// back to the concrete User type.
42    fn as_any(&self) -> &dyn Any;
43}