Implement /register

This commit is contained in:
Pere Lev 2024-07-06 00:14:36 +03:00
parent 19194426c2
commit b8922b3157
No known key found for this signature in database
GPG key ID: 5252C5C863E5E57D
3 changed files with 49 additions and 1 deletions

View file

@ -243,6 +243,7 @@ instance Yesod App where
Just (ProjectInboxR _) -> return False
Just (GitUploadRequestR _) -> return False
Just (DvaraR _) -> return False
Just RegisterR -> return False
Just r -> isWriteRequest r
)
defaultCsrfHeaderName
@ -851,6 +852,7 @@ instance YesodBreadcrumbs App where
ActorKey2R -> ("Actor Key 2", Just HomeR)
RegisterEnabledR -> ("", Nothing)
RegisterAvailableR -> ("", Nothing)
RegisterR -> ("", Nothing)
HomeR -> ("Home", Nothing)
BrowseR -> ("Browse", Just HomeR)

View file

@ -20,6 +20,7 @@ module Vervis.Handler.Client
, getActorKey2R
, getRegisterEnabledR
, getRegisterAvailableR
, postRegisterR
, getHomeR
, getBrowseR
@ -69,6 +70,7 @@ import Control.Applicative
import Control.Concurrent.STM.TVar
import Control.Monad
import Control.Monad.Trans.Except
import Data.Aeson
import Data.Bifunctor
import Data.Bitraversable
import Data.Function
@ -86,7 +88,7 @@ import Yesod.Auth
import Yesod.Auth.Account
import Yesod.Auth.Account.Message
import Yesod.Core
import Yesod.Form
import Yesod.Form hiding (emailField)
import Yesod.Persist.Core
import qualified Data.ByteString.Char8 as BC
@ -195,6 +197,49 @@ getRegisterAvailableR = do
runDB $ getBy $ UniquePersonUsername $ text2username username
unless (isNothing maybePerson) $ invalidArgs ["taken"]
postRegisterR :: Handler Encoding
postRegisterR = do
requireAppAuth
settings <- asksSite appSettings
unless (appRegister settings) $ invalidArgs ["disabled"]
room <-
case appAccounts settings of
Nothing -> return True
Just cap -> do
current <- runDB $ count ([] :: [Filter Person])
return $ current < cap
unless room $ invalidArgs ["full"]
(username, pass, email) <-
runInputPost $ (,,)
<$> ireq (checkM checkValidUsername textField) "username"
<*> ireq passwordField "passphrase"
<*> ireq emailField "email"
muser <- runDB $ getBy $ UniquePersonUsername $ text2username username
unless (isNothing muser) $ invalidArgs ["username_taken"]
muser' <- runDB $ getBy $ UniquePersonEmail email
unless (isNothing muser') $ invalidArgs ["email_taken"]
key <- newVerifyKey
hashed <- hashPassphrase pass
mnew <- unAccountPersistDB' $ addNewUser username email key hashed
person <-
case mnew of
Left err -> invalidArgs [err]
Right p -> pure p
verif <- getsYesod requireEmailVerification
if verif
then sendVerifyEmail username email $ AuthR $ verifyR username key
else unAccountPersistDB' $ verifyAccount person
return $ pairs
( "email_sent" .= verif
)
getHomeR :: Handler Html
getHomeR = do
mp <- maybeAuth

View file

@ -120,6 +120,7 @@
/register/enabled RegisterEnabledR GET
/register/available RegisterAvailableR GET
/register RegisterR POST
---- Client ------------------------------------------------------------------