From a40c4825996c90d107901b0d71162f9356f1395a Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Sun, 19 Jul 2015 20:24:54 +0200 Subject: Showing login token error message in the UI --- public/javascripts/elmLauncher.js | 11 ++++++++++- src/client/Main.elm | 15 ++++++++++++--- src/client/Update.elm | 4 ++++ src/server/Application.hs | 11 ++++++++--- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/public/javascripts/elmLauncher.js b/public/javascripts/elmLauncher.js index 52b1087..c4a00b3 100644 --- a/public/javascripts/elmLauncher.js +++ b/public/javascripts/elmLauncher.js @@ -1 +1,10 @@ -Elm.fullscreen(Elm.Main); +Elm.fullscreen(Elm.Main, { + signInError: getParameterByName('signInError') +}); + +function getParameterByName(name) { + name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); + var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), + results = regex.exec(location.search); + return results && decodeURIComponent(results[1].replace(/\+/g, " ")); +} diff --git a/src/client/Main.elm b/src/client/Main.elm index e79fe2b..519360a 100644 --- a/src/client/Main.elm +++ b/src/client/Main.elm @@ -15,6 +15,7 @@ import Model exposing (Model, initialModel) import Model.Payment exposing (Payments, paymentsDecoder) import Update exposing (Action(..), actions, updateModel) +import Update.SignIn exposing (..) import View.Page exposing (renderPage) @@ -28,13 +29,21 @@ main = Signal.map renderPage model model : Signal Model model = Signal.foldp updateModel initialModel actions.signal +------------------------- + +port signInError : Maybe String + --------------------------------------- port fetchPayments : Task Http.Error () port fetchPayments = - getPayments - |> flip Task.andThen reportSuccess - |> flip Task.onError reportError + case signInError of + Just msg -> + Signal.send actions.address (SignInError msg) + Nothing -> + getPayments + |> flip Task.andThen reportSuccess + |> flip Task.onError reportError reportSuccess : Payments -> Task x () reportSuccess payments = Signal.send actions.address (UpdatePayments payments) diff --git a/src/client/Update.elm b/src/client/Update.elm index 1d0fe95..508ee2f 100644 --- a/src/client/Update.elm +++ b/src/client/Update.elm @@ -14,6 +14,7 @@ import Update.SignIn exposing (..) type Action = NoOp | SignIn + | SignInError String | UpdateSignIn SignInAction | UpdatePayments Payments @@ -27,6 +28,9 @@ updateModel action model = model SignIn -> { model | view <- SignInView initSignIn } + SignInError msg -> + let signIn = { initSignIn | result <- Just (Err msg) } + in { model | view <- SignInView signIn } UpdateSignIn signInAction -> case model.view of SignInView signIn -> diff --git a/src/server/Application.hs b/src/server/Application.hs index 739fe33..7e93fe1 100644 --- a/src/server/Application.hs +++ b/src/server/Application.hs @@ -23,6 +23,7 @@ import Control.Monad.IO.Class (liftIO) import Data.Text (Text) import qualified Data.Text as T +import qualified Data.Text.Lazy as TL import qualified Data.Text.Encoding as TE import Data.String (fromString) import Data.Time.Clock (getCurrentTime, diffUTCTime) @@ -109,18 +110,22 @@ validateSignInAction token = do Just signIn -> if signInIsUsed . entityVal $ signIn then - errorResponse "The token has already been used." + redirectError "The token has already been used." else let diffTime = now `diffUTCTime` (signInCreation . entityVal $ signIn) in if diffTime > 2 * 60 -- 2 minutes then - errorResponse "The token has expired." + redirectError "The token has expired." else do LoginSession.put (signInEmail . entityVal $ signIn) liftIO . runDb . signInTokenToUsed . entityKey $ signIn redirect "/" Nothing -> - errorResponse "The token is invalid." + redirectError "The token is invalid." + +redirectError :: Text -> ActionM () +redirectError msg = + redirect . TL.fromStrict . T.concat $ ["/?signInError=", msg] signOutAction :: ActionM () signOutAction = do -- cgit v1.2.3