Implemented login
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package dao
|
||||
|
||||
import jakarta.inject.Inject
|
||||
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
|
||||
import slick.jdbc.PostgresProfile
|
||||
import slick.jdbc.PostgresProfile.api._
|
||||
|
||||
import java.nio.ByteBuffer
|
||||
import java.security.SecureRandom
|
||||
import java.util.Base64
|
||||
import java.time.{Instant, Duration}
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
import models.AuthToken
|
||||
import schemas.AuthTokens
|
||||
|
||||
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 createToken(user: models.User): Future[AuthToken] = {
|
||||
val tokenRaw = ByteBuffer.allocate(10)
|
||||
random.nextBytes(tokenRaw.array())
|
||||
val token = AuthToken(
|
||||
id = None,
|
||||
userId = user.id.get,
|
||||
token = base64.encodeToString(tokenRaw.array()),
|
||||
createdAt = Instant.now,
|
||||
expiresAt = Instant.now.plus(tokenValidDuration)
|
||||
)
|
||||
|
||||
db.run((AuthTokens returning AuthTokens.map(_.id)) += token).map { tokenId => token.copy(id = Some(tokenId)) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user