-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAuthController.java
More file actions
52 lines (43 loc) · 1.59 KB
/
AuthController.java
File metadata and controls
52 lines (43 loc) · 1.59 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
41
42
43
44
45
46
47
48
49
50
51
52
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.validation.Valid;
import java.util.List;
@Controller
@AllArgsConstructor
@RequestMapping("/auth")
public class AuthController {
UserService userService;
@GetMapping("/login")
public String login(){
return "auth/login";
}
@GetMapping("/register")
public String register(Model model){
model.addAttribute("user", new User());
return "auth/register";
}
@PostMapping("/register")
public String registerUser(Model model, @Valid User user, BindingResult bindingResult){
if(bindingResult.hasErrors()){
model.addAttribute("errorMessage", "User not Registered!");
model.addAttribute("bindingResult", bindingResult);
return "auth/register";
}
List<Object> userPresentObj = userService.isUserPresent(user);
if((Boolean) userPresentObj.get(0)){
model.addAttribute("successMessage", userPresentObj.get(1));
return "auth/register";
}
userService.saveUser(user);
model.addAttribute("successMessage", "User registered successfully!");
return "auth/login";
}
}