Question

Since I have very few Haskell experience I'm really having a hard time to wrap my head around Snap. As an introductory I'm trying to make the example that comes with the framework check for user existence. Can someone tell me how to adapt the function below to get me started? This is what I get now. After checking if the username exists how do I continue?

------------------------------------------------------------------------------
-- | Handle new user form submit
handleNewUser :: Handler App (AuthManager App) ()
handleNewUser = method GET handleForm <|> method POST handleFormSubmit
  where
    handleForm = render "new_user"
    handleFormSubmit = do result <- usernameExists "login"
                          result <- createUser "login" "password" >> redirect "/"
                          return result
Was it helpful?

Solution

Try something like this. Haven't checked it in ghc but should give you the idea.

handleFormSubmit = do
    exists <- usernameExists "someUserName"
    case exists of
        True    -> writeBS "The username already exists"
        False   -> do
            createUser "someUserName" "password"
            redirect' "/" 301

You can't return a value after using redirect because that routes the client to a new page. Since createUser throws a DuplicateLogin error if the user already exists it may be better to just catch that error than doing a check because it is still possible between the check and the insert the same username got inserted elsewhere. Unlikely but possible. Also, you should use redirect' because this signifies a permanent redirect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top