From 044991d62380421c36079a079cd2c54e10b54daa Mon Sep 17 00:00:00 2001 From: Alphonse Paix Date: Wed, 17 Sep 2025 23:57:45 +0200 Subject: [PATCH] Fix redirect issues Dashboard button now correctly redirects to login page if user is not logged in while login page redirects to dashboard the other way around. --- src/routes/login.rs | 18 +++++++++++++++--- templates/base.html | 3 ++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/routes/login.rs b/src/routes/login.rs index 08dfb05..dc5138f 100644 --- a/src/routes/login.rs +++ b/src/routes/login.rs @@ -6,12 +6,15 @@ use crate::{ templates::ErrorTemplate, }; use askama::Template; -use axum::http::{HeaderMap, StatusCode}; use axum::{ Form, Json, extract::State, response::{Html, IntoResponse, Response}, }; +use axum::{ + http::{HeaderMap, StatusCode}, + response::Redirect, +}; use secrecy::SecretString; #[derive(thiserror::Error)] @@ -65,8 +68,17 @@ pub struct LoginFormData { password: SecretString, } -pub async fn get_login() -> Html { - Html(LoginTemplate.render().unwrap()) +pub async fn get_login(session: TypedSession) -> Result { + if session + .get_user_id() + .await + .map_err(|e| LoginError::UnexpectedError(e.into()))? + .is_some() + { + Ok(Redirect::to("/admin/dashboard").into_response()) + } else { + Ok(Html(LoginTemplate.render().unwrap()).into_response()) + } } pub async fn post_login( diff --git a/templates/base.html b/templates/base.html index 5733504..9171ada 100644 --- a/templates/base.html +++ b/templates/base.html @@ -9,7 +9,7 @@ {% block title %}zero2prod{% endblock %} - +
@@ -24,6 +24,7 @@