Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/drewolson/purescript-httpure-contrib-biscotti
Helpers for using Biscotti Cookies and Sessions with HTTPure applications
https://github.com/drewolson/purescript-httpure-contrib-biscotti
Last synced: about 14 hours ago
JSON representation
Helpers for using Biscotti Cookies and Sessions with HTTPure applications
- Host: GitHub
- URL: https://github.com/drewolson/purescript-httpure-contrib-biscotti
- Owner: drewolson
- Created: 2019-09-01T16:33:28.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-05T22:44:34.000Z (almost 2 years ago)
- Last Synced: 2024-12-11T06:19:39.309Z (about 1 month ago)
- Language: PureScript
- Size: 167 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# purescript-httpure-contrib-biscotti
[![Build
Status](https://github.com/drewolson/purescript-httpure-contrib-biscotti/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/drewolson/purescript-httpure-contrib-biscotti/actions/workflows/test.yml)Documentation is on
[Pursuit](https://pursuit.purescript.org/packages/purescript-httpure-contrib-biscotti).This library provides a middleware that uses
[Biscotti.Cookie](https://github.com/drewolson/purescript-biscotti-cookie) and
[Biscotti.Session](https://github.com/drewolson/purescript-biscotti-session) to
add session handling to an
[HTTPure](https://github.com/cprussin/purescript-httpure) application. The
`Biscotti.middleware` function requires a session store and then behaves similar
to other `HTTPure` middleware.Suppose the type of your session is:
```purescript
type Session = { currentUser :: String }
```Note that _any_ type which implements `EncodeJson` and `DecodeJson` from
[Argonaut](https://github.com/purescript-contrib/purescript-argonaut) can be
used as your session type.The primary difference from other middleware is that `Biscotti.middleware`
expects the next middleware in the stack to receive a `Maybe Session` along with the
`HTTPure.Request`. It expects the return type to be an `MonadAff m => m (Tuple
HTTPure.Response (Maybe Session))` rather than just a `m HTTPure.Response`.If you return `Nothing` for your `Session`, your session will be destroyed. If
you return a `Just Session`, your session will be created if it doesn't
currently exist, otherwise it will be updated.Here's a small example application:
```purescript
import Biscotti.Session as Session
import HTTPure.Contrib.Biscotti as Biscottitype Session = { currentUser :: String }
router :: Maybe Session -> HTTPure.Request -> Aff (Tuple HTTPure.Response (Maybe Session))
router session req = do
case req of
{ path: ["login"] } -> do
response <- HTTPure.ok "login"
pure $ Tuple response (Just { currentUser: "Drew" }){ path: ["logout"] } -> do
response <- HTTPure.ok "logout"
pure $ Tuple response Nothing_ -> do
response <- HTTPure.ok "hello"
pure $ Tuple response sessionmain :: Effect Unit
main = launchAff_ do
let cookieName = "_test"
store <- liftEffect $ Session.memoryStore cookieName
let middleware = Biscotti.middleware cookieName storeliftEffect $ HTTPure.server (middleware router) do
log "Server running"
```In a larger application, you may prefer to have another layer of middleware that
places the `Session` in a `StateT` to make it easier to access and modify within
your router.