From de039e5e6c9fba2ea2b5cae8fcadcb37a3a95b90 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Thu, 21 Mar 2024 01:59:27 +0100 Subject: [PATCH] Revert to old page when reloading without being logged, clean logs. --- src/App/Container.purs | 21 ++++++++++++++++----- src/App/Page/Authentication.purs | 4 ++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/App/Container.purs b/src/App/Container.purs index 596ac73..7340344 100644 --- a/src/App/Container.purs +++ b/src/App/Container.purs @@ -93,8 +93,10 @@ type Password = String type LogInfo = Tuple Login Password data Action + = Initialize + -- | Handle events from `AuthenticationInterface`. - = AuthenticationInterfaceEvent AI.Output + | AuthenticationInterfaceEvent AI.Output -- | Handle events from `RegistrationInterface`. | RegistrationInterfaceEvent RI.Output @@ -209,7 +211,9 @@ component = H.mkComponent { initialState , render - , eval: H.mkEval $ H.defaultEval { handleAction = handleAction } + , eval: H.mkEval $ H.defaultEval { initialize = Just Initialize + , handleAction = handleAction + } } -- | Initial state is simple: the user is on the home page, nothing else is stored. @@ -287,6 +291,14 @@ render state handleAction :: forall o monad. MonadAff monad => Action -> H.HalogenM State Action ChildSlots o monad Unit handleAction = case _ of + Initialize -> do + handleAction $ Log $ SystemLog "Hello, welcome to this application! 🥳" + sessionstorage <- H.liftEffect $ Window.sessionStorage =<< HTML.window + token <- H.liftEffect $ Storage.getItem "user-authd-token" sessionstorage + case token of + Nothing -> revert_old_page + Just _ -> pure unit -- Authentication will happen when web sockets are up! + Routing page -> do -- Store the current page we are on and restore it when we reload. sessionstorage <- H.liftEffect $ Window.sessionStorage =<< HTML.window @@ -335,7 +347,7 @@ handleAction = case _ of sessionstorage <- H.liftEffect $ Window.sessionStorage =<< HTML.window token <- H.liftEffect $ Storage.getItem "user-authd-token" sessionstorage case token of - Nothing -> handleAction $ Log $ ErrorLog "no token!" + Nothing -> pure unit Just t -> do H.modify_ _ { token = Just t } handleAction AuthenticateToDNSManager @@ -442,7 +454,7 @@ handleAction = case _ of sessionstorage <- H.liftEffect $ Window.sessionStorage =<< HTML.window token <- H.liftEffect $ Storage.getItem "user-authd-token" sessionstorage case token of - Nothing -> handleAction $ Log $ ErrorLog "no token!" + Nothing -> pure unit Just t -> do handleAction $ Log $ SystemLog "Let's authenticate to authd" handleAction $ AuthenticateToAuthd (Left t) @@ -587,7 +599,6 @@ handleAction = case _ of WS.MessageReceived (Tuple _ message) -> do handleAction $ DecodeDNSMessage message WS.WSJustConnected -> do - handleAction $ Log $ SystemLog "Connection with dnsmanagerd was closed, let's re-authenticate" handleAction AuthenticateToDNSManager H.tell _dli unit DomainListInterface.ConnectionIsUp WS.WSJustClosed -> H.tell _dli unit DomainListInterface.ConnectionIsDown diff --git a/src/App/Page/Authentication.purs b/src/App/Page/Authentication.purs index cedb25b..001c93c 100644 --- a/src/App/Page/Authentication.purs +++ b/src/App/Page/Authentication.purs @@ -2,7 +2,7 @@ -- | TODO: token validation. module App.Page.Authentication where -import Prelude (Unit, bind, discard, pure, ($), (<<<), (=<<), (<>), (>), (==), map, show) +import Prelude (Unit, bind, discard, pure, ($), (<<<), (=<<), (<>), (>), (==), map, show, unit) import Data.Array as A import Data.ArrayBuffer.Types (ArrayBuffer) @@ -289,7 +289,7 @@ handleAction = case _ of sessionstorage <- H.liftEffect $ Window.sessionStorage =<< HTML.window old_tab <- H.liftEffect $ Storage.getItem "current-auth-tab" sessionstorage case old_tab of - Nothing -> H.raise $ Log $ ErrorLog "We hadn't changed tab before reload apparently." + Nothing -> pure unit Just current_tab -> case current_tab of "Auth" -> handleAction $ ChangeTab Auth "TabPasswordRecovery" -> handleAction $ ChangeTab TabPasswordRecovery