This is ugly..
This commit is contained in:
parent
1f9923dc82
commit
69c35d0732
@ -7,11 +7,14 @@ import be.vandewalleh.migrations.Migration
|
||||
import be.vandewalleh.routing.registerRoutes
|
||||
import be.vandewalleh.services.serviceModule
|
||||
import io.ktor.application.Application
|
||||
import io.ktor.application.call
|
||||
import io.ktor.application.feature
|
||||
import io.ktor.application.log
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
import io.ktor.routing.Routing
|
||||
import io.ktor.routing.RoutingPath.Companion.root
|
||||
import io.ktor.routing.get
|
||||
import io.ktor.routing.routing
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import org.kodein.di.Kodein
|
||||
|
||||
@ -11,7 +11,7 @@ import org.kodein.di.generic.instance
|
||||
fun Application.authenticationModule() {
|
||||
install(Authentication) {
|
||||
jwt {
|
||||
val simpleJwt: SimpleJWT by kodein.instance()
|
||||
val simpleJwt by kodein.instance<SimpleJWT>()
|
||||
verifier(simpleJwt.verifier)
|
||||
validate {
|
||||
UserIdPrincipal(it.payload.getClaim("name").asString())
|
||||
|
||||
@ -1,8 +1,14 @@
|
||||
package be.vandewalleh.routing
|
||||
|
||||
import io.ktor.auth.authenticate
|
||||
import io.ktor.routing.Routing
|
||||
import io.ktor.routing.route
|
||||
import org.kodein.di.Kodein
|
||||
|
||||
fun Routing.chapters(kodein: Kodein) {
|
||||
authenticate {
|
||||
route("/notes/{noteTitle}/chapters/{chapterNumber}") {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ import io.ktor.request.receive
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Routing
|
||||
import io.ktor.routing.post
|
||||
import io.ktor.routing.route
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.instance
|
||||
import org.mindrot.jbcrypt.BCrypt
|
||||
@ -19,6 +20,7 @@ fun Routing.login(kodein: Kodein) {
|
||||
|
||||
data class TokenResponse(val token: String)
|
||||
|
||||
route("/login"){
|
||||
post {
|
||||
val credential = call.receive<UsernamePasswordCredential>()
|
||||
|
||||
@ -31,5 +33,7 @@ fun Routing.login(kodein: Kodein) {
|
||||
|
||||
return@post call.respond(TokenResponse(simpleJwt.sign(email)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -3,6 +3,7 @@ package be.vandewalleh.routing
|
||||
import be.vandewalleh.extensions.userId
|
||||
import be.vandewalleh.services.NotesService
|
||||
import io.ktor.application.call
|
||||
import io.ktor.auth.authenticate
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Routing
|
||||
import io.ktor.routing.get
|
||||
@ -12,9 +13,11 @@ import org.kodein.di.generic.instance
|
||||
fun Routing.notes(kodein: Kodein) {
|
||||
val notesService by kodein.instance<NotesService>()
|
||||
|
||||
get {
|
||||
authenticate {
|
||||
get("/notes") {
|
||||
val userId = call.userId()
|
||||
val notes = notesService.getNotes(userId)
|
||||
call.respond(notes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ import org.mindrot.jbcrypt.BCrypt
|
||||
fun Routing.register(kodein: Kodein) {
|
||||
val userService by kodein.instance<UserService>()
|
||||
|
||||
post {
|
||||
post("/register") {
|
||||
val user = call.receive<UserRegistrationDto>()
|
||||
|
||||
if (userService.userExists(user.username, user.email))
|
||||
|
||||
@ -1,30 +1,12 @@
|
||||
package be.vandewalleh.routing
|
||||
|
||||
import io.ktor.auth.authenticate
|
||||
import io.ktor.routing.Routing
|
||||
import io.ktor.routing.route
|
||||
import org.kodein.di.Kodein
|
||||
|
||||
fun Routing.registerRoutes(kodein: Kodein) {
|
||||
route("/login") {
|
||||
this@registerRoutes.login(kodein)
|
||||
}
|
||||
|
||||
route("/register") {
|
||||
this@registerRoutes.register(kodein)
|
||||
}
|
||||
|
||||
authenticate {
|
||||
route("/notes") {
|
||||
this@registerRoutes.notes(kodein)
|
||||
|
||||
route("/{noteTitle}") {
|
||||
this@registerRoutes.title(kodein)
|
||||
|
||||
route("/chapters/{chapterNumber}") {
|
||||
this@registerRoutes.chapters(kodein)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
login(kodein)
|
||||
register(kodein)
|
||||
notes(kodein)
|
||||
title(kodein)
|
||||
chapters(kodein)
|
||||
}
|
||||
@ -3,6 +3,7 @@ package be.vandewalleh.routing
|
||||
import be.vandewalleh.extensions.*
|
||||
import be.vandewalleh.services.NotesService
|
||||
import io.ktor.application.call
|
||||
import io.ktor.auth.authenticate
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.*
|
||||
@ -12,6 +13,8 @@ import org.kodein.di.generic.instance
|
||||
fun Routing.title(kodein: Kodein) {
|
||||
val notesService by kodein.instance<NotesService>()
|
||||
|
||||
authenticate {
|
||||
route("/notes/{noteTitle}") {
|
||||
post {
|
||||
val userId = call.userId()
|
||||
val title = call.parameters.noteTitle()
|
||||
@ -57,3 +60,5 @@ fun Routing.title(kodein: Kodein) {
|
||||
call.respondStatus(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user