From 04e2740c6f0fd9f1a88429c6df4a212f2f27bb39 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Wed, 13 Dec 2023 08:02:22 +0000 Subject: add redirect from login page if already logged in --- src/routes/login.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/routes/login.rs') diff --git a/src/routes/login.rs b/src/routes/login.rs index bd7eaf1..9bb4efa 100644 --- a/src/routes/login.rs +++ b/src/routes/login.rs @@ -2,6 +2,7 @@ use actix_session::Session; use actix_web::http::header::LOCATION; use actix_web::{get, post, web, HttpResponse}; use serde::Deserialize; +use uuid::Uuid; use crate::error::PinussyError; use crate::notification::{Kind, Notification}; @@ -10,15 +11,24 @@ use crate::Pinussy; use crate::Result; #[get("/login")] -async fn get() -> HttpResponse { - HttpResponse::Ok().body(render!(templates::login_html, None).unwrap()) +async fn get(session: Session, state: web::Data) -> Result { + if let Some(user_id) = session.get::("user_id")? { + if state.db.users().read(user_id).await.is_ok() { + return Ok(HttpResponse::SeeOther() + .insert_header((LOCATION, "/")) + .finish()); + } else { + session.purge() + } + } + Ok(HttpResponse::Ok().body(render!(templates::login_html, None).unwrap())) } #[derive(Deserialize)] struct LoginForm { username: String, password: String, - rememberme: Option, + // rememberme: Option, } #[post("/login")] -- cgit