eidolon/Handler/Reactivate.hs

55 lines
1.8 KiB
Haskell
Raw Normal View History

2014-09-04 01:57:01 +00:00
module Handler.Reactivate where
import Import
import Control.Monad
import Data.Text.Encoding
getReactivateR :: Handler Html
getReactivateR = do
(reactivateWidget, enctype) <- generateFormPost reactivateForm
defaultLayout $ do
2014-09-24 21:03:18 +00:00
setTitle "Eidolon :: Reactivate account"
2014-09-04 01:57:01 +00:00
$(widgetFile "reactivate")
postReactivateR :: Handler Html
postReactivateR = do
2014-12-28 06:12:25 +00:00
((result, _), _) <- runFormPost reactivateForm
2014-09-04 01:57:01 +00:00
case result of
FormSuccess temp -> do
2014-12-08 05:14:26 +00:00
users <- runDB $ selectList [UserEmail ==. temp] []
case null users of
True -> do
2014-09-04 01:57:01 +00:00
userTokens <- foldM (\userTokens (Entity userId user) -> do
token <- liftIO $ generateString
2014-12-28 06:12:25 +00:00
_ <- runDB $ insert $ Token (encodeUtf8 token) "activate" (Just userId)
2014-09-04 01:57:01 +00:00
return $ (user, token) : userTokens
) [] users
2014-12-28 06:12:25 +00:00
_ <- foldM (\sent (user, token) ->
2014-09-04 01:57:01 +00:00
case sent of
False ->
return False
True -> do
activateLink <- ($ ActivateR token) <$> getUrlRender
sendMail (userEmail user) "Reset your password" $
[shamlet|
2014-12-08 05:14:26 +00:00
<h1>Welcome again to Eidolon #{userName user}
2014-09-04 01:57:01 +00:00
To reset your password visit the following link:
<a href="#{activateLink}">#{activateLink}
See you soon!
|]
return True
) True userTokens
2014-12-08 05:14:26 +00:00
setMessage "Your new password activation will arrive in your e-mail"
2014-09-04 01:57:01 +00:00
redirect $ HomeR
2014-12-08 05:14:26 +00:00
False -> do
2014-09-04 01:57:01 +00:00
setMessage "No user mith this Email"
redirect $ LoginR
2014-12-08 05:14:26 +00:00
_ -> do
setMessage "There is something wrong with your email"
redirect $ ReactivateR
2014-09-04 01:57:01 +00:00
reactivateForm :: Form Text
reactivateForm = renderDivs $ (\a -> a)
<$> areq emailField "Email" Nothing