show name of loggedin_user; add logout functionality

This commit is contained in:
philipp 2023-04-03 22:10:12 +02:00
parent 387d93bbaf
commit 303624ab83
3 changed files with 15 additions and 4 deletions

View File

@ -52,6 +52,13 @@ async fn login(
Flash::success(Redirect::to("/"), "Login erfolgreich") Flash::success(Redirect::to("/"), "Login erfolgreich")
} }
pub fn routes() -> Vec<Route> { #[get("/logout")]
routes![index, login] async fn logout(cookies: &CookieJar<'_>, _user: User) -> Flash<Redirect> {
cookies.remove_private(Cookie::named("loggedin_user"));
Flash::success(Redirect::to("/auth"), "Logout erfolgreich")
}
pub fn routes() -> Vec<Route> {
routes![index, login, logout]
} }

View File

@ -7,8 +7,8 @@ use crate::model::user::User;
mod auth; mod auth;
#[get("/")] #[get("/")]
fn index(_user: User) -> Template { fn index(user: User) -> Template {
Template::render("index", context! {}) Template::render("index", context! {loggedin_user: user})
} }
#[catch(401)] //unauthorized #[catch(401)] //unauthorized

View File

@ -2,6 +2,10 @@
{% block content %} {% block content %}
{% if loggedin_user %}
Hi {{ loggedin_user.name }}. <a href="/auth/logout">LOGOUT</a>
{% endif %}
{% if flash %} {% if flash %}
{% if flash.0 == "success" %} {% if flash.0 == "success" %}
<div class="row"> <div class="row">