29 lines
1.1 KiB
Kotlin
29 lines
1.1 KiB
Kotlin
package be.simplenotes.domain.usecases.users.login
|
|
|
|
import arrow.core.computations.either
|
|
import arrow.core.filterOrElse
|
|
import arrow.core.rightIfNotNull
|
|
import be.simplenotes.domain.security.PasswordHash
|
|
import be.simplenotes.domain.security.SimpleJwt
|
|
import be.simplenotes.domain.validation.UserValidations
|
|
import be.simplenotes.persistance.repositories.UserRepository
|
|
import be.simplenotes.types.LoggedInUser
|
|
import io.micronaut.context.annotation.Primary
|
|
import javax.inject.Singleton
|
|
|
|
@Singleton
|
|
@Primary
|
|
internal class LoginUseCaseImpl(
|
|
private val userRepository: UserRepository,
|
|
private val passwordHash: PasswordHash,
|
|
private val jwt: SimpleJwt<LoggedInUser>
|
|
) : LoginUseCase {
|
|
override fun login(form: LoginForm) = either.eager<LoginError, Token> {
|
|
val user = !UserValidations.validateLogin(form)
|
|
!userRepository.find(user.username)
|
|
.rightIfNotNull { Unregistered }
|
|
.filterOrElse({ passwordHash.verify(form.password!!, it.password) }, { WrongPassword })
|
|
.map { jwt.sign(LoggedInUser(it)) }
|
|
}
|
|
}
|