Skip to content

Commit

Permalink
change import constants
Browse files Browse the repository at this point in the history
  • Loading branch information
QuyDang1108 committed Sep 11, 2024
1 parent 0a954a2 commit 3b56d9f
Showing 1 changed file with 31 additions and 38 deletions.
69 changes: 31 additions & 38 deletions src/main/java/com/fjb/sunrise/controllers/AuthController.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package com.fjb.sunrise.controllers;

import static com.fjb.sunrise.utils.Constants.ApiConstant.AUTH_REDIRECT_LOGIN;
import static com.fjb.sunrise.utils.Constants.ApiConstant.AUTH_VIEW;
import static com.fjb.sunrise.utils.Constants.ApiConstant.CHANGE_PASSWORD_VIEW;
import static com.fjb.sunrise.utils.Constants.ApiConstant.EMAIL_OBJECT;
import static com.fjb.sunrise.utils.Constants.ApiConstant.ERROR_MESSAGE_OBJECT;
import static com.fjb.sunrise.utils.Constants.ApiConstant.LOGIN_OBJECT;
import static com.fjb.sunrise.utils.Constants.ApiConstant.NEW_PASSWORD_OBJECT;
import static com.fjb.sunrise.utils.Constants.ApiConstant.REGISTER_OBJECT;
import static com.fjb.sunrise.utils.Constants.ApiConstant.VERIFICATION_BY_EMAIL_VIEW;

import com.fjb.sunrise.dtos.requests.LoginRequest;
import com.fjb.sunrise.dtos.requests.RegisterRequest;
import com.fjb.sunrise.dtos.requests.VerificationByEmail;
import com.fjb.sunrise.services.EmailService;
import com.fjb.sunrise.services.UserService;
import com.fjb.sunrise.utils.Constants;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
Expand All @@ -36,37 +27,38 @@ public class AuthController {
@GetMapping("/login")
public ModelAndView indexLogin(@RequestParam(value = "error", required = false) String error) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName(AUTH_VIEW);
modelAndView.addObject(LOGIN_OBJECT, new LoginRequest());
modelAndView.addObject(REGISTER_OBJECT, new RegisterRequest());
modelAndView.setViewName(Constants.ApiConstant.AUTH_VIEW);
modelAndView.addObject(Constants.ApiConstant.LOGIN_OBJECT, new LoginRequest());
modelAndView.addObject(Constants.ApiConstant.REGISTER_OBJECT, new RegisterRequest());
if (error != null) {
modelAndView.addObject(ERROR_MESSAGE_OBJECT, "Đăng nhập không thành công!");
modelAndView.addObject(Constants.ApiConstant.ERROR_MESSAGE_OBJECT, "Đăng nhập không thành công!");
}
return modelAndView;
}

@GetMapping("/register")
public ModelAndView indexRegister() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName(AUTH_VIEW);
modelAndView.addObject(LOGIN_OBJECT, new LoginRequest());
modelAndView.addObject(REGISTER_OBJECT, new RegisterRequest());
modelAndView.setViewName(Constants.ApiConstant.AUTH_VIEW);
modelAndView.addObject(Constants.ApiConstant.LOGIN_OBJECT, new LoginRequest());
modelAndView.addObject(Constants.ApiConstant.REGISTER_OBJECT, new RegisterRequest());
return modelAndView;
}

@PostMapping("/register")
public ModelAndView doRegister(@ModelAttribute(REGISTER_OBJECT) RegisterRequest registerRequest) {
public ModelAndView doRegister(@ModelAttribute(Constants.ApiConstant.REGISTER_OBJECT)
RegisterRequest registerRequest) {
//setup object for view
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName(AUTH_VIEW);
modelAndView.addObject(LOGIN_OBJECT, new LoginRequest());
modelAndView.addObject(REGISTER_OBJECT, new RegisterRequest());
modelAndView.setViewName(Constants.ApiConstant.AUTH_VIEW);
modelAndView.addObject(Constants.ApiConstant.LOGIN_OBJECT, new LoginRequest());
modelAndView.addObject(Constants.ApiConstant.REGISTER_OBJECT, new RegisterRequest());

// implement register for user
if (userService.checkRegister(registerRequest)) {
modelAndView.setViewName(AUTH_REDIRECT_LOGIN);
modelAndView.setViewName(Constants.ApiConstant.AUTH_REDIRECT_LOGIN);
} else {
modelAndView.addObject(ERROR_MESSAGE_OBJECT, "Đăng kí không thành công");
modelAndView.addObject(Constants.ApiConstant.ERROR_MESSAGE_OBJECT, "Đăng kí không thành công");
}

return modelAndView;
Expand All @@ -75,21 +67,22 @@ public ModelAndView doRegister(@ModelAttribute(REGISTER_OBJECT) RegisterRequest
@GetMapping("/forgotPassword")
public ModelAndView indexForgotPassword() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName(VERIFICATION_BY_EMAIL_VIEW);
modelAndView.addObject(EMAIL_OBJECT, "");
modelAndView.setViewName(Constants.ApiConstant.VERIFICATION_BY_EMAIL_VIEW);
modelAndView.addObject(Constants.ApiConstant.EMAIL_OBJECT, "");
return modelAndView;
}

@PostMapping("/sendToEmail")
public ModelAndView doSendCodeToEmail(@ModelAttribute(EMAIL_OBJECT) String email) {
public ModelAndView doSendCodeToEmail(@ModelAttribute(Constants.ApiConstant.EMAIL_OBJECT)
String email) {
ModelAndView modelAndView = new ModelAndView();

VerificationByEmail verification = new VerificationByEmail(email, LocalDateTime.now());

String message = emailService.sendEmail(verification);

modelAndView.setViewName(VERIFICATION_BY_EMAIL_VIEW);
modelAndView.addObject(ERROR_MESSAGE_OBJECT, message);
modelAndView.setViewName(Constants.ApiConstant.VERIFICATION_BY_EMAIL_VIEW);
modelAndView.addObject(Constants.ApiConstant.ERROR_MESSAGE_OBJECT, message);

return modelAndView;
}
Expand All @@ -101,31 +94,31 @@ public ModelAndView doVerify(@RequestParam("code") String code) {
String message = emailService.checkCode(code);

if (message != null) {
modelAndView.setViewName(VERIFICATION_BY_EMAIL_VIEW);
modelAndView.addObject(ERROR_MESSAGE_OBJECT, message);
modelAndView.setViewName(Constants.ApiConstant.VERIFICATION_BY_EMAIL_VIEW);
modelAndView.addObject(Constants.ApiConstant.ERROR_MESSAGE_OBJECT, message);
return modelAndView;
}

String email = emailService.getEmailFromCode(code);

modelAndView.setViewName(CHANGE_PASSWORD_VIEW);
modelAndView.addObject(EMAIL_OBJECT, email);
modelAndView.addObject(NEW_PASSWORD_OBJECT, "");
modelAndView.setViewName(Constants.ApiConstant.CHANGE_PASSWORD_VIEW);
modelAndView.addObject(Constants.ApiConstant.EMAIL_OBJECT, email);
modelAndView.addObject(Constants.ApiConstant.NEW_PASSWORD_OBJECT, "");
return modelAndView;
}


@PostMapping("/changePassword")
public ModelAndView doChangePassword(@ModelAttribute(EMAIL_OBJECT) String email,
@ModelAttribute(NEW_PASSWORD_OBJECT) String password) {
public ModelAndView doChangePassword(@ModelAttribute(Constants.ApiConstant.EMAIL_OBJECT) String email,
@ModelAttribute(Constants.ApiConstant.NEW_PASSWORD_OBJECT) String password) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName(AUTH_REDIRECT_LOGIN);
modelAndView.setViewName(Constants.ApiConstant.AUTH_REDIRECT_LOGIN);

String message = userService.changePassword(email, password);

if (message != null) {
modelAndView.setViewName(VERIFICATION_BY_EMAIL_VIEW);
modelAndView.addObject(ERROR_MESSAGE_OBJECT, message);
modelAndView.setViewName(Constants.ApiConstant.VERIFICATION_BY_EMAIL_VIEW);
modelAndView.addObject(Constants.ApiConstant.ERROR_MESSAGE_OBJECT, message);
}

return modelAndView;
Expand Down

0 comments on commit 3b56d9f

Please sign in to comment.