From 55cfb7b3584d7e558fbb191cb4d65ad9fff8a537 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Mon, 19 Feb 2024 03:21:49 -0500 Subject: [PATCH] [+] Login --- .../icu/samnyan/aqua/net/UserRegistrar.kt | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt index c281303f..182750d8 100644 --- a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt +++ b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt @@ -26,9 +26,10 @@ class UserRegistrar( */ @PostMapping("/register") suspend fun register( - @RP username: Str, @RP email: Str, @RP password: Str, - @RP turnstile: Str, request: HttpServletRequest - ) { + @RP username: Str, @RP email: Str, @RP password: Str, @RP turnstile: Str, + request: HttpServletRequest + ): Any { + val ip = geoIP.getIP(request) // Check captcha @@ -69,6 +70,27 @@ class UserRegistrar( // TODO: Send confirmation email - 200 - "User created" + return mapOf("success" to true) + } + + @PostMapping("/login") + suspend fun login( + @RP email: Str, @RP password: Str, @RP turnstile: Str, + request: HttpServletRequest + ): Any { + + // Check captcha + val ip = geoIP.getIP(request) + if (!turnstileService.validate(turnstile, ip)) 400 - "Invalid captcha" + + // Treat email as email / username + val user = async { userRepo.findByEmailIgnoreCase(email) ?: userRepo.findByUsernameIgnoreCase(email) } + ?: (400 - "User not found") + if (!hasher.matches(password, user.pwHash)) 400 - "Invalid password" + + // Generate JWT token + val token = jwt.gen(user) + + return mapOf("token" to token) } } \ No newline at end of file