better_auth/plugins/
two_factor.rs1use async_trait::async_trait;
2
3use crate::core::{AuthPlugin, AuthRoute, AuthContext};
4use crate::types::{AuthRequest, AuthResponse, HttpMethod};
5use crate::error::{AuthError, AuthResult};
6
7pub struct TwoFactorPlugin {
9 }
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(&self, req: &AuthRequest, ctx: &AuthContext) -> AuthResult<Option<AuthResponse>> {
39 match (req.method(), req.path()) {
40 (HttpMethod::Post, path) if path.starts_with("/2fa/") => {
41 Ok(Some(AuthResponse::json(501, &serde_json::json!({
43 "error": "Not implemented",
44 "message": "Two-factor authentication plugin not yet implemented"
45 }))?))
46 },
47 _ => Ok(None),
48 }
49 }
50}