Move nested Maybe wrapped in Result to flat Err variant in Session API, and remove obsolete expectSession helper.

This commit is contained in:
Dillon Kearns 2022-10-06 09:50:27 -07:00
parent b4a603fca8
commit eb398c30e3
7 changed files with 33 additions and 54 deletions

File diff suppressed because one or more lines are too long

View File

@ -61,8 +61,7 @@ action routeParams =
case nameResult of case nameResult of
Err errors -> Err errors ->
( session ( session
|> Result.withDefault Nothing |> Result.withDefault Session.empty
|> Maybe.withDefault Session.empty
, Response.render , Response.render
{ errors = errors { errors = errors
} }
@ -70,8 +69,7 @@ action routeParams =
Ok ( _, name ) -> Ok ( _, name ) ->
( session ( session
|> Result.withDefault Nothing |> Result.withDefault Session.empty
|> Maybe.withDefault Session.empty
|> Session.insert "name" name |> Session.insert "name" name
|> Session.withFlash "message" ("Welcome " ++ name ++ "!") |> Session.withFlash "message" ("Welcome " ++ name ++ "!")
, Route.redirectTo Route.Greet , Route.redirectTo Route.Greet
@ -169,7 +167,7 @@ data routeParams =
|> MySession.withSession |> MySession.withSession
(\() session -> (\() session ->
case session of case session of
Ok (Just okSession) -> Ok okSession ->
let let
flashMessage : Maybe String flashMessage : Maybe String
flashMessage = flashMessage =

View File

@ -18,7 +18,7 @@ cookieOptions =
withSession : withSession :
(request -> Result () (Maybe Session.Session) -> DataSource ( Session.Session, Response data errorPage )) (request -> Result Session.NotLoadedReason Session.Session -> DataSource ( Session.Session, Response data errorPage ))
-> Parser request -> Parser request
-> Parser (DataSource (Response data errorPage)) -> Parser (DataSource (Response data errorPage))
withSession = withSession =
@ -30,7 +30,7 @@ withSession =
withSessionOrRedirect : withSessionOrRedirect :
(request -> Maybe Session.Session -> DataSource ( Session.Session, Response data errorPage )) (request -> Session.Session -> DataSource ( Session.Session, Response data errorPage ))
-> Parser request -> Parser request
-> Parser (DataSource (Response data errorPage)) -> Parser (DataSource (Response data errorPage))
withSessionOrRedirect toRequest handler = withSessionOrRedirect toRequest handler =
@ -64,9 +64,8 @@ expectSessionOrRedirect toRequest handler =
} }
(\request sessionResult -> (\request sessionResult ->
sessionResult sessionResult
|> Result.map (Maybe.map (toRequest request)) |> Result.map (toRequest request)
|> Result.withDefault Nothing |> Result.withDefault
|> Maybe.withDefault
(DataSource.succeed (DataSource.succeed
( Session.empty ( Session.empty
, Route.redirectTo Route.Login , Route.redirectTo Route.Login

View File

@ -204,8 +204,7 @@ data routeParams =
okSessionThing : Session okSessionThing : Session
okSessionThing = okSessionThing =
session session
|> Result.withDefault Nothing |> Result.withDefault Session.empty
|> Maybe.withDefault Session.empty
maybeSessionId : Maybe String maybeSessionId : Maybe String
maybeSessionId = maybeSessionId =
@ -309,8 +308,7 @@ action routeParams =
let let
okSession = okSession =
session session
|> Result.withDefault Nothing |> Result.withDefault Session.empty
|> Maybe.withDefault Session.empty
in in
case usernameResult of case usernameResult of
Err (Form.Response error) -> Err (Form.Response error) ->

View File

@ -257,15 +257,14 @@ action routeParams =
okSession : Session okSession : Session
okSession = okSession =
session session
|> Result.withDefault Nothing |> Result.withDefault Session.empty
|> Maybe.withDefault Session.empty
in in
DataSource.succeed ( okSession, Response.render { errors = Nothing } ) DataSource.succeed ( okSession, Response.render { errors = Nothing } )
) )
withUserSession : withUserSession :
Result x (Maybe Session) Result x Session
-> (Uuid -> DataSource (Response ActionData ErrorPage)) -> (Uuid -> DataSource (Response ActionData ErrorPage))
-> DataSource ( Session, Response ActionData ErrorPage ) -> DataSource ( Session, Response ActionData ErrorPage )
withUserSession cookieSession continue = withUserSession cookieSession continue =
@ -273,8 +272,7 @@ withUserSession cookieSession continue =
okSession : Session okSession : Session
okSession = okSession =
cookieSession cookieSession
|> Result.withDefault Nothing |> Result.withDefault Session.empty
|> Maybe.withDefault Session.empty
in in
okSession okSession
|> Session.get "sessionId" |> Session.get "sessionId"

View File

@ -18,7 +18,7 @@ cookieOptions =
withSession : withSession :
(request -> Result () (Maybe Session.Session) -> DataSource ( Session.Session, Response data errorPage )) (request -> Result Session.NotLoadedReason Session.Session -> DataSource ( Session.Session, Response data errorPage ))
-> Parser request -> Parser request
-> Parser (DataSource (Response data errorPage)) -> Parser (DataSource (Response data errorPage))
withSession = withSession =
@ -30,7 +30,7 @@ withSession =
withSessionOrRedirect : withSessionOrRedirect :
(request -> Maybe Session.Session -> DataSource ( Session.Session, Response data errorPage )) (request -> Session.Session -> DataSource ( Session.Session, Response data errorPage ))
-> Parser request -> Parser request
-> Parser (DataSource (Response data errorPage)) -> Parser (DataSource (Response data errorPage))
withSessionOrRedirect toRequest handler = withSessionOrRedirect toRequest handler =
@ -64,9 +64,8 @@ expectSessionOrRedirect toRequest handler =
} }
(\request sessionResult -> (\request sessionResult ->
sessionResult sessionResult
|> Result.map (Maybe.map (toRequest request)) |> Result.map (toRequest request)
|> Result.withDefault Nothing |> Result.withDefault
|> Maybe.withDefault
(DataSource.succeed (DataSource.succeed
( Session.empty ( Session.empty
, Route.redirectTo Route.Login , Route.redirectTo Route.Login

View File

@ -1,5 +1,5 @@
module Server.Session exposing module Server.Session exposing
( withSession, expectSession ( withSession
, NotLoadedReason(..) , NotLoadedReason(..)
, Session, empty, get, insert, remove, update, withFlash , Session, empty, get, insert, remove, update, withFlash
) )
@ -96,7 +96,7 @@ And then a user makes a request but had a session signed with our old secret (`2
(so `withSession` would parse the session for that request as `Nothing`). It's standard for cookies to have an expiration date, (so `withSession` would parse the session for that request as `Nothing`). It's standard for cookies to have an expiration date,
so there's nothing wrong with an old session expiring (and the browser will eventually delete old cookies), just be aware of that when rotating secrets. so there's nothing wrong with an old session expiring (and the browser will eventually delete old cookies), just be aware of that when rotating secrets.
@docs withSession, expectSession @docs withSession
@docs NotLoadedReason @docs NotLoadedReason
@ -208,8 +208,8 @@ empty =
{-| -} {-| -}
type NotLoadedReason type NotLoadedReason
= NoCookies = NoSessionCookie
| MissingHeaders | InvalidSessionCookie
{-| -} {-| -}
@ -239,50 +239,37 @@ flashPrefix =
"__flash__" "__flash__"
{-| -}
expectSession :
{ name : String
, secrets : DataSource (List String)
, options : SetCookie.Options
}
-> Server.Request.Parser request
-> (request -> Result () Session -> DataSource ( Session, Response data errorPage ))
-> Server.Request.Parser (DataSource (Response data errorPage))
expectSession config userRequest toRequest =
Server.Request.map2
(\sessionCookie userRequestData ->
sessionCookie
|> unsignCookie config
|> DataSource.andThen
(encodeSessionUpdate config toRequest userRequestData)
)
(Server.Request.expectCookie config.name)
userRequest
{-| -} {-| -}
withSession : withSession :
{ name : String { name : String
, secrets : DataSource (List String) , secrets : DataSource (List String)
, options : SetCookie.Options , options : SetCookie.Options
} }
-> (request -> Result () (Maybe Session) -> DataSource ( Session, Response data errorPage )) -> (request -> Result NotLoadedReason Session -> DataSource ( Session, Response data errorPage ))
-> Server.Request.Parser request -> Server.Request.Parser request
-> Server.Request.Parser (DataSource (Response data errorPage)) -> Server.Request.Parser (DataSource (Response data errorPage))
withSession config toRequest userRequest = withSession config toRequest userRequest =
Server.Request.map2 Server.Request.map2
(\maybeSessionCookie userRequestData -> (\maybeSessionCookie userRequestData ->
let let
unsigned : DataSource (Result () (Maybe Session)) unsigned : DataSource (Result NotLoadedReason Session)
unsigned = unsigned =
case maybeSessionCookie of case maybeSessionCookie of
Just sessionCookie -> Just sessionCookie ->
sessionCookie sessionCookie
|> unsignCookie config |> unsignCookie config
|> DataSource.map (Result.map Just) |> DataSource.map
(\unsignResult ->
case unsignResult of
Ok decoded ->
Ok decoded
Err () ->
Err InvalidSessionCookie
)
Nothing -> Nothing ->
Ok Nothing Err NoSessionCookie
|> DataSource.succeed |> DataSource.succeed
in in
unsigned unsigned