Skip to content

Instantly share code, notes, and snippets.

@asg0451
Created September 24, 2015 05:45
Show Gist options
  • Save asg0451/b094375a5e75ceb4bfcd to your computer and use it in GitHub Desktop.
Save asg0451/b094375a5e75ceb4bfcd to your computer and use it in GitHub Desktop.
example usage of SLS
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import qualified Data.Text.Lazy as T
import Web.Scotty as S
import Web.Scotty.Login.Session
conf :: SessionConfig
conf = defaultSessionConfig
main :: IO ()
main = do
initializeCookieDb conf
scotty 4040 routes
routes :: ScottyM ()
routes = do
S.get "/denied" $ S.text "login denied -- wrong username or password"
S.get "/login" $ do S.html $ T.pack $ unlines $
[ "<form method=\"POST\" action=\"/login\">"
, "<input type=\"text\" name=\"username\">"
, "<input type=\"password\" name=\"password\">"
, "<input type=\"submit\" name=\"login\" value=\"login\">"
, "</form>" ]
S.post "/login" $ do
(usn :: T.Text) <- param "username"
(pass :: T.Text) <- param "password"
res <- addSession conf usn pass
case res of
Nothing -> S.html "login failed"
Just s -> S.html "logged in"
S.get "/authed" $ authCheck Nothing (S.text "denied") $
S.text "authorized"
S.get "/guestsOnlyPage" $ authCheck (Just "guest") (S.html "access denied") $
S.text "you're in"
S.get "/milesOnlyPage" $ authCheck (Just "miles") (S.html "access denied") $
S.text "you're in"
S.get "/users/:user" $ do user <- param "user"
authCheck (Just user) (S.html "access denied") $
S.html "you're in"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment