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/14 login res #93

Merged
merged 2 commits into from
Aug 18, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.moreveiw.domain.member.controller;

import com.example.moreveiw.domain.member.model.dao.Member;
import com.example.moreveiw.domain.member.model.dto.TokenDto;
import com.example.moreveiw.domain.member.model.dto.request.MemberLoginRequest;
import com.example.moreveiw.domain.member.model.dto.request.MemberRequest;
Expand All @@ -20,6 +21,7 @@
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.*;

@RestController
Expand Down Expand Up @@ -48,15 +50,15 @@ public ResponseEntity<TokenDto> authorize(@Valid @RequestBody MemberLoginRequest
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);

Long memberId = memberService.findMemberIdByEmail(request.getEmail());
Member member = memberService.findByEmailOptional(request.getEmail())
.orElseThrow(() -> new UsernameNotFoundException("No member found with email: " + request.getEmail()));

String jwt = tokenProvider.createToken(authentication);

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(JwtFilter.AUTHORIZATION_HEADER, "Bearer " + jwt);


TokenDto tokenDto = new TokenDto(jwt, memberId);
TokenDto tokenDto = new TokenDto(jwt, member.getId(), member.getName(), member.getEmail());

return new ResponseEntity<>(tokenDto, httpHeaders, HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public class TokenDto {

private String token;
private Long memberId;
private String name;
private String email;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.moreveiw.domain.member.model.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import lombok.*;
Expand All @@ -10,9 +11,13 @@
@AllArgsConstructor
@NoArgsConstructor
public class MemberLoginRequest {

@NotBlank
@Email
@Schema(description = "로그인 이메일", example = "[email protected]")
private String email;

@NotBlank
@Schema(description = "비밀번호", example = "asdf1234!")
private String password;
}
Loading