From 90b7136b8f210eb0fab6d2bd29107d3757ccf9e3 Mon Sep 17 00:00:00 2001 From: Johannes Randerath Date: Fri, 30 Aug 2024 12:44:50 +0200 Subject: [PATCH] Added logout and manage account links redirecting to the oauth provider --- src/main.rs | 17 +++++++++++++---- templates/base.html.tera | 9 +++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index d91a7b1..7a21860 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,7 @@ use auth::AuthUser; use rocket::fs::{FileServer, relative}; use rocket_dyn_templates::{Template, context}; -use rocket::response::Redirect; +use rocket::response::{Flash, Redirect}; use inventur_db; use rocket_sync_db_pools::{database, diesel}; use rocket_oauth2::OAuth2; @@ -38,6 +38,7 @@ use std::env; use dotenvy::dotenv; use rocket::Config; use rocket::figment::providers::Env; +use rocket::http::CookieJar; /// Database connection using diesel and rocket_sync_db_pools #[database("inventur")] @@ -66,10 +67,20 @@ async fn home(conn: Db, user: AuthUser) -> Template { tnames: tnames, columns: cols, rows: rows, + username: user.uname, + email: user.email, } ) } +#[get("/logout")] +async fn logout(_user: AuthUser, cookies: &CookieJar<'_>) -> Flash { + for cookie in cookies.iter() { + cookies.remove_private(cookie.clone()); + } + Flash::success(Redirect::to("https://ldap.randerath.eu/realms/master/protocol/openid-connect/logout"), "Goodbye!") +} + /// If no user is authenticated, redirect the user to authenticate with the oauth identity provider. #[get("/", rank=2)] async fn login_home() -> Redirect { @@ -103,7 +114,7 @@ async fn rocket() -> _ { .attach(Template::fairing()) .attach(Db::fairing()) .attach(OAuth2::::fairing("oauth")) - .mount("/", routes![auth::oauth_login, auth::oauth_callback, home, login_home, favicon]) + .mount("/", routes![auth::oauth_login, auth::oauth_callback, home, login_home, favicon, logout]) .mount("/table", routes![table::table, table::table_sec, table::edit_tname, table::create_table, table::import_table, table::delete_table]) .mount("/row", routes![table::new_entry, table::edit_entry, table::delete_entry]) .mount("/column", routes![table::delete_column, table::edit_column, table::create_column]) @@ -112,6 +123,4 @@ async fn rocket() -> _ { .mount("/css", FileServer::from(relative!("static/css"))) .mount("/js", FileServer::from(relative!("static/js"))) .mount("/txt", FileServer::from(relative!("static/txt"))) - - } diff --git a/templates/base.html.tera b/templates/base.html.tera index 225f692..97d5872 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -63,6 +63,15 @@ +