mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-24 06:54:03 +03:00
Move nested Maybe wrapped in Result to flat Err variant in Session API, and remove obsolete expectSession helper.
This commit is contained in:
parent
b4a603fca8
commit
eb398c30e3
@ -61,8 +61,7 @@ action routeParams =
|
||||
case nameResult of
|
||||
Err errors ->
|
||||
( session
|
||||
|> Result.withDefault Nothing
|
||||
|> Maybe.withDefault Session.empty
|
||||
|> Result.withDefault Session.empty
|
||||
, Response.render
|
||||
{ errors = errors
|
||||
}
|
||||
@ -70,8 +69,7 @@ action routeParams =
|
||||
|
||||
Ok ( _, name ) ->
|
||||
( session
|
||||
|> Result.withDefault Nothing
|
||||
|> Maybe.withDefault Session.empty
|
||||
|> Result.withDefault Session.empty
|
||||
|> Session.insert "name" name
|
||||
|> Session.withFlash "message" ("Welcome " ++ name ++ "!")
|
||||
, Route.redirectTo Route.Greet
|
||||
@ -169,7 +167,7 @@ data routeParams =
|
||||
|> MySession.withSession
|
||||
(\() session ->
|
||||
case session of
|
||||
Ok (Just okSession) ->
|
||||
Ok okSession ->
|
||||
let
|
||||
flashMessage : Maybe String
|
||||
flashMessage =
|
||||
|
@ -18,7 +18,7 @@ cookieOptions =
|
||||
|
||||
|
||||
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 (DataSource (Response data errorPage))
|
||||
withSession =
|
||||
@ -30,7 +30,7 @@ withSession =
|
||||
|
||||
|
||||
withSessionOrRedirect :
|
||||
(request -> Maybe Session.Session -> DataSource ( Session.Session, Response data errorPage ))
|
||||
(request -> Session.Session -> DataSource ( Session.Session, Response data errorPage ))
|
||||
-> Parser request
|
||||
-> Parser (DataSource (Response data errorPage))
|
||||
withSessionOrRedirect toRequest handler =
|
||||
@ -64,9 +64,8 @@ expectSessionOrRedirect toRequest handler =
|
||||
}
|
||||
(\request sessionResult ->
|
||||
sessionResult
|
||||
|> Result.map (Maybe.map (toRequest request))
|
||||
|> Result.withDefault Nothing
|
||||
|> Maybe.withDefault
|
||||
|> Result.map (toRequest request)
|
||||
|> Result.withDefault
|
||||
(DataSource.succeed
|
||||
( Session.empty
|
||||
, Route.redirectTo Route.Login
|
||||
|
@ -204,8 +204,7 @@ data routeParams =
|
||||
okSessionThing : Session
|
||||
okSessionThing =
|
||||
session
|
||||
|> Result.withDefault Nothing
|
||||
|> Maybe.withDefault Session.empty
|
||||
|> Result.withDefault Session.empty
|
||||
|
||||
maybeSessionId : Maybe String
|
||||
maybeSessionId =
|
||||
@ -309,8 +308,7 @@ action routeParams =
|
||||
let
|
||||
okSession =
|
||||
session
|
||||
|> Result.withDefault Nothing
|
||||
|> Maybe.withDefault Session.empty
|
||||
|> Result.withDefault Session.empty
|
||||
in
|
||||
case usernameResult of
|
||||
Err (Form.Response error) ->
|
||||
|
@ -257,15 +257,14 @@ action routeParams =
|
||||
okSession : Session
|
||||
okSession =
|
||||
session
|
||||
|> Result.withDefault Nothing
|
||||
|> Maybe.withDefault Session.empty
|
||||
|> Result.withDefault Session.empty
|
||||
in
|
||||
DataSource.succeed ( okSession, Response.render { errors = Nothing } )
|
||||
)
|
||||
|
||||
|
||||
withUserSession :
|
||||
Result x (Maybe Session)
|
||||
Result x Session
|
||||
-> (Uuid -> DataSource (Response ActionData ErrorPage))
|
||||
-> DataSource ( Session, Response ActionData ErrorPage )
|
||||
withUserSession cookieSession continue =
|
||||
@ -273,8 +272,7 @@ withUserSession cookieSession continue =
|
||||
okSession : Session
|
||||
okSession =
|
||||
cookieSession
|
||||
|> Result.withDefault Nothing
|
||||
|> Maybe.withDefault Session.empty
|
||||
|> Result.withDefault Session.empty
|
||||
in
|
||||
okSession
|
||||
|> Session.get "sessionId"
|
||||
|
@ -18,7 +18,7 @@ cookieOptions =
|
||||
|
||||
|
||||
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 (DataSource (Response data errorPage))
|
||||
withSession =
|
||||
@ -30,7 +30,7 @@ withSession =
|
||||
|
||||
|
||||
withSessionOrRedirect :
|
||||
(request -> Maybe Session.Session -> DataSource ( Session.Session, Response data errorPage ))
|
||||
(request -> Session.Session -> DataSource ( Session.Session, Response data errorPage ))
|
||||
-> Parser request
|
||||
-> Parser (DataSource (Response data errorPage))
|
||||
withSessionOrRedirect toRequest handler =
|
||||
@ -64,9 +64,8 @@ expectSessionOrRedirect toRequest handler =
|
||||
}
|
||||
(\request sessionResult ->
|
||||
sessionResult
|
||||
|> Result.map (Maybe.map (toRequest request))
|
||||
|> Result.withDefault Nothing
|
||||
|> Maybe.withDefault
|
||||
|> Result.map (toRequest request)
|
||||
|> Result.withDefault
|
||||
(DataSource.succeed
|
||||
( Session.empty
|
||||
, Route.redirectTo Route.Login
|
||||
|
@ -1,5 +1,5 @@
|
||||
module Server.Session exposing
|
||||
( withSession, expectSession
|
||||
( withSession
|
||||
, NotLoadedReason(..)
|
||||
, 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 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
|
||||
|
||||
@ -208,8 +208,8 @@ empty =
|
||||
|
||||
{-| -}
|
||||
type NotLoadedReason
|
||||
= NoCookies
|
||||
| MissingHeaders
|
||||
= NoSessionCookie
|
||||
| InvalidSessionCookie
|
||||
|
||||
|
||||
{-| -}
|
||||
@ -239,50 +239,37 @@ flashPrefix =
|
||||
"__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 :
|
||||
{ name : String
|
||||
, secrets : DataSource (List String)
|
||||
, 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 (DataSource (Response data errorPage))
|
||||
withSession config toRequest userRequest =
|
||||
Server.Request.map2
|
||||
(\maybeSessionCookie userRequestData ->
|
||||
let
|
||||
unsigned : DataSource (Result () (Maybe Session))
|
||||
unsigned : DataSource (Result NotLoadedReason Session)
|
||||
unsigned =
|
||||
case maybeSessionCookie of
|
||||
Just sessionCookie ->
|
||||
sessionCookie
|
||||
|> unsignCookie config
|
||||
|> DataSource.map (Result.map Just)
|
||||
|> DataSource.map
|
||||
(\unsignResult ->
|
||||
case unsignResult of
|
||||
Ok decoded ->
|
||||
Ok decoded
|
||||
|
||||
Err () ->
|
||||
Err InvalidSessionCookie
|
||||
)
|
||||
|
||||
Nothing ->
|
||||
Ok Nothing
|
||||
Err NoSessionCookie
|
||||
|> DataSource.succeed
|
||||
in
|
||||
unsigned
|
||||
|
Loading…
Reference in New Issue
Block a user