Skip to content

Commit

Permalink
Merge pull request #7 from handong-app/#4/login-backend
Browse files Browse the repository at this point in the history
처음 로그인 유저일경우 DB에 유저 생성
  • Loading branch information
Sangdeveloper7 authored Sep 25, 2024
2 parents 12e14d0 + fa25c3d commit fe400b7
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 286 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ sourceSets {
}
}

processResources { dependsOn "copyReactBuildFiles" }
processResources { dependsOn "copyReactBuildFiles" }

task installReact(type: Exec) {
workingDir "$frontendDir"
Expand Down
94 changes: 56 additions & 38 deletions src/main/java/com/thc/realspr/controller/TbuserController.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.thc.realspr.controller;

import com.thc.realspr.domain.Tbuser;
import com.thc.realspr.dto.GoogleLoginRequest;
import com.thc.realspr.dto.GoogleLoginResponse;
import com.thc.realspr.exception.NoAuthorizationException;
import com.thc.realspr.dto.TbuserDto;
import com.thc.realspr.service.GoogleAuthService;
import com.thc.realspr.service.TbuserService;
import com.thc.realspr.util.TokenFactory;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/api/tbuser")
Expand All @@ -22,48 +22,72 @@ public class TbuserController {

private final GoogleAuthService googleAuthService;


public TbuserController(TbuserService tbuserService, GoogleAuthService googleAuthService) {
this.tbuserService = tbuserService;
this.googleAuthService = googleAuthService;
}


@PostMapping("")
public Map<String, Object> create(@RequestBody Map<String, Object> param){
System.out.println(param);
return tbuserService.create(param);
}
@PutMapping("")
public Map<String, Object> update(@RequestBody Map<String, Object> param){
System.out.println(param);
return tbuserService.update(param);
}
@GetMapping("/get/{id}")
public Map<String, Object> detail(@PathVariable("id") String id){
System.out.println(id);
return tbuserService.get(id);
// CREATE: POST 요청으로 새로운 사용자 생성
@GetMapping("")
public Map<String, Object> create(@RequestParam Map<String, Object> params) {
return tbuserService.create(params);
}

// @PutMapping("")
// public Map<String, Object> update(@RequestBody Map<String, Object> param){
// System.out.println(param);
// return tbuserService.update(param);
// }
// @GetMapping("/get/{id}")
// public ResponseEntity<Tbuser> detail(@PathVariable("id") String id) {
// System.out.println(id);
// Tbuser response = tbuserService.get(id);
// return ResponseEntity.ok(response);
// }

// Google 로그인 처리
@PostMapping("/login/google")
public ResponseEntity<GoogleLoginResponse> loginWithGoogle(@RequestBody GoogleLoginRequest request) {
String email = googleAuthService.verifyGoogleToken(request.getCredential());

System.out.println("동작중입니다. ");

System.out.println(email);
// Google 로그인 처리
// @PostMapping("/login/google")
// public ResponseEntity<GoogleLoginResponse> loginWithGoogle(@RequestBody GoogleLoginRequest request) {
// String email = googleAuthService.verifyGoogleToken(request.getCredential());
// String name = googleAuthService.verifyGoogleToken(request.getCredential());
// LocalDateTime now = LocalDateTime.now();
// String uuid = UUID.randomUUID().toString().replace("-", "");
// Tbuser tbuser = Tbuser.of(uuid, name, now, now);
//
// System.out.println("동작중입니다.");
// System.out.println("이메일: " + email);
// System.out.println("이름: " + name);
// System.out.println("UUID: " + uuid);
//
//
// System.out.println("동작중입니다. ");
//
// if (email == null || !email.endsWith("@handong.ac.kr")) {
// throw new NoAuthorizationException("Unauthorized user");
// }
//
//
// String refreshToken = TokenFactory.issueRefreshToken(email); // 리프레시 토큰 발급
//
// System.out.println(refreshToken);
// return ResponseEntity.ok(new GoogleLoginResponse(refreshToken));
//
// }

if (email == null || !email.endsWith("@handong.ac.kr")) {
throw new NoAuthorizationException("Unauthorized user");
}

@PostMapping("/login/google")
public ResponseEntity<GoogleLoginResponse> loginWithGoogle(@RequestBody GoogleLoginRequest request) {
// 서비스 계층에서 유저 로그인 처리
Tbuser tbuser = tbuserService.loginWithGoogle(request.getCredential());

String refreshToken = TokenFactory.issueRefreshToken(email); // 리프레시 토큰 발급
// 리프레시 토큰 발급
String refreshToken = TokenFactory.issueRefreshToken(tbuser.getEmail());

System.out.println(refreshToken);
// 응답 리턴
return ResponseEntity.ok(new GoogleLoginResponse(refreshToken));

}

// @ResponseBody
Expand All @@ -78,9 +102,6 @@ public ResponseEntity<GoogleLoginResponse> loginWithGoogle(@RequestBody GoogleLo
// }





// @PostMapping("/token/refresh")
// public ResponseEntity<String> refreshAccessToken(@RequestBody String refreshToken) {
// // 리프레시 토큰 검증 후 액세스 토큰 생성
Expand All @@ -90,7 +111,4 @@ public ResponseEntity<GoogleLoginResponse> loginWithGoogle(@RequestBody GoogleLo
// }





}
}
84 changes: 55 additions & 29 deletions src/main/java/com/thc/realspr/domain/Tbuser.java
Original file line number Diff line number Diff line change
@@ -1,58 +1,84 @@
package com.thc.realspr.domain;

import com.thc.realspr.config.Role;
import com.thc.realspr.dto.TbuserDto;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import org.antlr.v4.runtime.misc.NotNull;
import org.checkerframework.common.aliasing.qual.Unique;

import java.time.LocalDateTime;
import java.util.UUID;

@Getter
@Entity
public class Tbuser {
@Id private String id;
@Setter @Column(nullable = false) private String deleted; // 삭제여부

@Setter @Column(nullable = false) private String username; // 사용자아이디
@Setter @Column(nullable = false) private String password; // 비번
/*
* - email
- uuid
- 이름
- last login time
- modified_at
- created_at*/
@Id
@Setter @Column(nullable = false) private String id; // 사용자아이디
@Unique @Setter @Column(nullable = false) private String email; // 이메일
@Setter @Column(nullable = false) private String name;
@Setter @Column(nullable = false) private String nick;
@Setter @Column(nullable = false) private String phone;
@Setter @Column(nullable = false) private String mpic; // 프로필 사진
@Setter @Column(nullable = false, length=2000000) @Lob private String content; // 본문
@Setter @Column(nullable = false) private LocalDateTime last_login_time; // 최근 로그인 시간
@Setter @Column(nullable = false) private LocalDateTime modified_at; // 수정날짜
@Setter @Column(nullable = false) private LocalDateTime created_at; // 생성 날짜


protected Tbuser() {
}

@Enumerated(EnumType.STRING)
@NotNull
private Role role;

private Tbuser(String email, String uuid, String name, LocalDateTime last_login_time, LocalDateTime modified_at, LocalDateTime created_at) {
this.email = email;
this.id = uuid;
this.name = name;
this.last_login_time = last_login_time;
this.modified_at = modified_at;
this.created_at = created_at;
}

protected Tbuser(){}
private Tbuser(String username, String password, String name, String nick, String phone, String mpic, String content, Role role) {
this.username = username;
this.password = password;
public Tbuser(String email, String uuid, String name, LocalDateTime last_login_time, LocalDateTime created_at) {
this.email = email;
this.id = uuid;
this.name = name;
this.nick = nick;
this.phone = phone;
this.mpic = mpic;
this.content = content;
this.role = role;
this.last_login_time = last_login_time;
this.created_at = created_at;
}

public Tbuser(String email) {
this.email = email;
}


// TODO: role 추가 해야 함 .
public static Tbuser of(String username, String password){
return new Tbuser(username, password, "", "", "", "", "", null);
public static Tbuser of(String email, String uuid, String name, LocalDateTime last_login_time, LocalDateTime modified_at, LocalDateTime created_at) {
return new Tbuser(email, uuid, name, last_login_time, modified_at, created_at);
}




@PrePersist
public void onPrePersist() {
this.id = UUID.randomUUID().toString().replace("-", "");
this.deleted = "N";
}

public String getRoleKey(){
return this.role.getKey();
public TbuserDto.CreateResDto toCreateResDto() {
return TbuserDto.CreateResDto.builder().email(this.getEmail()).build();
}



}
// public String getRoleKey(){
// return this.role.getKey();
// }


}
Loading

0 comments on commit fe400b7

Please sign in to comment.