Skip to content

Commit

Permalink
Make email optional at registration (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins authored Jul 13, 2022
1 parent afaae0d commit fa3d2b5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion fission-cli/library/Fission/CLI/Handler/User/Register.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ createAccount maybeUsername maybeEmail = do
let
form = Registration
{ username
, email
, email = Just email
, password = Nothing
, exchangePK = Just exchangePK
}
Expand Down
22 changes: 14 additions & 8 deletions fission-core/library/Fission/User/Registration/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Web.UCAN.Internal.Orphanage.RSA2048.Public ()

data Registration = Registration
{ username :: Username
, email :: Email
, email :: Maybe Email
, password :: Maybe Password
, exchangePK :: Maybe RSA.PublicKey
}
Expand All @@ -29,16 +29,22 @@ instance Arbitrary Registration where
return Registration {..}

instance ToJSON Registration where
toJSON Registration { username, email = Email email' } =
Object [ ("username", String $ textDisplay username)
, ("email", String email')
]
toJSON Registration { username, email = mayEmail } =
case mayEmail of
Just (Email email) ->
Object [ ("username", String $ textDisplay username)
, ("email", String email)
]

Nothing ->
Object [ ("username", String $ textDisplay username) ]


instance FromJSON Registration where
parseJSON = withObject "Registration" \obj -> do
username <- obj .: "username"
password <- obj .:? "password"
email <- obj .: "email"
email <- obj .:? "email"
exchangePK <- obj .:? "exchangePK"

return Registration {..}
Expand All @@ -54,11 +60,11 @@ instance ToSchema Registration where
[ ("username", username')
, ("email", email')
]
|> required .~ ["username", "email"]
|> required .~ ["username"]
|> description ?~ "The information that a user needs to provide to login/register."
|> example ?~ toJSON Registration
{ username = "username"
, email = "alice@example.com"
, email = Just "alice@example.com"
, password = Nothing
, exchangePK = Nothing
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,19 @@ withDID ::
, Challenge.Creator m
)
=> ServerT User.Create.WithDID m
withDID User.Registration {username, email} (DID.Key publicKey) = do
withDID User.Registration {username, email = mayEmail} (DID.Key publicKey) = do
now <- currentTime
userId <- Web.Err.ensureM $ User.create username publicKey email now
userId <- Web.Err.ensureM $ User.create username publicKey mayEmail now
challenge <- Web.Err.ensureM $ Challenge.create userId
Web.Err.ensureM $ sendVerificationEmail (Recipient email username) challenge
return NoContent

case mayEmail of
Just email -> do
Web.Err.ensureM $ sendVerificationEmail (Recipient email username) challenge
return NoContent

Nothing ->
return NoContent


withPassword ::
( MonadDNSLink m
Expand All @@ -64,9 +71,15 @@ withPassword ::
withPassword User.Registration {password = Nothing} =
Web.Err.throw err422 { errBody = "Missing password" }

withPassword User.Registration {username, password = Just pass, email} = do
withPassword User.Registration {username, password = Just pass, email = mayEmail} = do
now <- currentTime
userId <- Web.Err.ensureM $ User.createWithPassword username pass email now
userId <- Web.Err.ensureM $ User.createWithPassword username pass mayEmail now
challenge <- Web.Err.ensureM $ Challenge.create userId
Web.Err.ensureM $ sendVerificationEmail (Recipient email username) challenge
return ()

case mayEmail of
Just email -> do
Web.Err.ensureM $ sendVerificationEmail (Recipient email username) challenge
return ()

Nothing ->
return ()
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ start middleware runner = do
logInfo @Text "🙋 Ensuring default user is in DB"
userId <- User.getByPublicKey serverPK >>= \case
Just (Entity userId _) -> return userId
Nothing -> Web.Error.ensureM $ User.createDB "fission" serverPK "hello@fission.codes" now
Nothing -> Web.Error.ensureM $ User.createDB "fission" serverPK (Just "hello@fission.codes") now

logInfo @Text "💽 Ensuring default data domain domains is in DB"
Domain.getByDomainName userRootDomain >>= \case
Expand Down
12 changes: 6 additions & 6 deletions fission-web-server/library/Fission/Web/Server/User/Creator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ createDB ::
MonadIO m
=> Username
-> Key.Public
-> Email
-> Maybe Email
-> UTCTime
-> Transaction m (Either Errors' UserId)
createDB username pk email now =
createDB username pk mayEmail now =
insertUnique user >>= \case
Just userId -> return $ Right userId
Nothing -> determineConflict username (Just pk)
Expand All @@ -50,7 +50,7 @@ createDB username pk email now =
{ userPublicKey = Just pk
, userExchangeKeys = Just []
, userUsername = username
, userEmail = Just email
, userEmail = mayEmail
, userRole = Regular
, userActive = True
, userVerified = False
Expand All @@ -66,10 +66,10 @@ createWithPasswordDB ::
MonadIO m
=> Username
-> Password
-> Email
-> Maybe Email
-> UTCTime
-> Transaction m (Either Errors' UserId)
createWithPasswordDB username password email now =
createWithPasswordDB username password mayEmail now =
Password.hashPassword password >>= \case
Left err ->
return $ Error.openLeft err
Expand All @@ -79,7 +79,7 @@ createWithPasswordDB username password email now =
{ userPublicKey = Nothing
, userExchangeKeys = Just []
, userUsername = username
, userEmail = Just email
, userEmail = mayEmail
, userRole = Regular
, userActive = True
, userVerified = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ class Heroku.AddOn.Creator m => Creator m where
create ::
Username
-> Key.Public
-> Email
-> Maybe Email
-> UTCTime
-> m (Either Errors' UserId)

createWithPassword ::
Username
-> Password
-> Email
-> Maybe Email
-> UTCTime
-> m (Either Errors' UserId)

Expand Down

0 comments on commit fa3d2b5

Please sign in to comment.