-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAuthController.java
More file actions
40 lines (34 loc) · 1.39 KB
/
AuthController.java
File metadata and controls
40 lines (34 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.example.devSns.controller;
import com.example.devSns.dto.LoginRequest;
import com.example.devSns.dto.LoginResponse;
import com.example.devSns.dto.SignUpRequest;
import com.example.devSns.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/auth")
public class AuthController {
@Autowired
private MemberService MemberService;
// 회원 가입
@PostMapping("/signup")
public ResponseEntity<String> signup(@RequestBody SignUpRequest signUpRequest) {
// 이메일 중복 체크
if (MemberService.isEmailExists(signUpRequest.getEmail())) {
return ResponseEntity.status(400).body("Email already exists");
}
// 회원가입 처리
MemberService.register(signUpRequest);
return ResponseEntity.status(201).body("Sign up successful");
}
// 로그인(JWT 발급)
@PostMapping("/login")
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest loginRequest) {
String token = MemberService.login(loginRequest);
if (token == null) {
return ResponseEntity.status(401).body(new LoginResponse("Login failed")); // 로그인 실패 시
}
return ResponseEntity.ok(new LoginResponse(token));
}
}