Adds login ability, error handling, and logging (#1)
Reviewed-on: #1 Co-authored-by: aarbit <aarbit@gmail.com> Co-committed-by: aarbit <aarbit@gmail.com>
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package net.halfbinary.scavengerhuntapi.controller
|
||||
|
||||
import jakarta.servlet.http.Cookie
|
||||
import jakarta.servlet.http.HttpServletResponse
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toLoginResponse
|
||||
import net.halfbinary.scavengerhuntapi.model.request.LoginRequest
|
||||
import net.halfbinary.scavengerhuntapi.model.response.LoginResponse
|
||||
import net.halfbinary.scavengerhuntapi.service.LoginService
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import java.net.URLEncoder
|
||||
|
||||
|
||||
@RestController
|
||||
class LoginController(private val loginService: LoginService) {
|
||||
@PostMapping("/login")
|
||||
fun login(@RequestBody body: LoginRequest, response: HttpServletResponse): ResponseEntity<LoginResponse> {
|
||||
val result = loginService.login(body.toDomain())
|
||||
val creds = "${result.email}|${result.name}"
|
||||
val encodedCreds = URLEncoder.encode(creds, "UTF-8")
|
||||
response.addCookie(Cookie("creds", encodedCreds))
|
||||
return ResponseEntity.ok(result.toLoginResponse())
|
||||
}
|
||||
|
||||
@PostMapping("/logout")
|
||||
fun logout(response: HttpServletResponse): ResponseEntity<String> {
|
||||
val cookie = Cookie("creds", null)
|
||||
cookie.maxAge = 0
|
||||
response.addCookie(cookie)
|
||||
return ResponseEntity.ok("OK")
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,7 @@ import org.springframework.web.bind.annotation.RestController
|
||||
class SignupController(private val signupService: SignupService) {
|
||||
@PostMapping("/signup")
|
||||
fun hunterSignup(@RequestBody body: HunterSignupRequest): ResponseEntity<Any> {
|
||||
try {
|
||||
signupService.createNewHunter(body.toDomain())
|
||||
return ResponseEntity.ok().build()
|
||||
} catch (e: RuntimeException) {
|
||||
return ResponseEntity.badRequest().body(e.message)
|
||||
}
|
||||
|
||||
signupService.createNewHunter(body.toDomain())
|
||||
return ResponseEntity.ok().build()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user