feat: add expire time into login response
This commit is contained in:
@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* REST controller for user authentication endpoints (login, logout).
|
||||
@@ -43,12 +44,14 @@ public class AuthController {
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<UserResponse> login(@Validated @RequestBody LoginRequest request) {
|
||||
var user = authService.login(request);
|
||||
var currentTime = LocalDateTime.now();
|
||||
var accessToken = tokenClient.generateToken(user);
|
||||
var accessTokenCookie = cookieService.buildCookie(CookieName.ACCESS_TOKEN, accessToken);
|
||||
var cookieMaxAge = accessTokenCookie.getMaxAge();
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.SET_COOKIE, accessTokenCookie.toString())
|
||||
.body(UserResponse.from(user));
|
||||
.body(UserResponse.from(user, currentTime.plus(cookieMaxAge)));
|
||||
}
|
||||
|
||||
@RequiresAuth
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.onixbyte.deltaforceguide.domain.dto;
|
||||
|
||||
import com.onixbyte.deltaforceguide.domain.entity.User;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* Response DTO for a user account, including associated credentials.
|
||||
*
|
||||
@@ -10,13 +12,15 @@ import com.onixbyte.deltaforceguide.domain.entity.User;
|
||||
public record UserResponse(
|
||||
Long id,
|
||||
String username,
|
||||
String email
|
||||
String email,
|
||||
LocalDateTime expiration
|
||||
) {
|
||||
public static UserResponse from(User user) {
|
||||
public static UserResponse from(User user, LocalDateTime expiration) {
|
||||
return new UserResponse(
|
||||
user.getId(),
|
||||
user.getUsername(),
|
||||
user.getEmail()
|
||||
user.getEmail(),
|
||||
expiration
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user