Skip to main content

better_auth_api/plugins/
two_factor.rs

1use async_trait::async_trait;
2
3use better_auth_core::DatabaseAdapter;
4use better_auth_core::{AuthContext, AuthPlugin, AuthRoute};
5use better_auth_core::{AuthError, AuthResult};
6use better_auth_core::{AuthRequest, AuthResponse, HttpMethod};
7
8/// Two-factor authentication plugin
9pub struct TwoFactorPlugin {
10    // TODO: Add 2FA configuration
11}
12
13impl TwoFactorPlugin {
14    pub fn new() -> Self {
15        Self {}
16    }
17}
18
19impl Default for TwoFactorPlugin {
20    fn default() -> Self {
21        Self::new()
22    }
23}
24
25#[async_trait]
26impl<DB: DatabaseAdapter> AuthPlugin<DB> for TwoFactorPlugin {
27    fn name(&self) -> &'static str {
28        "two-factor"
29    }
30
31    fn routes(&self) -> Vec<AuthRoute> {
32        vec![
33            AuthRoute::post("/2fa/setup", "setup_2fa"),
34            AuthRoute::post("/2fa/verify", "verify_2fa"),
35            AuthRoute::post("/2fa/disable", "disable_2fa"),
36        ]
37    }
38
39    async fn on_request(
40        &self,
41        req: &AuthRequest,
42        _ctx: &AuthContext<DB>,
43    ) -> AuthResult<Option<AuthResponse>> {
44        match (req.method(), req.path()) {
45            (HttpMethod::Post, path) if path.starts_with("/2fa/") => Err(
46                AuthError::not_implemented("Two-factor authentication plugin not yet implemented"),
47            ),
48            _ => Ok(None),
49        }
50    }
51}