From 9ac0f796e8e8b0e66ad1856002f56dea00b2f633 Mon Sep 17 00:00:00 2001 From: Wikijito7 Date: Thu, 22 Dec 2022 19:10:47 +0100 Subject: [PATCH] Server refinement --- src/main/kotlin/es/wokis/data/dto/user/auth/Auth.kt | 8 ++++++++ .../es/wokis/data/repository/user/UserRepository.kt | 7 ++++--- src/main/kotlin/es/wokis/routing/AuthRouting.kt | 10 ++++++---- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/es/wokis/data/dto/user/auth/Auth.kt b/src/main/kotlin/es/wokis/data/dto/user/auth/Auth.kt index 45bb9ae..b9079ae 100644 --- a/src/main/kotlin/es/wokis/data/dto/user/auth/Auth.kt +++ b/src/main/kotlin/es/wokis/data/dto/user/auth/Auth.kt @@ -14,4 +14,12 @@ data class RegisterDTO( val password: String, val lang: String = DEFAULT_LANG, val isGoogleAuth: Boolean = false +) + +data class AuthResponseDTO( + val authToken: String +) + +data class GoogleAuthDTO ( + val authToken: String ) \ No newline at end of file diff --git a/src/main/kotlin/es/wokis/data/repository/user/UserRepository.kt b/src/main/kotlin/es/wokis/data/repository/user/UserRepository.kt index 1e770d2..6252f9e 100644 --- a/src/main/kotlin/es/wokis/data/repository/user/UserRepository.kt +++ b/src/main/kotlin/es/wokis/data/repository/user/UserRepository.kt @@ -8,6 +8,7 @@ import es.wokis.data.bo.user.UserBO import es.wokis.data.constants.ServerConstants.DEFAULT_LANG import es.wokis.data.constants.ServerConstants.EMPTY_TEXT import es.wokis.data.datasource.user.UserLocalDataSource +import es.wokis.data.dto.user.auth.GoogleAuthDTO import es.wokis.data.dto.user.auth.LoginDTO import es.wokis.data.dto.user.auth.RegisterDTO import es.wokis.data.mapper.user.toBO @@ -18,7 +19,7 @@ import org.mindrot.jbcrypt.BCrypt interface UserRepository { suspend fun login(login: LoginDTO): String? - suspend fun loginWithGoogle(googleToken: String): String? + suspend fun loginWithGoogle(googleToken: GoogleAuthDTO): String? suspend fun register(register: RegisterDTO): String? suspend fun getUsers(): List suspend fun getUserById(id: String?): UserBO? @@ -40,7 +41,7 @@ class UserRepositoryImpl(private val userLocalDataSource: UserLocalDataSource) : } } - override suspend fun loginWithGoogle(googleToken: String): String? { + override suspend fun loginWithGoogle(googleToken: GoogleAuthDTO): String? { val verifier: GoogleIdTokenVerifier = GoogleIdTokenVerifier.Builder( NetHttpTransport(), @@ -50,7 +51,7 @@ class UserRepositoryImpl(private val userLocalDataSource: UserLocalDataSource) : .setIssuer("https://accounts.google.com") .build() - return verifier.verify(googleToken)?.let { + return verifier.verify(googleToken.authToken)?.let { val payload: GoogleIdToken.Payload = it.payload // Print user identifier diff --git a/src/main/kotlin/es/wokis/routing/AuthRouting.kt b/src/main/kotlin/es/wokis/routing/AuthRouting.kt index cc21e4e..6eb745d 100644 --- a/src/main/kotlin/es/wokis/routing/AuthRouting.kt +++ b/src/main/kotlin/es/wokis/routing/AuthRouting.kt @@ -1,5 +1,7 @@ package es.wokis.routing +import es.wokis.data.dto.user.auth.AuthResponseDTO +import es.wokis.data.dto.user.auth.GoogleAuthDTO import es.wokis.data.dto.user.auth.LoginDTO import es.wokis.data.dto.user.auth.RegisterDTO import es.wokis.data.repository.user.UserRepository @@ -18,7 +20,7 @@ fun Routing.setUpAuthRouting() { val token: String? = userRepository.login(user) token?.let { - call.respond(HttpStatusCode.OK, it) + call.respond(HttpStatusCode.OK, AuthResponseDTO(it)) } ?: run { call.respond(HttpStatusCode.NotFound, "Wrong username or password") } @@ -30,17 +32,17 @@ fun Routing.setUpAuthRouting() { val token: String? = userRepository.register(user) token?.let { - call.respond(HttpStatusCode.OK, it) + call.respond(HttpStatusCode.OK, AuthResponseDTO(it)) } ?: run { call.respond(HttpStatusCode.Conflict, "That user already exists") } } post("/google-auth") { - val googleToken = call.receive() + val googleToken = call.receive() val token: String? = userRepository.loginWithGoogle(googleToken) token?.let { - call.respond(HttpStatusCode.OK, it) + call.respond(HttpStatusCode.OK, AuthResponseDTO(it)) } ?: call.respond(HttpStatusCode.NotFound, "That user doesn't exists.") } } \ No newline at end of file