Actually I'm using a Yesod application which use their plugin system for authentication.
Here is some working code:
~~~~
import Data.CaseInsensitive (CI)
identityHeaderName :: CI ByteString
identityHeaderName = "Yesod-Identity"
addQueryId :: Maybe ByteString -> W.Request -> W.Request
addQueryId identity req =
let f = maybe id (\x hs -> (identityHeaderName,x):hs) identity
in req {W.requestHeaders = f (W.requestHeaders req)}
removeIdentityQuery :: W.Request -> W.Request
removeIdentityQuery req = req {
W.requestHeaders = filter ((/= identityHeaderName) . fst)
(W.requestHeaders req)
}
addIdentity :: Maybe ByteString -> Middleware
addIdentity identity app req sendResp =
let req' = removeIdentityQuery req
req'' = addQueryId identity req'
in
app req'' sendResp
addIdentity :: Maybe ByteString -> Middleware
addIdentity identity app req sendResp =
let req' = removeIdentityQuery req
req'' = addQueryId identity req'
in
app req'' sendResp
followToApp :: Handler TypedContent
followToApp = do
app <- getYesod
auth <- maybeAuth
let identity = case auth of
Just (Entity _ u) -> Just (encodeUtf8 (userIdent u))
Nothing -> Nothing
print identity
sendWaiApplication (addIdentity identity (appREST app))
getHomeR :: Texts -> Handler TypedContent
getHomeR _ = followToApp
postHomeR :: Texts -> Handler TypedContent
postHomeR _ = followToApp
~~~~
and I had to put this in config/routes:
~~~
!/*Texts HomeR GET POST
~~~
The `appREST` is the WAI application returned by Servant that I initialise in Foundation.hs.
I added `appREST` field of type Application in the App record.