Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 로그인 여부 확인 API #913

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/main/java/in/koreatech/koin/domain/user/controller/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static in.koreatech.koin.domain.user.model.UserType.OWNER;
import static in.koreatech.koin.domain.user.model.UserType.STUDENT;

import in.koreatech.koin.domain.user.dto.*;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -12,22 +14,6 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import in.koreatech.koin.domain.user.dto.AuthResponse;
import in.koreatech.koin.domain.user.dto.CoopResponse;
import in.koreatech.koin.domain.user.dto.EmailCheckExistsRequest;
import in.koreatech.koin.domain.user.dto.FindPasswordRequest;
import in.koreatech.koin.domain.user.dto.NicknameCheckExistsRequest;
import in.koreatech.koin.domain.user.dto.StudentLoginRequest;
import in.koreatech.koin.domain.user.dto.StudentLoginResponse;
import in.koreatech.koin.domain.user.dto.StudentRegisterRequest;
import in.koreatech.koin.domain.user.dto.StudentResponse;
import in.koreatech.koin.domain.user.dto.StudentUpdateRequest;
import in.koreatech.koin.domain.user.dto.StudentUpdateResponse;
import in.koreatech.koin.domain.user.dto.UserLoginRequest;
import in.koreatech.koin.domain.user.dto.UserLoginResponse;
import in.koreatech.koin.domain.user.dto.UserPasswordCheckRequest;
import in.koreatech.koin.domain.user.dto.UserTokenRefreshRequest;
import in.koreatech.koin.domain.user.dto.UserTokenRefreshResponse;
import in.koreatech.koin.global.auth.Auth;
import in.koreatech.koin.global.host.ServerURL;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -254,4 +240,18 @@ ResponseEntity<Void> checkPassword(
@Valid @RequestBody UserPasswordCheckRequest request,
@Auth(permit = {STUDENT, OWNER, COOP}) Integer userId
);

@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
@ApiResponse(responseCode = "401", content = @Content(schema = @Schema(hidden = true))),
}
)
@Operation(summary = "로그인 여부 확인")
@SecurityRequirement(name = "Jwt Authentication")
@GetMapping("/user/check/login")
ResponseEntity<Void> checkLogin(
@ParameterObject @ModelAttribute(value = "access_token")
@Valid UserAccessTokenRequest request
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.net.URI;

import in.koreatech.koin.domain.user.dto.*;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -18,24 +20,6 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

안 쓰는 import 정리 굿

import in.koreatech.koin.domain.user.dto.AuthResponse;
import in.koreatech.koin.domain.user.dto.AuthTokenRequest;
import in.koreatech.koin.domain.user.dto.CoopResponse;
import in.koreatech.koin.domain.user.dto.EmailCheckExistsRequest;
import in.koreatech.koin.domain.user.dto.FindPasswordRequest;
import in.koreatech.koin.domain.user.dto.NicknameCheckExistsRequest;
import in.koreatech.koin.domain.user.dto.StudentLoginRequest;
import in.koreatech.koin.domain.user.dto.StudentLoginResponse;
import in.koreatech.koin.domain.user.dto.StudentRegisterRequest;
import in.koreatech.koin.domain.user.dto.StudentResponse;
import in.koreatech.koin.domain.user.dto.StudentUpdateRequest;
import in.koreatech.koin.domain.user.dto.StudentUpdateResponse;
import in.koreatech.koin.domain.user.dto.UserLoginRequest;
import in.koreatech.koin.domain.user.dto.UserLoginResponse;
import in.koreatech.koin.domain.user.dto.UserPasswordChangeRequest;
import in.koreatech.koin.domain.user.dto.UserPasswordCheckRequest;
import in.koreatech.koin.domain.user.dto.UserTokenRefreshRequest;
import in.koreatech.koin.domain.user.dto.UserTokenRefreshResponse;
import in.koreatech.koin.domain.user.service.StudentService;
import in.koreatech.koin.domain.user.service.UserService;
import in.koreatech.koin.global.auth.Auth;
Expand Down Expand Up @@ -171,6 +155,15 @@ public ResponseEntity<Void> findPassword(
return new ResponseEntity<>(HttpStatusCode.valueOf(201));
}

@GetMapping("/user/check/login")
public ResponseEntity<Void> checkLogin(
@ParameterObject @ModelAttribute(value = "access_token")
@Valid UserAccessTokenRequest request
) {
userService.checkLogin(request.accessToken());
return ResponseEntity.ok().build();
}

@PostMapping("/user/check/password")
public ResponseEntity<Void> checkPassword(
@Valid @RequestBody UserPasswordCheckRequest request,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package in.koreatech.koin.domain.user.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

public record UserAccessTokenRequest(
@Schema(description = "access_token", example = "eyJ0eXAiOiJKV1QiLCJhbGcIUzUxMic9.eyJpZCI6NTM5NhwIjoxkzI3MTA5ODE5fQ.rLEYGQfKI5_24ZlwLVwlgwnriqySPKwXNOeTRrbmxoCtlOzCVvM8FFcO9BA2vkqsmhf-w", requiredMode = REQUIRED)
@NotBlank(message = "access_token을 입력해주세요.")
@JsonProperty("access_token")
String accessToken
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public void withdraw(Integer userId) {
eventPublisher.publishEvent(new UserDeleteEvent(user.getEmail(), user.getUserType()));
}

public void checkLogin(String accessToken) {
jwtProvider.getUserId(accessToken);
}

public void checkPassword(UserPasswordCheckRequest request, Integer userId) {
User user = userRepository.getById(userId);
if (!user.isSamePassword(passwordEncoder, request.password())) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/in/koreatech/koin/acceptance/UserApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -706,4 +706,17 @@ void setup() {
)
.andExpect(status().isBadRequest());
}

@Test
void 사용자가_로그인상태인지_확인한다() throws Exception {
Student student = userFixture.준호_학생();
String accessToken = userFixture.getToken(student.getUser());

mockMvc.perform(
get("/user/check/login")
.param("access_token", accessToken)
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk());
}
}
Loading