Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/kotlin/es/wokis/data/dto/user/auth/Auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<UserBO>
suspend fun getUserById(id: String?): UserBO?
Expand All @@ -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(),
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/es/wokis/routing/AuthRouting.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
}
Expand All @@ -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<String>()
val googleToken = call.receive<GoogleAuthDTO>()
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.")
}
}