Skip to content
This repository
Browse code

Add "loggedInUser" pre-packaged splice for Auth

  • Loading branch information...
commit 7fc38a58fd9c1f70310364b860b5b2fd323ca225 1 parent 0505fe5
Ozgun Ataman ozataman authored
1  src/Snap/Snaplet/Auth.hs
@@ -65,6 +65,7 @@ module Snap.Snaplet.Auth
65 65 , addAuthSplices
66 66 , ifLoggedIn
67 67 , ifLoggedOut
  68 + , loggedInUser
68 69 )
69 70 where
70 71
19 src/Snap/Snaplet/Auth/SpliceHelpers.hs
@@ -15,6 +15,7 @@ module Snap.Snaplet.Auth.SpliceHelpers
15 15 addAuthSplices
16 16 , ifLoggedIn
17 17 , ifLoggedOut
  18 + , loggedInUser
18 19 ) where
19 20
20 21 import Data.Lens.Lazy
@@ -24,6 +25,7 @@ import Text.Templating.Heist
24 25 import Snap.Snaplet
25 26 import Snap.Snaplet.Auth.AuthManager
26 27 import Snap.Snaplet.Auth.Handlers
  28 +import Snap.Snaplet.Auth.Types
27 29 import Snap.Snaplet.Heist
28 30
29 31
@@ -41,6 +43,7 @@ addAuthSplices
41 43 addAuthSplices auth = addSplices
42 44 [ ("ifLoggedIn", ifLoggedIn auth)
43 45 , ("ifLoggedOut", ifLoggedOut auth)
  46 + , ("loggedInUser", loggedInUser auth)
44 47 ]
45 48
46 49
@@ -49,9 +52,7 @@ addAuthSplices auth = addSplices
49 52 -- present, this will run the contents of the node.
50 53 --
51 54 -- > <ifLoggedIn> Show this when there is a logged in user </ifLoggedIn>
52   -ifLoggedIn
53   - :: Lens b (Snaplet (AuthManager b))
54   - -> SnapletSplice b v
  55 +ifLoggedIn :: Lens b (Snaplet (AuthManager b)) -> SnapletSplice b v
55 56 ifLoggedIn auth = do
56 57 chk <- liftHandler $ withTop auth isLoggedIn
57 58 case chk of
@@ -64,12 +65,18 @@ ifLoggedIn auth = do
64 65 -- not present, this will run the contents of the node.
65 66 --
66 67 -- > <ifLoggedOut> Show this when there is a logged in user </ifLoggedOut>
67   -ifLoggedOut
68   - :: Lens b (Snaplet (AuthManager b))
69   - -> SnapletSplice b v
  68 +ifLoggedOut :: Lens b (Snaplet (AuthManager b)) -> SnapletSplice b v
70 69 ifLoggedOut auth = do
71 70 chk <- liftHandler $ withTop auth isLoggedIn
72 71 case chk of
73 72 False -> liftHeist $ getParamNode >>= return . X.childNodes
74 73 True -> return []
75 74
  75 +
  76 +-------------------------------------------------------------------------------
  77 +-- | A splice that will simply print the current user's login, if
  78 +-- there is one.
  79 +loggedInUser :: Lens b (Snaplet (AuthManager b)) -> SnapletSplice b v
  80 +loggedInUser auth = do
  81 + u <- liftHandler $ withTop auth currentUser
  82 + liftHeist $ maybe (return []) (textSplice . userLogin) u

0 comments on commit 7fc38a5

Please sign in to comment.
Something went wrong with that request. Please try again.