Skip to content

Commit

Permalink
Feat: 회원가입, 로그인 API #1
Browse files Browse the repository at this point in the history
  • Loading branch information
tokyj515 committed Jul 30, 2023
1 parent 7c60524 commit faba928
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 113 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ dependencies {
// gson
implementation 'com.google.code.gson:gson:2.9.0'

//implementation 'io.awspring.cloud:spring-cloud-starter-aws:2.3.5'
//implementation 'org.springframework.cloud:spring-cloud-start-aws:2.0.1.RELEASE'
//implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'


implementation 'org.json:json:20200518'


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.neoul.dto.TokenRes;
import com.example.neoul.dto.UserReq;
import com.example.neoul.global.entity.ApiResponse;
import com.example.neoul.global.entity.BaseEntity;
import com.example.neoul.global.exception.BadRequestException;
import com.example.neoul.service.AuthService;
Expand Down Expand Up @@ -29,12 +30,12 @@ public class AuthController {
"카카오의 access_token을 반환 후" +
"access_token을 /kakao/login의 요청에 넣어서 우리 사이트의 로그인하고 그 결과를 얻음")
@GetMapping("/kakao")
public String getAccessTokenKakao(@RequestParam String code) {
public ApiResponse<String> getAccessTokenKakao(@RequestParam String code) {
String accessToken=authService.getKakaoAccessToken(code);
System.out.println("code for kakaoServer : " + code);
System.out.println("for /oauth/kakao : " + accessToken);

return accessToken;
return new ApiResponse<>(accessToken);
}

@ApiOperation(value = "카카오 로그인", notes = "카카오 로그인")
Expand Down
62 changes: 30 additions & 32 deletions src/main/java/com/example/neoul/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.example.neoul.controller;


import com.example.neoul.dto.TokenRes;
import com.example.neoul.dto.UserReq;
import com.example.neoul.dto.UserRes;
import com.example.neoul.entity.user.User;
import com.example.neoul.global.entity.ApiResponse;
import com.example.neoul.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;


@RequiredArgsConstructor
@RestController
Expand All @@ -19,38 +22,33 @@ public class UserController {
private final UserService userService;


// @ApiOperation(value = "현재 로그인 유저", notes = "현재 로그인 유저")
// @GetMapping("/now")
// public BaseResponse<User> now(){
//
// if(userService.findNowLoginUser() != null){
// return BaseResponse.ok(SUCCESS, userService.findNowLoginUser());
// }
//
// return BaseResponse.ok(USER_NEED_TO_LOGIN);
// }
@ApiOperation(value = "현재 로그인 유저(이거 지금 실행x)", notes = "현재 로그인 유저")
@GetMapping("/now")
public ApiResponse<User> now(){
return new ApiResponse<>(userService.findNowLoginUser());
}


// @ApiOperation(value = "회원가입", notes = "회원가입")
// @PostMapping("/signup")
// public BaseResponse<User> signup(@RequestBody UserReq.SignupUserReq signupUserReq) {
//
// if(!signupUserReq.isEmailVerified())
// return BaseResponse.ok(USER_EMAIL_VERIFIED_FALSE);
//
// return BaseResponse.ok(SUCCESS, userService.signup(signupUserReq));
// }
//
//
//
//
// @ApiOperation(value = "로그인", notes = "로그인")
// @PostMapping("/login")
// public BaseResponse<TokenRes> signup(@RequestBody UserReq.LoginUserReq loginUserReq) {
//
// return BaseResponse.ok(SUCCESS, userService.login(loginUserReq));
// }
//
@ApiOperation(value = "회원가입", notes = "회원가입")
@PostMapping("/signup")
public ApiResponse<UserRes.UserDetailDto> signup(@RequestBody UserReq.SignupUserDto signupUserDto) {
//이메일 형식 체크

//비밀번호 형식 체크


return new ApiResponse<>(userService.signup(signupUserDto));
}




@ApiOperation(value = "로그인", notes = "로그인")
@PostMapping("/login")
public ApiResponse<TokenRes> signup(@RequestBody UserReq.LoginUserDto loginUserDto) {
return new ApiResponse<>(userService.login(loginUserDto));
}

//
// @ApiOperation(value = "회원 탈퇴", notes = "회원 탈퇴")
// @GetMapping("/withdrawal")
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/example/neoul/dto/TokenRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@Getter
public class TokenRes {
private String username;
private boolean firstLogin;
private String accessToken;
private String refreshToken;
}
11 changes: 4 additions & 7 deletions src/main/java/com/example/neoul/dto/UserReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class UserReq {
@AllArgsConstructor
@Setter
@Getter
public static class LoginUserReq {
public static class LoginUserDto {
@ApiModelProperty(example = "[email protected]")
private String username;
@ApiModelProperty(example = "test1234")
Expand All @@ -22,7 +22,7 @@ public static class LoginUserReq {
@AllArgsConstructor
@Setter
@Getter
public static class SocialLoginUserReq {
public static class SocialLoginUserDto {
@ApiModelProperty(example = "[email protected]")
private String username;
@ApiModelProperty(example = "kakao")
Expand All @@ -37,14 +37,11 @@ public static class SocialLoginUserReq {
@AllArgsConstructor
@Setter
@Getter
public static class SignupUserReq {
public static class SignupUserDto {
private String username;
private String password;
private String name;
private String imageUrl;

@ApiModelProperty(example = "true or false")
private boolean emailVerified;
}


Expand All @@ -53,7 +50,7 @@ public static class SignupUserReq {
@AllArgsConstructor
@Setter
@Getter
public static class EmailAuthReq {
public static class EmailAuthDto {
private String email;
}

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/example/neoul/dto/UserRes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.neoul.dto;

import com.example.neoul.entity.user.User;
import lombok.*;

public class UserRes {
Expand All @@ -16,6 +17,31 @@ public static class SignupUserRes {
}


@Builder
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public static class UserDetailDto {
private String username;
private String name;
private String phone;
private String imageUrl;
private String social;
private boolean firstLogin;

public static UserDetailDto toDto(User user){
return UserDetailDto.builder()
.username(user.getUsername())
.name(user.getName())
.phone(user.getPhone())
.imageUrl(user.getImageUrl())
.social(user.getSocial())
.firstLogin(user.isFirstLogin())
.build();
}
}



@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ public class ServerErrorException extends RuntimeException {
public ServerErrorException() {
super("서버 내부 에러입니다.");
}

public ServerErrorException(String s) {
}
}

16 changes: 8 additions & 8 deletions src/main/java/com/example/neoul/global/jwt/TokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,29 @@ public void afterPropertiesSet() {
// .compact();
// }

public String createToken(Long userIdx) {
public String createToken(Long userId) {

long now = (new Date()).getTime();
Date validity = new Date(now + this.accessTime);

return Jwts.builder()
//.setSubject(authentication.getName())
.claim("userIdx",userIdx)
.claim("userId",userId)
.setIssuedAt(new Date())
//.claim(AUTHORITIES_KEY, authorities)
.signWith(SignatureAlgorithm.HS512, secret)
.setExpiration(validity)
.compact();
}

public String createRefreshToken(Long userIdx) {
public String createRefreshToken(Long userId) {

long now = (new Date()).getTime();
Date validity = new Date(now + this.refreshTime);

return Jwts.builder()
//.setSubject(authentication.getName())
.claim("userIdx",userIdx)
.claim("userId",userId)
.setIssuedAt(new Date())
//.claim(AUTHORITIES_KEY, authorities)
.signWith(SignatureAlgorithm.HS512, refreshSecret)
Expand All @@ -115,12 +115,12 @@ public String createRefreshToken(Long userIdx) {
}


public GenerateToken createAllToken(Long userIdx){
String accessToken=createToken(userIdx);
String refreshToken=createRefreshToken(userIdx);
public GenerateToken createAllToken(Long userId){
String accessToken = createToken(userId);
String refreshToken = createRefreshToken(userId);

// //redis에 리프레시토큰 저장
// redisService.saveToken(String.valueOf(userIdx),refreshToken, (System.currentTimeMillis()+ refreshTime*1000));
// redisService.saveToken(String.valueOf(userId),refreshToken, (System.currentTimeMillis()+ refreshTime*1000));

return new GenerateToken(accessToken,refreshToken);
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/example/neoul/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,27 @@ public TokenRes createAndLoginKakaoUser(UserReq.SocialReq socialReq) {

//카카오 계정의 이메일과 현재 일반 회원가입한 이메일 중에서 같은 것이 없다면 카카오 계정의 이메일로 회원가입
if(!userRepository.existsByUsernameAndSocial(String.valueOf(email),socialReq.getSocial())){
User user= User.toSocialLoginUser(String.valueOf(email),socialReq.getSocial(),name);
User user = User.toSocialLoginUser(String.valueOf(email),socialReq.getSocial(),name);

userRepository.save(user);
}
br.close();

//로그인
User user=userRepository.findByUsernameAndSocial(String.valueOf(email),socialReq.getSocial());
Long userIdx=user.getUserId();
User user = userRepository.findByUsernameAndSocial(String.valueOf(email),socialReq.getSocial());
Long userId = user.getUserId();

GenerateToken generateToken=tokenProvider.createAllToken(userIdx);
GenerateToken generateToken = tokenProvider.createAllToken(userId);

return new TokenRes(name,generateToken.getAccessToken(),generateToken.getRefreshToken());
boolean isFirstLogin = user.isFirstLogin();
user.setFirstLogin(false); //첫 로그인 이후는 전부 0으로 바꾸기

return TokenRes.builder()
.username(user.getUsername())
.firstLogin(isFirstLogin)
.accessToken(generateToken.getAccessToken())
.refreshToken(generateToken.getRefreshToken())
.build();


} catch (IOException e) {
Expand Down
Loading

0 comments on commit faba928

Please sign in to comment.