Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. package ch.becompany.authn
  2.  
  3. trait Dsl[F[_]] {
  4. }
  5.  
  6. object Dsl {
  7.  
  8. type UserRepository = List[User]
  9.  
  10. type UserRepositoryState[A] = State[UserRepository, A]
  11.  
  12. implicit object StateInterpreter extends Dsl[UserRepositoryState] {
  13.  
  14. override def register(email: String @@ EmailAddress, password: String):
  15. UserRepositoryState[Either[RegistrationError, User]] =
  16. State { users =>
  17. if (users.exists(_.email === email))
  18. (users, RegistrationError("User already exists").asLeft)
  19. else {
  20. val user = User(email, password)
  21. (users :+ user, user.asRight)
  22. }
  23. }
  24.  
  25. override def authn(email: String @@ EmailAddress, password: String):
  26. UserRepositoryState[Either[AuthnError, User]] =
  27. State.inspect(_
  28. .find(user => user.email === email && user.password === password)
  29. .toRight(AuthnError("Authentication failed")))
  30. }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement