use actix_web::{
middleware::from_fn,
web::{self, ServiceConfig},
};
use authr_core::database::AuthDatabase;
use crate::{
handlers,
middleware::{csrf::csrf_protection, rate_limit::rate_limit, session::require_session},
};
pub fn config<D: AuthDatabase + 'static>(cfg: &mut ServiceConfig) {
cfg.service(
web::scope("")
.wrap(from_fn(csrf_protection::<D, _>))
.wrap(from_fn(rate_limit))
.configure(public_routes::<D>)
.configure(session_routes::<D>)
.service(
web::scope("")
.wrap(from_fn(require_session::<D, _>))
.configure(protected_routes::<D>),
),
);
}
fn public_routes<D: AuthDatabase + 'static>(cfg: &mut ServiceConfig) {
cfg
.route("/sign-up", web::post().to(handlers::sign_up::<D>))
.route(
"/sign-in/email",
web::post().to(handlers::sign_in_email::<D>),
)
.route(
"/sign-in/social",
web::post().to(handlers::sign_in_social::<D>),
)
.route(
"/callback/{provider}",
web::get().to(handlers::callback::<D>),
)
.route(
"/request-password-reset",
web::post().to(handlers::request_password_reset::<D>),
)
.route(
"/reset-password/{token}",
web::get().to(handlers::reset_password_callback::<D>),
)
.route(
"/reset-password",
web::post().to(handlers::reset_password::<D>),
)
.route(
"/send-verification-email",
web::post().to(handlers::send_verification_email::<D>),
)
.route("/verify-email", web::get().to(handlers::verify_email::<D>));
}
fn session_routes<D: AuthDatabase + 'static>(cfg: &mut ServiceConfig) {
cfg.route("/sign-out", web::post().to(handlers::sign_out::<D>))
.route("/get-session", web::get().to(handlers::get_session::<D>));
}
fn protected_routes<D: AuthDatabase + 'static>(cfg: &mut ServiceConfig) {
cfg
.route("/update-user", web::post().to(handlers::update_user::<D>))
.route("/delete-user", web::post().to(handlers::delete_user::<D>))
.route(
"/verify-password",
web::post().to(handlers::verify_password::<D>),
)
.route(
"/change-password",
web::post().to(handlers::change_password::<D>),
)
.route("/set-password", web::post().to(handlers::set_password::<D>))
.route(
"/list-accounts",
web::get().to(handlers::list_accounts::<D>),
)
.route(
"/get-access-token",
web::post().to(handlers::get_access_token::<D>),
)
.route(
"/refresh-token",
web::post().to(handlers::refresh_token::<D>),
)
.route("/account-info", web::get().to(handlers::account_info::<D>))
.route(
"/link-account",
web::post().to(handlers::link_social_account::<D>),
)
.route(
"/unlink-account",
web::post().to(handlers::unlink_account::<D>),
)
.route(
"/list-sessions",
web::get().to(handlers::list_sessions::<D>),
)
.route(
"/revoke-session",
web::post().to(handlers::revoke_session::<D>),
)
.route(
"/revoke-sessions",
web::post().to(handlers::revoke_sessions::<D>),
)
.route(
"/revoke-other-sessions",
web::post().to(handlers::revoke_other_sessions::<D>),
);
}