rustauth_plugins/last_login_method/
mod.rs1mod config;
4mod cookie;
5mod persistence;
6mod resolve;
7mod schema;
8
9use rustauth_core::plugin::{AuthPlugin, PluginAfterHookAction};
10
11pub use config::{
12 LastLoginMethodOptions, LastLoginMethodOptionsBuilder, DEFAULT_COOKIE_MAX_AGE,
13 DEFAULT_COOKIE_NAME, DEFAULT_DATABASE_FIELD_NAME,
14};
15pub use resolve::{default_login_method, LoginMethodContext};
16
17pub const UPSTREAM_PLUGIN_ID: &str = "last-login-method";
18
19#[must_use]
20pub fn last_login_method(options: LastLoginMethodOptions) -> AuthPlugin {
21 let hook_options = options.clone();
22 let init_options = options;
23 AuthPlugin::new(UPSTREAM_PLUGIN_ID)
24 .with_version(crate::VERSION)
25 .with_init(move |_context| Ok(schema::init_output(&init_options)))
26 .with_async_after_hook("*", move |context, request, response| {
27 let options = hook_options.clone();
28 Box::pin(async move {
29 let response =
30 cookie::set_last_login_method_cookie(context, request, response, &options)?;
31 if let Err(error) =
32 persistence::persist_last_login_method(context, request, &options).await
33 {
34 let message = error.to_string();
35 context
36 .logger
37 .error("Failed to update last_login_method", &[message.as_str()]);
38 }
39 Ok(PluginAfterHookAction::Continue(response))
40 })
41 })
42}