Skip to content

Commit

Permalink
add a usernameExists function
Browse files Browse the repository at this point in the history
  • Loading branch information
lightquake committed Oct 19, 2011
1 parent 4583f24 commit 2f03ddc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Snap/Snaplet/Auth.hs
Expand Up @@ -19,6 +19,7 @@ module Snap.Snaplet.Auth


-- * Higher Level Handler Functions -- * Higher Level Handler Functions
createUser createUser
, usernameExists
, saveUser , saveUser
, destroyUser , destroyUser
, loginByUsername , loginByUsername
Expand Down
11 changes: 10 additions & 1 deletion src/Snap/Snaplet/Auth/Handlers.hs
Expand Up @@ -12,6 +12,7 @@


module Snap.Snaplet.Auth.Handlers where module Snap.Snaplet.Auth.Handlers where


import Control.Applicative
import Control.Monad.CatchIO (throw) import Control.Monad.CatchIO (throw)
import Control.Monad.State import Control.Monad.State
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
Expand Down Expand Up @@ -48,6 +49,13 @@ createUser
-> Handler b (AuthManager b) AuthUser -> Handler b (AuthManager b) AuthUser
createUser unm pwd = withBackend (\r -> liftIO $ buildAuthUser r unm pwd) createUser unm pwd = withBackend (\r -> liftIO $ buildAuthUser r unm pwd)


------------------------------------------------------------------------------
-- | Check whether a user with the given username exists.
usernameExists
:: Text
-- ^ The username to be checked
-> Handler b (AuthManager b) Bool
usernameExists username = withBackend $ \r -> liftIO $ isJust <$> lookupByLogin r username


------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- | Lookup a user by her username, check given password and perform login -- | Lookup a user by her username, check given password and perform login
Expand All @@ -63,7 +71,8 @@ loginByUsername unm pwd rm = do
rp <- gets rememberPeriod rp <- gets rememberPeriod
withBackend $ loginByUsername' sk cn rp withBackend $ loginByUsername' sk cn rp
where where
loginByUsername' :: (IAuthBackend t) => Key -> ByteString -> Maybe Int -> t -> Handler b (AuthManager b) (Either AuthFailure AuthUser) loginByUsername' :: (IAuthBackend t) => Key -> ByteString -> Maybe Int -> t
-> Handler b (AuthManager b) (Either AuthFailure AuthUser)
loginByUsername' sk cn rp r = do loginByUsername' sk cn rp r = do
au <- liftIO $ lookupByLogin r (decodeUtf8 unm) au <- liftIO $ lookupByLogin r (decodeUtf8 unm)
case au of case au of
Expand Down

0 comments on commit 2f03ddc

Please sign in to comment.