smartproc-be/src/main/java/com/iconplus/smartproc/controller/AuthenticationController.java
2023-05-29 19:19:25 +07:00

84 lines
3.5 KiB
Java

package com.iconplus.smartproc.controller;
import com.iconplus.smartproc.exception.BusinessException;
import com.iconplus.smartproc.helper.model.EmptyRequest;
import com.iconplus.smartproc.helper.model.EmptyResponse;
import com.iconplus.smartproc.model.request.*;
import com.iconplus.smartproc.model.response.GlobalResponse;
import com.iconplus.smartproc.model.response.InitForgotPasswordResponse;
import com.iconplus.smartproc.model.response.RefreshTokenResponse;
import com.iconplus.smartproc.service.authentication.*;
import org.springframework.web.bind.annotation.*;
@CrossOrigin(origins = "${fe.server}", allowCredentials = "true")
@RestController
@RequestMapping("/api/authentication")
public class AuthenticationController {
private final LoginService loginService;
private final TokenManagementService tokenManagementService;
private final LogoutService logoutService;
private final ChangePasswordService changePasswordService;
private final ForgotPasswordService forgotPasswordService;
private final InitForgotPasswordService initForgotPasswordService;
public AuthenticationController(LoginService loginService,
TokenManagementService tokenManagementService,
LogoutService logoutService,
ChangePasswordService changePasswordService,
ForgotPasswordService forgotPasswordService,
InitForgotPasswordService initForgotPasswordService) {
this.loginService = loginService;
this.tokenManagementService = tokenManagementService;
this.logoutService = logoutService;
this.changePasswordService = changePasswordService;
this.forgotPasswordService = forgotPasswordService;
this.initForgotPasswordService = initForgotPasswordService;
}
@PostMapping("/login")
public GlobalResponse getLoginResponse(@RequestBody LoginRequest loginRequest) {
try {
var loginResponse = loginService.execute(loginRequest);
return GlobalResponse.builder()
.isOk(true)
.message("Succes")
.data(loginResponse)
.build();
} catch (BusinessException exception) {
return GlobalResponse.builder()
.isOk(false)
.message(exception.getErrorMessage())
.build();
}
}
@PostMapping("/refresh-token")
public RefreshTokenResponse getRefreshToken(@RequestBody RefreshTokenRequest refreshTokenRequest) {
return tokenManagementService.execute(refreshTokenRequest);
}
@PostMapping("/logout")
public EmptyResponse logoutUser(EmptyRequest request) {
return logoutService.execute(request);
}
@PostMapping("/change-password")
public EmptyResponse changePassword(@RequestBody ChangePasswordRequest changePasswordRequest) {
return changePasswordService.execute(changePasswordRequest);
}
@PostMapping("/forgot-password")
public EmptyResponse forgotPassword(@RequestBody ForgotPasswordRequest forgotPasswordRequest) {
return forgotPasswordService.execute(forgotPasswordRequest);
}
@PostMapping("/init/forgot-password")
public InitForgotPasswordResponse initForgotPassword(@RequestBody InitForgotPasswordRequest initForgotPasswordRequest) {
return initForgotPasswordService.execute(initForgotPasswordRequest);
}
}