User can sign out

This commit is contained in:
Pierre HUBERT 2022-04-01 19:05:40 +02:00
parent e07dee7fde
commit 372dfa3f31
2 changed files with 25 additions and 4 deletions

View File

@ -26,18 +26,31 @@ struct LoginTemplate {
}
#[derive(serde::Deserialize)]
pub struct LoginRequest {
pub struct LoginRequestBody {
login: String,
password: String,
}
#[derive(serde::Deserialize)]
pub struct LoginRequestQuery {
logout: Option<bool>,
}
/// Authenticate user
pub async fn login_route(users: web::Data<Addr<UsersActor>>,
req: Option<web::Form<LoginRequest>>,
query: web::Query<LoginRequestQuery>,
req: Option<web::Form<LoginRequestBody>>,
id: Identity) -> impl Responder {
let mut danger = String::new();
let mut success = String::new();
let mut login = String::new();
// Check if user session must be closed
if let Some(true) = query.logout {
id.forget();
success = "Goodbye!".to_string();
}
// Check if user is already authenticated
if SessionIdentity::is_authenticated(&id) {
return redirect_user("/");
@ -75,9 +88,14 @@ pub async fn login_route(users: web::Data<Addr<UsersActor>>,
_parent: BaseLoginPage {
page_title: "Login",
danger,
success: "".to_string(),
success,
app_name: APP_NAME,
},
login,
}.render().unwrap())
}
/// Sign out user
pub async fn logout_route() -> impl Responder {
redirect_user("/login?logout=true")
}

View File

@ -4,7 +4,7 @@ use clap::Parser;
use basic_oidc::constants::{DEFAULT_ADMIN_PASSWORD, DEFAULT_ADMIN_USERNAME};
use basic_oidc::controllers::assets_controller::assets_route;
use basic_oidc::controllers::login_controller::login_route;
use basic_oidc::controllers::login_controller::{login_route, logout_route};
use basic_oidc::data::app_config::AppConfig;
use basic_oidc::data::entity_manager::EntityManager;
use basic_oidc::data::user::{hash_password, User};
@ -81,6 +81,9 @@ async fn main() -> std::io::Result<()> {
// Login page
.route("/login", web::get().to(login_route))
.route("/login", web::post().to(login_route))
// Logout page
.route("/logout", web::get().to(logout_route))
})
.bind(config.listen_address)?
.run()