From 303624ab83de5fd90fa69c79ee92c3ce3098395c Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 3 Apr 2023 22:10:12 +0200 Subject: [PATCH] show name of loggedin_user; add logout functionality --- src/rest/auth.rs | 11 +++++++++-- src/rest/mod.rs | 4 ++-- templates/index.html.tera | 4 ++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/rest/auth.rs b/src/rest/auth.rs index 83682af..746c7e6 100644 --- a/src/rest/auth.rs +++ b/src/rest/auth.rs @@ -52,6 +52,13 @@ async fn login( Flash::success(Redirect::to("/"), "Login erfolgreich") } -pub fn routes() -> Vec { - routes![index, login] +#[get("/logout")] +async fn logout(cookies: &CookieJar<'_>, _user: User) -> Flash { + cookies.remove_private(Cookie::named("loggedin_user")); + + Flash::success(Redirect::to("/auth"), "Logout erfolgreich") +} + +pub fn routes() -> Vec { + routes![index, login, logout] } diff --git a/src/rest/mod.rs b/src/rest/mod.rs index 0d2f670..9208a5d 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -7,8 +7,8 @@ use crate::model::user::User; mod auth; #[get("/")] -fn index(_user: User) -> Template { - Template::render("index", context! {}) +fn index(user: User) -> Template { + Template::render("index", context! {loggedin_user: user}) } #[catch(401)] //unauthorized diff --git a/templates/index.html.tera b/templates/index.html.tera index 90987dc..bfc7bf6 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -2,6 +2,10 @@ {% block content %} +{% if loggedin_user %} + Hi {{ loggedin_user.name }}. LOGOUT +{% endif %} + {% if flash %} {% if flash.0 == "success" %}