Added User to AuthenticatedAction

This commit is contained in:
2026-08-25 23:37:40 +03:00
parent 9bfee2978e
commit 8582a92d85
4 changed files with 41 additions and 12 deletions
+7 -5
View File
@@ -12,12 +12,11 @@ import java.time.{Duration, Instant}
import scala.concurrent.{ExecutionContext, Future}
import models.AuthToken
import schemas.AuthTokens
import utils.Random
import scala.language.postfixOps
class AuthTokensDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)(implicit val ec: ExecutionContext) extends HasDatabaseConfigProvider[PostgresProfile] {
private lazy val random = SecureRandom()
private lazy val base64 = Base64.getEncoder
private final lazy val tokenValidDuration = Duration.ofDays(30)
def find(token: String): Future[Option[AuthToken]] = {
@@ -26,12 +25,10 @@ class AuthTokensDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProv
}
def createToken(user: models.User): Future[AuthToken] = {
val tokenRaw = ByteBuffer.allocate(12)
random.nextBytes(tokenRaw.array())
val token = AuthToken(
id = None,
userId = user.id.get,
token = base64.encodeToString(tokenRaw.array()),
token = Random.randomBase64(12),
createdAt = Instant.now,
expiresAt = Instant.now.plus(tokenValidDuration)
)
@@ -39,6 +36,11 @@ class AuthTokensDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProv
db.run((AuthTokens returning AuthTokens.map(_.id)) += token).map { tokenId => token.copy(id = Some(tokenId)) }
}
def getUser(token: AuthToken): Future[Option[models.User]] = {
val query = schemas.Users.filter(_.id === token.userId).take(1).result.headOption
db.run(query)
}
def destroy(token: AuthToken): Future[Unit] = {
val q = AuthTokens.filter(_.token === token.token).delete
db.run(q).map{ _ => () }