mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-25 04:43:03 +03:00
Update Session API to pass through existing Session.
This commit is contained in:
parent
c42075fad9
commit
aff8b1f850
@ -63,15 +63,15 @@ data routeParams =
|
||||
username : String
|
||||
username =
|
||||
okSession
|
||||
|> Dict.get "name"
|
||||
|> Session.get "name"
|
||||
|> Maybe.withDefault "NONAME"
|
||||
|
||||
flashMessage : Maybe String
|
||||
flashMessage =
|
||||
okSession
|
||||
|> Dict.get "message"
|
||||
|> Session.get "message"
|
||||
in
|
||||
( Session.noUpdates
|
||||
( okSession
|
||||
, { username = username
|
||||
, requestTime = requestTime
|
||||
, flashMessage = flashMessage
|
||||
@ -84,7 +84,7 @@ data routeParams =
|
||||
|> DataSource.succeed
|
||||
|
||||
_ ->
|
||||
( Session.noUpdates, Server.Response.temporaryRedirect "/login" )
|
||||
( Session.empty, Server.Response.temporaryRedirect "/login" )
|
||||
|> DataSource.succeed
|
||||
)
|
||||
]
|
||||
|
@ -51,8 +51,11 @@ data routeParams =
|
||||
[ MySession.withSession
|
||||
(Request.expectFormPost (\{ field } -> field "name"))
|
||||
(\name session ->
|
||||
( Session.oneUpdate "name" name
|
||||
|> Session.withFlash "message" ("Welcome " ++ name ++ "!")
|
||||
( session
|
||||
|> Result.withDefault Nothing
|
||||
|> Maybe.withDefault Session.empty
|
||||
|> Session.insert "name" name
|
||||
|> Session.withFlash2 "message" ("Welcome " ++ name ++ "!")
|
||||
, "/greet"
|
||||
|> Server.Response.temporaryRedirect
|
||||
)
|
||||
@ -63,20 +66,16 @@ data routeParams =
|
||||
(\() session ->
|
||||
case session of
|
||||
Ok (Just okSession) ->
|
||||
( Session.oneUpdate "name"
|
||||
(okSession
|
||||
|> Dict.get "name"
|
||||
|> Maybe.withDefault "error"
|
||||
)
|
||||
( okSession
|
||||
, okSession
|
||||
|> Dict.get "name"
|
||||
|> Session.get "name"
|
||||
|> Data
|
||||
|> Server.Response.render
|
||||
)
|
||||
|> DataSource.succeed
|
||||
|
||||
_ ->
|
||||
( Session.noUpdates
|
||||
( Session.empty
|
||||
, { username = Nothing }
|
||||
|> Server.Response.render
|
||||
)
|
||||
|
@ -136,10 +136,22 @@ succeed constructor =
|
||||
|> OptimizedDecoder.succeed
|
||||
|
||||
|
||||
setValues : SessionUpdate -> Dict String String -> Json.Encode.Value
|
||||
setValues (SessionUpdate dict) original =
|
||||
Dict.union dict original
|
||||
setValues : Session -> Json.Encode.Value
|
||||
setValues (Session session) =
|
||||
session
|
||||
|> 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)
|
||||
|> Json.Encode.object
|
||||
|
||||
@ -164,27 +176,34 @@ withSession :
|
||||
, sameSite : String
|
||||
}
|
||||
-> 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))
|
||||
withSession config userRequest toRequest =
|
||||
Request.map2
|
||||
(\maybeSessionCookie userRequestData ->
|
||||
let
|
||||
decrypted : DataSource (Result String (Maybe (Dict String String)))
|
||||
decrypted : DataSource (Result String (Maybe Session))
|
||||
decrypted =
|
||||
case maybeSessionCookie of
|
||||
Just sessionCookie ->
|
||||
sessionCookie
|
||||
|> decrypt config.secrets (OptimizedDecoder.dict OptimizedDecoder.string)
|
||||
|> DataSource.map
|
||||
(Dict.Extra.mapKeys
|
||||
(\key ->
|
||||
if key |> String.startsWith flashPrefix then
|
||||
key |> String.dropLeft (flashPrefix |> String.length)
|
||||
(\dict ->
|
||||
dict
|
||||
|> Dict.toList
|
||||
|> List.map
|
||||
(\( key, value ) ->
|
||||
if key |> String.startsWith flashPrefix then
|
||||
( key |> String.dropLeft (flashPrefix |> String.length)
|
||||
, ExpiringFlash value
|
||||
)
|
||||
|
||||
else
|
||||
key
|
||||
)
|
||||
else
|
||||
( key, Persistent value )
|
||||
)
|
||||
|> Dict.fromList
|
||||
|> Session
|
||||
)
|
||||
|> DataSource.map (Just >> Ok)
|
||||
|
||||
@ -215,7 +234,7 @@ withSession config userRequest toRequest =
|
||||
let
|
||||
encodedCookie : Json.Encode.Value
|
||||
encodedCookie =
|
||||
setValues sessionUpdate (cookieDict |> clearFlashCookies)
|
||||
setValues sessionUpdate
|
||||
in
|
||||
DataSource.map2
|
||||
(\encoded originalCookieValues ->
|
||||
|
Loading…
Reference in New Issue
Block a user