Add ExistingUser handling
This commit is contained in:
parent
214f64dd94
commit
d5f014b3d5
@ -13,10 +13,7 @@ import io.ktor.request.receive
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Routing
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import me.liuwj.ktorm.dsl.eq
|
||||
import me.liuwj.ktorm.dsl.from
|
||||
import me.liuwj.ktorm.dsl.select
|
||||
import me.liuwj.ktorm.dsl.where
|
||||
import me.liuwj.ktorm.dsl.*
|
||||
import me.liuwj.ktorm.entity.add
|
||||
import me.liuwj.ktorm.entity.sequenceOf
|
||||
import org.kodein.di.Kodein
|
||||
@ -39,11 +36,11 @@ class UserController(kodein: Kodein) : KodeinController(kodein) {
|
||||
.where { Users.username eq credential.username }
|
||||
.map { row -> row[Users.email]!! to row[Users.password]!! }
|
||||
.firstOrNull()
|
||||
?: return@post call.respond(HttpStatusCode.BadRequest, ApiError.InvalidCredentialError())
|
||||
?: return@post call.respond(HttpStatusCode.BadRequest, ApiError.InvalidCredentialError)
|
||||
|
||||
|
||||
if (!BCrypt.checkpw(credential.password, password)) {
|
||||
return@post call.respond(HttpStatusCode.BadRequest, ApiError.InvalidCredentialError())
|
||||
return@post call.respond(HttpStatusCode.BadRequest, ApiError.InvalidCredentialError)
|
||||
}
|
||||
|
||||
return@post call.respond(Response(simpleJwt.sign(email)))
|
||||
@ -54,8 +51,14 @@ class UserController(kodein: Kodein) : KodeinController(kodein) {
|
||||
|
||||
val user = call.receive<SignUpInfo>()
|
||||
|
||||
// TODO check if user does not already exists
|
||||
// db won't let you insert it anyway
|
||||
val exists = db.from(Users)
|
||||
.select()
|
||||
.where { (Users.username eq user.username) or (Users.email eq user.email) }
|
||||
.any()
|
||||
|
||||
if (exists) {
|
||||
return@post call.respond(HttpStatusCode.Conflict, ApiError.ExistingUserError)
|
||||
}
|
||||
|
||||
val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt())
|
||||
|
||||
@ -68,7 +71,7 @@ class UserController(kodein: Kodein) : KodeinController(kodein) {
|
||||
|
||||
db.sequenceOf(Users).add(newUser)
|
||||
|
||||
call.respond(HttpStatusCode.Created, Response("User created successfully"))
|
||||
return@post call.respond(HttpStatusCode.Created, Response("User created successfully"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package be.vandewalleh.errors
|
||||
|
||||
sealed class ApiError(val message: String){
|
||||
class InvalidCredentialError : ApiError("Invalid credentials")
|
||||
object InvalidCredentialError : ApiError("Invalid credentials")
|
||||
object ExistingUserError : ApiError("User already exists")
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user