📮 Adds auth routing

This commit is contained in:
Daniel Svitan 2025-05-11 17:45:43 +02:00
parent ac0ac8dd98
commit e8ce65fbd1
2 changed files with 44 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package dev.svitan.plugins
import dev.svitan.routes.routeAuth
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.plugins.requestvalidation.*
@ -28,4 +29,6 @@ fun Application.configureRouting() {
call.respond("Hello World!")
}
}
routeAuth()
}

View File

@ -0,0 +1,41 @@
package dev.svitan.routes
import dev.svitan.schemas.AuthService
import dev.svitan.schemas.NewAuthDTO
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.auth.authentication
import io.ktor.server.plugins.NotFoundException
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.*
import java.util.UUID
fun Application.routeAuth() {
routing {
authentication {
get("/auth") {
call.respond(AuthService.readAll())
}
put("/auth") {
val auth = call.receive<NewAuthDTO>()
call.respond(HttpStatusCode.Created, AuthService.create(auth))
}
get("/auth/{id}") {
val idRaw = call.parameters["id"] ?: throw IllegalArgumentException("Invalid id")
val id = UUID.fromString(idRaw)
val auth = AuthService.read(id) ?: throw NotFoundException()
call.respond(auth)
}
delete("/auth/{id}") {
val idRaw = call.parameters["id"] ?: throw IllegalArgumentException("Invalid id")
val id = UUID.fromString(idRaw)
AuthService.delete(id)
call.respond(HttpStatusCode.NoContent)
}
}
}
}