Skip to content

Instantly share code, notes, and snippets.

Created July 13, 2017 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d0b0ff714707046da550812602b3d307 to your computer and use it in GitHub Desktop.
Save anonymous/d0b0ff714707046da550812602b3d307 to your computer and use it in GitHub Desktop.
the description for this gist
/**
* Register new user with credentials access.
* Given email needs to be unique.
*
* @param c - user's email and password hash
* @return `RegistrationResponse`.
*/
def registerUser(c: Credentials): FS[RegistrationResponse] = {
import c.email
val hash = c.password.bcrypt
def emailAlreadyTaken: FS[RegistrationResponse] =
for {
_ <- log.info(s"Cannot create user, email '$email' is already used")
} yield AlreadyRegistered: RegistrationResponse
def userRegistered(id: UserId): FS[RegistrationResponse] =
for {
_ <- log.info("User account with credentials access created")
} yield UserRegistered(id): RegistrationResponse
for {
_ <- log.info(s"Trying to register new user with email: '$email'")
insertResult <- persistence.saveCredentialsUser(email, hash)
registrationResult <- insertResult match {
case UserInserted(id) =>
userRegistered(id)
case AlreadyExists =>
emailAlreadyTaken
case DBFailure(err) =>
FreeS.pure[F, RegistrationResponse](InternalFailure(err))
}
} yield registrationResult
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment