45 lines
1.6 KiB
Plaintext
45 lines
1.6 KiB
Plaintext
package com.healthcare.ohctech.controller;
|
|
|
|
import com.healthcare.ohctech.dto.LoginDto;
|
|
import com.healthcare.ohctech.dto.LoginResponseDto;
|
|
import com.healthcare.ohctech.dto.RegisterDto;
|
|
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
@RestController
|
|
@RequestMapping("/auth")
|
|
public class AuthenticationController {
|
|
private AuthServiceImpl authServiceImpl;
|
|
|
|
public AuthenticationController(AuthServiceImpl authServiceImpl) {
|
|
this.authServiceImpl = authServiceImpl;
|
|
}
|
|
|
|
@PostMapping("/register")
|
|
public ResponseEntity<?> register(@RequestBody RegisterDto registerDto) {
|
|
try {
|
|
authServiceImpl.register(registerDto);
|
|
} catch (Exception ex) {
|
|
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
|
|
}
|
|
return new ResponseEntity<>("Success", HttpStatus.CREATED);
|
|
}
|
|
|
|
@PostMapping("/login")
|
|
public LoginResponseDto login(@RequestBody LoginDto loginDto) {
|
|
LoginResponseDto loginResponseDto = authServiceImpl.login(loginDto);
|
|
return loginResponseDto;
|
|
}
|
|
|
|
@PostMapping("/refresh")
|
|
public ResponseEntity<String> getTokenByRefreshToken(@RequestParam Integer userId) {
|
|
String token = authServiceImpl.getTokenByRefreshToken(userId);
|
|
if (token == null || token.isEmpty()) {
|
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
}
|
|
return new ResponseEntity<>(token, HttpStatus.OK);
|
|
}
|
|
}
|