Update Session API to pass through existing Session.

This commit is contained in:
Dillon Kearns 2022-01-23 10:43:06 -08:00
parent c42075fad9
commit aff8b1f850
3 changed files with 44 additions and 26 deletions

View File

@ -63,15 +63,15 @@ data routeParams =
username : String username : String
username = username =
okSession okSession
|> Dict.get "name" |> Session.get "name"
|> Maybe.withDefault "NONAME" |> Maybe.withDefault "NONAME"
flashMessage : Maybe String flashMessage : Maybe String
flashMessage = flashMessage =
okSession okSession
|> Dict.get "message" |> Session.get "message"
in in
( Session.noUpdates ( okSession
, { username = username , { username = username
, requestTime = requestTime , requestTime = requestTime
, flashMessage = flashMessage , flashMessage = flashMessage
@ -84,7 +84,7 @@ data routeParams =
|> DataSource.succeed |> DataSource.succeed
_ -> _ ->
( Session.noUpdates, Server.Response.temporaryRedirect "/login" ) ( Session.empty, Server.Response.temporaryRedirect "/login" )
|> DataSource.succeed |> DataSource.succeed
) )
] ]

View File

@ -51,8 +51,11 @@ data routeParams =
[ MySession.withSession [ MySession.withSession
(Request.expectFormPost (\{ field } -> field "name")) (Request.expectFormPost (\{ field } -> field "name"))
(\name session -> (\name session ->
( Session.oneUpdate "name" name ( session
|> Session.withFlash "message" ("Welcome " ++ name ++ "!") |> Result.withDefault Nothing
|> Maybe.withDefault Session.empty
|> Session.insert "name" name
|> Session.withFlash2 "message" ("Welcome " ++ name ++ "!")
, "/greet" , "/greet"
|> Server.Response.temporaryRedirect |> Server.Response.temporaryRedirect
) )
@ -63,20 +66,16 @@ data routeParams =
(\() session -> (\() session ->
case session of case session of
Ok (Just okSession) -> Ok (Just okSession) ->
( Session.oneUpdate "name" ( okSession
(okSession
|> Dict.get "name"
|> Maybe.withDefault "error"
)
, okSession , okSession
|> Dict.get "name" |> Session.get "name"
|> Data |> Data
|> Server.Response.render |> Server.Response.render
) )
|> DataSource.succeed |> DataSource.succeed
_ -> _ ->
( Session.noUpdates ( Session.empty
, { username = Nothing } , { username = Nothing }
|> Server.Response.render |> Server.Response.render
) )

View File

@ -136,10 +136,22 @@ succeed constructor =
|> OptimizedDecoder.succeed |> OptimizedDecoder.succeed
setValues : SessionUpdate -> Dict String String -> Json.Encode.Value setValues : Session -> Json.Encode.Value
setValues (SessionUpdate dict) original = setValues (Session session) =
Dict.union dict original session
|> Dict.toList |> Dict.toList
|> List.filterMap
(\( key, value ) ->
case value of
Persistent string ->
Just ( key, string )
NewFlash string ->
Just ( flashPrefix ++ key, string )
ExpiringFlash _ ->
Nothing
)
|> List.map (Tuple.mapSecond Json.Encode.string) |> List.map (Tuple.mapSecond Json.Encode.string)
|> Json.Encode.object |> Json.Encode.object
@ -164,27 +176,34 @@ withSession :
, sameSite : String , sameSite : String
} }
-> Request request -> Request request
-> (request -> Result String (Maybe (Dict String String)) -> DataSource ( SessionUpdate, Response data )) -> (request -> Result String (Maybe Session) -> DataSource ( Session, Response data ))
-> Request (DataSource (Response data)) -> Request (DataSource (Response data))
withSession config userRequest toRequest = withSession config userRequest toRequest =
Request.map2 Request.map2
(\maybeSessionCookie userRequestData -> (\maybeSessionCookie userRequestData ->
let let
decrypted : DataSource (Result String (Maybe (Dict String String))) decrypted : DataSource (Result String (Maybe Session))
decrypted = decrypted =
case maybeSessionCookie of case maybeSessionCookie of
Just sessionCookie -> Just sessionCookie ->
sessionCookie sessionCookie
|> decrypt config.secrets (OptimizedDecoder.dict OptimizedDecoder.string) |> decrypt config.secrets (OptimizedDecoder.dict OptimizedDecoder.string)
|> DataSource.map |> DataSource.map
(Dict.Extra.mapKeys (\dict ->
(\key -> dict
if key |> String.startsWith flashPrefix then |> Dict.toList
key |> String.dropLeft (flashPrefix |> String.length) |> List.map
(\( key, value ) ->
if key |> String.startsWith flashPrefix then
( key |> String.dropLeft (flashPrefix |> String.length)
, ExpiringFlash value
)
else else
key ( key, Persistent value )
) )
|> Dict.fromList
|> Session
) )
|> DataSource.map (Just >> Ok) |> DataSource.map (Just >> Ok)
@ -215,7 +234,7 @@ withSession config userRequest toRequest =
let let
encodedCookie : Json.Encode.Value encodedCookie : Json.Encode.Value
encodedCookie = encodedCookie =
setValues sessionUpdate (cookieDict |> clearFlashCookies) setValues sessionUpdate
in in
DataSource.map2 DataSource.map2
(\encoded originalCookieValues -> (\encoded originalCookieValues ->