Skip to content

Instantly share code, notes, and snippets.

@adinapoli
Created October 1, 2012 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adinapoli/3811605 to your computer and use it in GitHub Desktop.
Save adinapoli/3811605 to your computer and use it in GitHub Desktop.
First attempt
loginUser
:: ByteString
-- ^ Username field
-> ByteString
-- ^ Password field
-> Maybe ByteString
-- ^ Remember field; Nothing if you want no remember function.
-> (AuthFailure -> Handler b (AuthManager b) ())
-- ^ Upon failure
-> Handler b (AuthManager b) ()
-- ^ Upon success
-> Handler b (AuthManager b) ()
loginUser unf pwdf remf loginFail loginSucc =
runEitherT (loginUser' unf pwdf remf)
>>= either loginFail (const loginSucc)
loginUser' :: ByteString
-> ByteString
-> Maybe ByteString
-> EitherT AuthFailure (Handler b (AuthManager b)) AuthUser
loginUser' unf pwdf remf = do
mbUsername <- lift $ getParam unf
mbPassword <- lift $ getParam pwdf
remember <- lift $ liftM (fromMaybe False)
(runMaybeT $
do field <- MaybeT $ return remf
value <- MaybeT $ getParam field
return $ value == "1")
password <- maybe (left PasswordMissing) return mbPassword
username <- maybe (left UsernameMissing) return mbUsername
-- Lifting already get the "success" value for me,
-- and store it inside loginStatus
loginStatus <- lift $ loginByUsername username (ClearText password) remember
EitherT $ return loginStatus
@adinapoli
Copy link
Author

I used left for the maybe trick:

left :: l -> EitherT l m a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment