Skip to content

Instantly share code, notes, and snippets.

Created July 13, 2017 06:33
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/bed7ea66ca2521944f5c35ec883ecc78 to your computer and use it in GitHub Desktop.
Save anonymous/bed7ea66ca2521944f5c35ec883ecc78 to your computer and use it in GitHub Desktop.
the description for this gist
/**
* Logs in user with his credentials.
*
* @param credentials - user's email and password hash
* @return `LoginResponse`.
*/
/**
* Logs in user with his credentials.
*
* @param credentials - user's email and password hash
* @return `LoginResponse`.
*/
def login(credentials: Credentials): FS[LoginResponse] = {
import credentials._
for {
userAndPassword <- persistence.getPasswordByEmail(email)
response <- userAndPassword match {
case Some((id, pass)) if password.isBcrypted(pass) =>
(for {
_ <- log.info("User '$id' provided valid email and password")
token <- jwt.issue(id)
_ <- log.info(s"Token is $token")
} yield LoggedIn(token): LoginResponse): FS[LoginResponse]
case Some((id, _)) =>
(for {
_ <- log.info(s"User '$id' provided invalid password")
} yield InvalidCredentials: LoginResponse): FS[LoginResponse]
case None =>
(for {
_ <- log.info(s"Log in rejected: no access for '$email'")
} yield InvalidCredentials: LoginResponse): FS[LoginResponse]
}
} yield response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment