Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Servant.OAuth2.Examples.Cookies
Description
This example follows the Servant.OAuth2.Examples.Simple example very closely, but this time we use a configuration that let's enables us to set a cookie, and then redirect to the homepage.
Moreover, we set things up so that we can read that cookie on any page, to determine if the current visitor is logged in.
We will assume you have read the Simple example, and mostly spend our time explaining what is different.
Synopsis
- type OAuth2Result = '[WithStatus 303 RedirectWithCookie]
- optionalUserAuthHandler :: Key -> AuthHandler Request (Maybe Text)
- data Routes mode = Routes {
- home :: mode :- (AuthProtect "optional-cookie" :> Get '[HTML] Html)
- auth :: mode :- (AuthProtect Github :> ("auth" :> ("github" :> NamedRoutes (OAuth2Routes OAuth2Result))))
- mkSettings :: Key -> OAuthConfig -> OAuth2Settings Handler Github OAuth2Result
- server :: OAuthConfig -> OAuth2Settings Handler Github OAuth2Result -> Routes (AsServerT Handler)
- main :: IO ()
Documentation
type OAuth2Result = '[WithStatus 303 RedirectWithCookie] Source #
This time our result type is a set of headers that both redirects, and sets a particular cookie value. The cookie will, here, contain simply the result of the oauth2 workflow; i.e. the users email.
Since: 0.1.0.0
optionalUserAuthHandler :: Key -> AuthHandler Request (Maybe Text) Source #
This is the corresponding handler for the above instance. Our
implementation is very simple, we just call getSessionIdFromCookie
, which
is provided by the Servant.OAuth2 library itself; this decodes a
previously-encoded value from the cookie, by the corresponding function
buildSessionCookie
, which we will later use through the
simpleCookieOAuth2Settings
function.
Since: 0.1.0.0
As last time, we have our routes; the main change is the inclusion of the
AuthProtect
tag on the home
route, that let's us bring a potential user
into scope for that page.
Since: 0.1.0.0
Constructors
Routes | |
Fields
|
Instances
Generic (Routes mode) Source # | |
type Rep (Routes mode) Source # | |
Defined in Servant.OAuth2.Examples.Cookies type Rep (Routes mode) = D1 ('MetaData "Routes" "Servant.OAuth2.Examples.Cookies" "servant-oauth2-examples-0.1.0.1-EmzJtQfsyJPG4cNzNv1mEY" 'False) (C1 ('MetaCons "Routes" 'PrefixI 'True) (S1 ('MetaSel ('Just "home") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (mode :- (AuthProtect "optional-cookie" :> Get '[HTML] Html))) :*: S1 ('MetaSel ('Just "auth") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (mode :- (AuthProtect Github :> ("auth" :> ("github" :> NamedRoutes (OAuth2Routes OAuth2Result)))))))) |
mkSettings :: Key -> OAuthConfig -> OAuth2Settings Handler Github OAuth2Result Source #
Again, we have settings, but this time, instead of using the
defaultOAuth2Settings
, we use the simpleCookieOAuth2Settings
function
to get default behaviour that, upon successful completion of the oauth2
flow, builds a cookie with a session id — in this case just the
ident of the user — and then redirects the browser to the homepage.
Since: 0.1.0.0
server :: OAuthConfig -> OAuth2Settings Handler Github OAuth2Result -> Routes (AsServerT Handler) Source #
Our entrypoint; the only addition here is that we need to obtain a Key
to do our cookie encryption/decryption; and we again need to build up our
context with our Github
-based oauth2AuthHandler
and our own custom one,
optionalUserAuthHandler
, to decode the cookie.
Since: 0.1.0.0