From e5f21844d82c496f74797ff0e90f9154cd928eb5 Mon Sep 17 00:00:00 2001 From: Nicholas Kalar Date: Sat, 16 Aug 2025 17:01:02 -0400 Subject: [PATCH] Added initial Login Routes --- src/main/kotlin/routes/LoginRoutes.kt | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/kotlin/routes/LoginRoutes.kt diff --git a/src/main/kotlin/routes/LoginRoutes.kt b/src/main/kotlin/routes/LoginRoutes.kt new file mode 100644 index 0000000..aa2e3ae --- /dev/null +++ b/src/main/kotlin/routes/LoginRoutes.kt @@ -0,0 +1,42 @@ +package codes.kalar.routes + +import codes.kalar.model.User +import com.auth0.jwt.JWT +import com.auth0.jwt.algorithms.Algorithm +import io.ktor.http.HttpStatusCode +import io.ktor.server.application.* +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.post +import io.ktor.server.routing.routing +import java.util.Date + +fun Application.configureLoginRoutes() { + val secret = environment.config.property("jwt.secret").getString() + val issuer = environment.config.property("jwt.issuer").getString() + val audience = environment.config.property("jwt.audience").getString() + val myRealm = environment.config.property("jwt.realm").getString() + + routing { + post("/login") { + try { + val user = call.receive() + val name = user.name + val password = user.password + + // TODO Check is username exists and password matches + + val token = JWT.create() + .withAudience(audience) + .withIssuer(issuer) + .withClaim("name", name) + .withExpiresAt(Date(System.currentTimeMillis() + 160000)) + .sign(Algorithm.HMAC256(secret)) + call.respond(hashMapOf("token" to token)) + } catch (e: Exception) { + call.respond(HttpStatusCode.BadRequest, e.message ?: "Something went wrong") + } + + } + } +} \ No newline at end of file