-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathAuthentication.purs
45 lines (39 loc) · 1.42 KB
/
Authentication.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Examples.Authentication where
import Prelude
import Control.Monad.Indexed.Qualified as Ix
import Effect.Aff (Aff)
import Effect (Effect)
import Data.Maybe (Maybe(Just, Nothing))
import Data.MediaType.Common (textHTML)
import Data.Tuple (Tuple(Tuple))
import Hyper.Middleware.Class (getConn)
import Hyper.Node.BasicAuth as BasicAuth
import Hyper.Node.Server (defaultOptionsWithLogging, runServer)
import Hyper.Response (closeHeaders, contentType, respond, writeStatus)
import Hyper.Status (statusOK)
import Text.Smolder.HTML (p)
import Text.Smolder.Markup (text)
import Text.Smolder.Renderer.String (render)
data User = User String
-- This could be a function checking the username/password in a database.
userFromBasicAuth :: Tuple String String -> Aff (Maybe User)
userFromBasicAuth =
case _ of
Tuple "admin" "admin" -> pure (Just (User "Administrator"))
_ -> pure Nothing
main :: Effect Unit
main =
let
myProfilePage = Ix.do
conn <- getConn
case conn.components.authentication of
User name → Ix.do
writeStatus statusOK
contentType textHTML
closeHeaders
respond (render (p (text ("You are authenticated as " <> name <> "."))))
app = Ix.do
BasicAuth.withAuthentication userFromBasicAuth
BasicAuth.authenticated "Authentication Example" myProfilePage
components = { authentication: unit }
in runServer defaultOptionsWithLogging components app