Split user controller into register and login

This commit is contained in:
Hubert Van De Walle 2020-04-20 16:26:19 +02:00
parent c387a2c4cf
commit 2a583ed399
4 changed files with 67 additions and 68 deletions

View File

@ -13,7 +13,8 @@ import org.kodein.di.generic.singleton
val controllerModule = Kodein.Module(name = "Controller") {
bind() from setBinding<KodeinController>()
bind<KodeinController>().inSet() with singleton { UserController(this.kodein) }
bind<KodeinController>().inSet() with singleton { RegisterController(this.kodein) }
bind<KodeinController>().inSet() with singleton { LoginController(this.kodein) }
bind<KodeinController>().inSet() with singleton { NotesController(this.kodein) }
bind<KodeinController>().inSet() with singleton { TitleController(this.kodein) }
}

View File

@ -0,0 +1,39 @@
package be.vandewalleh.controllers
import be.vandewalleh.auth.SimpleJWT
import be.vandewalleh.auth.UsernamePasswordCredential
import be.vandewalleh.controllers.base.KodeinController
import be.vandewalleh.services.UserService
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.Routing
import io.ktor.routing.post
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
import org.mindrot.jbcrypt.BCrypt
class LoginController(kodein: Kodein) : KodeinController("/login", kodein) {
private val simpleJwt by instance<SimpleJWT>()
private val userService by instance<UserService>()
data class TokenResponse(val token: String)
override fun Routing.routes() {
post {
val credential = call.receive<UsernamePasswordCredential>()
val (email, password) = userService.getEmailAndPasswordFromUsername(credential.username)
?: return@post call.respondStatus(HttpStatusCode.Unauthorized)
if (!BCrypt.checkpw(credential.password, password)) {
return@post call.respondStatus(HttpStatusCode.Unauthorized)
}
return@post call.respond(TokenResponse(simpleJwt.sign(email)))
}
}
}

View File

@ -1,10 +1,35 @@
package be.vandewalleh.controllers
import be.vandewalleh.controllers.base.KodeinController
import be.vandewalleh.services.UserRegistrationDto
import be.vandewalleh.services.UserService
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.Routing
import io.ktor.routing.post
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
import org.mindrot.jbcrypt.BCrypt
class RegisterController(kodein: Kodein) : KodeinController("/register", kodein) {
private val userService by instance<UserService>()
class RegisterController(kodein: Kodein) : KodeinController("", kodein) {
override fun Routing.routes() {
post {
val user = call.receive<UserRegistrationDto>()
if (userService.userExists(user.username, user.email))
return@post call.respond(HttpStatusCode.Conflict)
val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt())
userService.createUser(
UserRegistrationDto(user.username, user.email, hashedPassword)
)
return@post call.respondStatus(HttpStatusCode.Created)
}
}
}

View File

@ -1,66 +0,0 @@
package be.vandewalleh.controllers
import be.vandewalleh.auth.SimpleJWT
import be.vandewalleh.auth.UsernamePasswordCredential
import be.vandewalleh.controllers.base.KodeinController
import be.vandewalleh.services.UserRegistrationDto
import be.vandewalleh.services.UserService
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.locations.Location
import io.ktor.locations.post
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.Routing
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
import org.mindrot.jbcrypt.BCrypt
class UserController(kodein: Kodein) : KodeinController(kodein) {
private val simpleJwt by instance<SimpleJWT>()
private val userService by instance<UserService>()
override fun Routing.registerRoutes() {
post<Routes.Login> {
data class Response(val token: String)
val credential = call.receive<UsernamePasswordCredential>()
val (email, password) = userService.getEmailAndPasswordFromUsername(credential.username)
?: return@post call.respondStatus(HttpStatusCode.Unauthorized)
if (!BCrypt.checkpw(credential.password, password)) {
return@post call.respondStatus(HttpStatusCode.Unauthorized)
}
return@post call.respond(Response(simpleJwt.sign(email)))
}
post<Routes.Register> {
data class Response(val message: String)
val user = call.receive<UserRegistrationDto>()
if (userService.userExists(user.username, user.email))
return@post call.respond(HttpStatusCode.Conflict)
val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt())
userService.createUser(
UserRegistrationDto(user.username, user.email, hashedPassword)
)
return@post call.respondStatus(HttpStatusCode.Created)
}
}
object Routes {
@Location("/login")
class Login
@Location("/register")
class Register
}
}