diff --git a/examples/pokedex/src/Page/Greet.elm b/examples/pokedex/src/Page/Greet.elm index d4f60967..547d3222 100644 --- a/examples/pokedex/src/Page/Greet.elm +++ b/examples/pokedex/src/Page/Greet.elm @@ -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 ) ] diff --git a/examples/pokedex/src/Page/Login.elm b/examples/pokedex/src/Page/Login.elm index d1cd2fbd..f0b32ad0 100644 --- a/examples/pokedex/src/Page/Login.elm +++ b/examples/pokedex/src/Page/Login.elm @@ -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 ) diff --git a/src/Session.elm b/src/Session.elm index 084b0ad6..1e8f2de3 100644 --- a/src/Session.elm +++ b/src/Session.elm @@ -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 ->