| Copyright | (c) 2014 2015 Mārtiņš Mačs (c) 2023 Marco Zocca |
|---|---|
| License | BSD-3-Clause |
| Maintainer | |
| Stability | experimental |
| Portability | GHC |
| Safe Haskell | None |
| Language | Haskell2010 |
Web.Scotty.Cookie
Description
This module provides utilities for adding cookie support inside scotty applications. Most code has been adapted from 'scotty-cookie'.
Example
A simple hit counter that stores the number of page visits in a cookie:
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import Data.Monoid
import Data.Maybe
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Read as TL (decimal)
import Web.Scotty (scotty, html)
import Web.Scotty.Cookie (getCookie, setSimpleCookie)
main :: IO ()
main = scotty 3000 $
get "/" $ do
hits <- liftM (fromMaybe "0") $ getCookie "hits"
let hits' =
case TL.decimal hits of
Right n -> TL.pack . show . (+1) $ (fst n :: Integer)
Left _ -> "1"
setSimpleCookie "hits" $ TL.toStrict hits'
html $ mconcat [ "<html><body>"
, hits'
, "</body></html>"
]
Synopsis
- setCookie :: forall (m :: Type -> Type). MonadIO m => SetCookie -> ActionT m ()
- setSimpleCookie :: forall (m :: Type -> Type). MonadIO m => Text -> Text -> ActionT m ()
- getCookie :: forall (m :: Type -> Type). Monad m => Text -> ActionT m (Maybe Text)
- getCookies :: forall (m :: Type -> Type). Monad m => ActionT m CookiesText
- deleteCookie :: forall (m :: Type -> Type). MonadIO m => Text -> ActionT m ()
- type CookiesText = [(Text, Text)]
- makeSimpleCookie :: Text -> Text -> SetCookie
- data SetCookie
- defaultSetCookie :: SetCookie
- setCookieName :: SetCookie -> ByteString
- setCookieValue :: SetCookie -> ByteString
- setCookiePath :: SetCookie -> Maybe ByteString
- setCookieExpires :: SetCookie -> Maybe UTCTime
- setCookieMaxAge :: SetCookie -> Maybe DiffTime
- setCookieDomain :: SetCookie -> Maybe ByteString
- setCookieHttpOnly :: SetCookie -> Bool
- setCookieSecure :: SetCookie -> Bool
- setCookieSameSite :: SetCookie -> Maybe SameSiteOption
- data SameSiteOption
- sameSiteNone :: SameSiteOption
- sameSiteLax :: SameSiteOption
- sameSiteStrict :: SameSiteOption
Set cookie
setCookie :: forall (m :: Type -> Type). MonadIO m => SetCookie -> ActionT m () Source #
Set a cookie, with full access to its options (see SetCookie)
Arguments
| :: forall (m :: Type -> Type). MonadIO m | |
| => Text | name |
| -> Text | value |
| -> ActionT m () |
makeSimpleCookie and setCookie combined.
Get cookie(s)
Arguments
| :: forall (m :: Type -> Type). Monad m | |
| => Text | name |
| -> ActionT m (Maybe Text) |
Lookup one cookie name
getCookies :: forall (m :: Type -> Type). Monad m => ActionT m CookiesText Source #
Returns all cookies
Delete a cookie
Arguments
| :: forall (m :: Type -> Type). MonadIO m | |
| => Text | name |
| -> ActionT m () |
Browsers don't directly delete a cookie, but setting its expiry to a past date (e.g. the UNIX epoch) ensures that the cookie will be invalidated (whether and when it will be actually deleted by the browser seems to be browser-dependent).
Helpers and advanced interface (re-exported from cookie)
type CookiesText = [(Text, Text)] #
Arguments
| :: Text | name |
| -> Text | value |
| -> SetCookie |
Construct a simple cookie (an UTF-8 string pair with default cookie options)
cookie configuration
Instances
| Default SetCookie | |
Defined in Web.Cookie | |
| NFData SetCookie | |
Defined in Web.Cookie | |
| Show SetCookie | |
| Eq SetCookie | |
setCookieName :: SetCookie -> ByteString #
setCookieValue :: SetCookie -> ByteString #
setCookiePath :: SetCookie -> Maybe ByteString #
setCookieExpires :: SetCookie -> Maybe UTCTime #
setCookieMaxAge :: SetCookie -> Maybe DiffTime #
setCookieDomain :: SetCookie -> Maybe ByteString #
setCookieHttpOnly :: SetCookie -> Bool #
setCookieSecure :: SetCookie -> Bool #
setCookieSameSite :: SetCookie -> Maybe SameSiteOption #
data SameSiteOption #
Instances
| NFData SameSiteOption | |
Defined in Web.Cookie Methods rnf :: SameSiteOption -> () | |
| Show SameSiteOption | |
Defined in Web.Cookie Methods showsPrec :: Int -> SameSiteOption -> ShowS show :: SameSiteOption -> String showList :: [SameSiteOption] -> ShowS | |
| Eq SameSiteOption | |
Defined in Web.Cookie Methods (==) :: SameSiteOption -> SameSiteOption -> Bool (/=) :: SameSiteOption -> SameSiteOption -> Bool | |