Skip to main content

better_auth_api/plugins/
two_factor.rs

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