Implement Login via JSON endpoint

Add additional handling of JSON endpoint in addition to the HTML form
method.
This commit is contained in:
Sibi Prabakaran 2016-12-06 15:20:51 +05:30
parent 19840cdc89
commit b6cd72f49f
No known key found for this signature in database
GPG Key ID: D19E3E0EBB557613

View File

@ -512,11 +512,28 @@ $newline never
|] |]
parseCreds :: Value -> Parser (Text, Text)
parseCreds = withObject "creds" (\obj -> do
email' <- obj .: "email"
pass <- obj .: "password"
return (email', pass))
postLoginR :: YesodAuthEmail master => HandlerT Auth (HandlerT master IO) TypedContent postLoginR :: YesodAuthEmail master => HandlerT Auth (HandlerT master IO) TypedContent
postLoginR = do postLoginR = do
(identifier, pass) <- lift $ runInputPost $ (,) result <- lift $ runInputPostResult $ (,)
<$> ireq textField "email" <$> ireq textField "email"
<*> ireq textField "password" <*> ireq textField "password"
(creds :: Result Value) <- lift parseJsonBody
let midentifier = case result of
FormSuccess (iden, pass) -> Just (iden, pass)
_ -> case creds of
Error _ -> Nothing
Success val -> parseMaybe parseCreds val
case midentifier of
Nothing -> loginErrorMessageI LoginR Msg.NoIdentifierProvided
Just (identifier, pass) -> do
mecreds <- lift $ getEmailCreds identifier mecreds <- lift $ getEmailCreds identifier
maid <- maid <-
case ( mecreds >>= emailCredsAuthId case ( mecreds >>= emailCredsAuthId
@ -527,8 +544,7 @@ postLoginR = do
mrealpass <- lift $ getPassword aid mrealpass <- lift $ getPassword aid
case mrealpass of case mrealpass of
Nothing -> return Nothing Nothing -> return Nothing
Just realpass -> return $ Just realpass -> return $ if isValidPass pass realpass
if isValidPass pass realpass
then Just email then Just email
else Nothing else Nothing
_ -> return Nothing _ -> return Nothing