[O] Unwrap spaghetti code

This commit is contained in:
Azalea
2024-02-19 01:49:29 -05:00
parent 1e606f8b85
commit 4c3aafd266
3 changed files with 29 additions and 20 deletions

View File

@@ -3,6 +3,7 @@ package icu.samnyan.aqua.net
import ext.*
import icu.samnyan.aqua.net.db.AquaNetUser
import icu.samnyan.aqua.net.db.AquaNetUserRepo
import icu.samnyan.aqua.net.utils.GeoIP
import icu.samnyan.aqua.net.utils.TurnstileService
import jakarta.servlet.http.HttpServletRequest
import org.springframework.security.crypto.password.PasswordEncoder
@@ -15,7 +16,8 @@ import org.springframework.web.bind.annotation.RestController
class UserRegistrar(
val userRepo: AquaNetUserRepo,
val hasher: PasswordEncoder,
val turnstileService: TurnstileService
val turnstileService: TurnstileService,
val geoIP: GeoIP
) {
/**
* Register a new user
@@ -23,8 +25,10 @@ class UserRegistrar(
@PostMapping("/register")
suspend fun register(@RP username: Str, @RP email: Str, @RP password: Str,
@RP turnstile: Str?, request: HttpServletRequest) {
val ip = geoIP.getIP(request)
// Check captcha
if (!turnstileService.validate(turnstile, request)) 400 > "Invalid captcha"
if (!turnstileService.validate(turnstile, ip)) 400 > "Invalid captcha"
// Check if email is valid
if (!email.isValidEmail()) 400 > "Invalid email"
@@ -47,12 +51,14 @@ class UserRegistrar(
if (password.length < 8) 400 > "Password too short"
// GeoIP check to infer country
val country = geoIP.getCountry(ip)
val u = AquaNetUser(username = username, email = email, pwHash = hasher.encode(password),
regTime = millis(), lastLogin = millis())
regTime = millis(), lastLogin = millis(), country = country)
async { userRepo.save(u) }
// TODO: Send confirmation email
200 > "User created"
}
}