From d42573bf65672bcb8cc8d0ed9fff593d6b2ed0c6 Mon Sep 17 00:00:00 2001 From: Lucas Vas Date: Thu, 15 Jan 2026 08:37:43 -0500 Subject: [PATCH] feat: implement auth callback page This page will allow users to redirect to their previous path when they sign in. --- src/app.rs | 9 +++----- src/pages/auth_callback.rs | 37 ++++++++++++++++++++++++++++++ src/pages/mod.rs | 4 +++- src/pages/unauthenticated_page.rs | 38 +++++++++++++++++++------------ 4 files changed, 66 insertions(+), 22 deletions(-) create mode 100644 src/pages/auth_callback.rs diff --git a/src/app.rs b/src/app.rs index 9549173..09b05f0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,10 +1,9 @@ -use crate::pages::{AboutPage, HomePage, ProviderPortal, ComparisonTestPage, ScholarshipInfoPage, TestPage, ProviderContactPage, LoanerShell, LoanerFallback, LoanerBorrowForm, LoanerReturnForm}; +use crate::pages::{AboutPage, HomePage, ProviderPortal, ComparisonTestPage, ScholarshipInfoPage, TestPage, ProviderContactPage, LoanerShell, LoanerFallback, LoanerBorrowForm, LoanerReturnForm, AuthCallbackPage}; use leptos::prelude::*; use leptos_meta::{MetaTags, Stylesheet, Title, provide_meta_context}; use leptos_oidc::{Auth, AuthParameters, AuthSignal, Challenge}; use leptos_router::{components::{Route, Router, Routes, ParentRoute}, path}; use crate::components::ToastList; -use leptos::logging::log; pub fn shell(options: LeptosOptions) -> impl IntoView { view! { @@ -66,15 +65,12 @@ pub fn AppWithRoutes() -> impl IntoView { provide_meta_context(); let current_origin = use_origin(); - log!("Current origin: {}", current_origin); // Authentication setup let parameters = AuthParameters { issuer: "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_Lfjuy5zaM".into(), client_id: "10jr2h3vtpu9n7gj46pvg5qo2q".into(), - redirect_uri: format!("{}/", use_origin()), - // redirect_uri: Url::parse(format!("{}", current_origin).as_str()).unwrap().to_string(), - // redirect_uri: Url::parse("http://localhost:3000").unwrap().to_string(), + redirect_uri: format!("{}/auth/callback", current_origin), post_logout_redirect_uri: current_origin, scope: Some("openid%20profile%20email".into()), audience: None, @@ -103,6 +99,7 @@ pub fn AppWithRoutes() -> impl IntoView { // TODO Create a 404 page + diff --git a/src/pages/auth_callback.rs b/src/pages/auth_callback.rs new file mode 100644 index 0000000..4b2dbe3 --- /dev/null +++ b/src/pages/auth_callback.rs @@ -0,0 +1,37 @@ +use leptos::prelude::*; +use leptos_oidc::AuthSignal; +use leptos_router::hooks::use_navigate; + +#[component] +pub fn AuthCallbackPage() -> impl IntoView { + // This should wait for authentication, read the values in session storage, and then redirect + // to the correct page. The session storage should contain a path, which we'll need to check. + + let auth = expect_context::(); + let navigate = use_navigate(); + + Effect::new(move || { + // Wait for auth to load, then read information. + if let Some(_) = auth.get().authenticated() { + // Read the sessionStorage to get the last navigated path. + let storage_opt = window().session_storage().ok().flatten(); + let path = storage_opt + .and_then(|storage| storage.get_item("post_login_path").ok().flatten()) + .unwrap_or_else(|| "/".to_string()); + + // Clear storage in case we need to reauth again + if let Some(storage) = window().session_storage().ok().flatten() { + let _ = storage.remove_item("post_login_path"); + } + + // Redirect to the stored path + navigate(&path, Default::default()); + } + }); + + view! { +
+

"Completing the login process..."

+
+ } +} diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 4da8c68..1b363d8 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -9,8 +9,10 @@ mod test_page; mod unauthenticated_page; mod utils; mod provider_contact; +mod auth_callback; pub use self::{ about_page::*, comparison_test::*, home_page::*, loaner_page::*, provider_portal::*, - test_page::*, unauthenticated_page::*, scholarship_info::*, provider_contact::* + test_page::*, unauthenticated_page::*, scholarship_info::*, provider_contact::*, + auth_callback::*, }; diff --git a/src/pages/unauthenticated_page.rs b/src/pages/unauthenticated_page.rs index 7de72e3..e2d6857 100644 --- a/src/pages/unauthenticated_page.rs +++ b/src/pages/unauthenticated_page.rs @@ -2,35 +2,43 @@ use crate::components::ActionButton; use leptos::logging::log; use leptos::prelude::*; use leptos_oidc::AuthSignal; +use leptos_router::hooks::{use_navigate, use_location}; #[component] pub fn UnauthenticatedPage() -> impl IntoView { let auth = use_context::().expect("AuthSignal not present in LoginLink"); - let login_url = move || { + let login_url = Memo::new(move |_| { auth.with(|auth| { auth.unauthenticated() .map(|unauthenticated| unauthenticated.login_url()) }) + }); + + Effect::new(move || log!("Generated login url: {:?}", login_url.get())); + + let navigate = use_navigate(); + let location = use_location(); + + let on_click = move |_| { + let current_path = location.pathname.get(); + + let storage_opt = window().session_storage().ok().flatten(); + if let Some(session_storage) = storage_opt { + let _ = session_storage.set_item("post_login_path", ¤t_path); + } + if let Some(url) = login_url.get() { + navigate(&url, Default::default()) + } }; - Effect::new(move || log!("Generated login url: {:?}", login_url())); - - let navigate = leptos_router::hooks::use_navigate(); - - view! ( + view! {
-

"You're not signed in. Please sign in with your Region 15 email."

+

"You're not signed in. Please sign in with your Region 15 email."

- - "Sign in" - + "Sign in"
- ) + } }