Implemented login
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package dao
|
||||
|
||||
import de.mkammerer.argon2.Argon2Factory
|
||||
import execution.CryptoExecutionContext
|
||||
import jakarta.inject.Inject
|
||||
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
|
||||
import slick.jdbc.PostgresProfile
|
||||
import slick.jdbc.PostgresProfile.api.*
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
import models.User
|
||||
import schemas.Users
|
||||
|
||||
class UsersDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvider, private val cryptoEC: CryptoExecutionContext)(implicit val ec: ExecutionContext)
|
||||
extends HasDatabaseConfigProvider[PostgresProfile] {
|
||||
private val argon2 = Argon2Factory.create()
|
||||
|
||||
def authenticate(username: String, password: String): Future[Option[User]] = {
|
||||
val query = Users.filter(_.username === username).take(1).result.headOption
|
||||
|
||||
val userFuture = db.run(query)
|
||||
|
||||
userFuture.map { userOption =>
|
||||
for {
|
||||
user <- userOption
|
||||
digest <- user.passwordDigest
|
||||
if argon2.verify(digest, password.getBytes("UTF-8"))
|
||||
} yield user
|
||||
}(using cryptoEC)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user