Skip to main content

rustauth_plugins/username/
mod.rs

1//! Username plugin.
2
3mod endpoints;
4mod errors;
5mod hooks;
6mod options;
7mod schema;
8
9pub use errors::{
10    EMAIL_NOT_VERIFIED, INVALID_DISPLAY_USERNAME, INVALID_USERNAME, INVALID_USERNAME_OR_PASSWORD,
11    UNEXPECTED_ERROR, USERNAME_IS_ALREADY_TAKEN, USERNAME_TOO_LONG, USERNAME_TOO_SHORT,
12};
13pub use options::{
14    UsernameOptions, UsernameOptionsBuilder, UsernameValidationError, ValidationOrder,
15    ValidationPhase,
16};
17pub use schema::UsernameSchemaOptions;
18
19use rustauth_core::plugin::AuthPlugin;
20
21pub const UPSTREAM_PLUGIN_ID: &str = "username";
22
23#[must_use]
24pub fn username(options: UsernameOptions) -> AuthPlugin {
25    let options = std::sync::Arc::new(options);
26    let schema = options.schema.clone();
27    let mut plugin = AuthPlugin::new(UPSTREAM_PLUGIN_ID)
28        .with_version(crate::VERSION)
29        .with_endpoint(endpoints::sign_in_username_endpoint(options.clone()))
30        .with_endpoint(endpoints::is_username_available_endpoint(options.clone()))
31        .with_database_hook(hooks::normalize_create_user_hook(options.clone()))
32        .with_database_hook(hooks::normalize_update_user_hook(options.clone()))
33        .with_before_hook(
34            "/sign-up/email",
35            hooks::sign_up_before_hook(options.clone()),
36        )
37        .with_before_hook("/update-user", hooks::update_user_before_hook(options))
38        .with_schema(schema::username_field(&schema))
39        .with_schema(schema::display_username_field(&schema));
40
41    for error_code in errors::error_codes() {
42        plugin = plugin.with_error_code(error_code);
43    }
44
45    plugin
46}