aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/berzerkula/builddb/controllers/AccountController.java
blob: bfaf669fbcc5634b58e6df339d13c8e56aecdfd7 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.berzerkula.builddb.controllers;

import jakarta.validation.Valid;
import org.berzerkula.builddb.BuilddbConstants;
import org.berzerkula.builddb.models.AppUser;
import org.berzerkula.builddb.models.RegisterDto;
import org.berzerkula.builddb.repositories.AppUserRepository;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;

@Controller
public class AccountController {

	private final AppUserRepository repo;

	public AccountController(AppUserRepository repo) {
		this.repo = repo;
	}

	@GetMapping("/profile")
	public String profile(Authentication auth, Model model) {
		AppUser user = repo.findByEmail(auth.getName());
		model.addAttribute("appUser", user);
		
		return "profile";
	}

	@GetMapping("/login")
	public String login() {
		return "login";
	}

	@GetMapping("/register")
	public String register(Model model) {
		RegisterDto registerDto = new RegisterDto();
		model.addAttribute(registerDto);
		model.addAttribute("success", false);
		return BuilddbConstants.VIEW_REGISTER;
	}

	@PostMapping("/register")
	public String register(
			Model model,
			@Valid @ModelAttribute RegisterDto registerDto,
            BindingResult result
			) {
		
		if (!registerDto.getPassword().equals(registerDto.getConfirmPassword())) {
    		result.addError(
    				new FieldError(BuilddbConstants.DTO_REGISTER, "confirmPassword"
    						, "Password and Confirm Password do not match")
    		);
    	}

		if (repo.findByEmail(registerDto.getEmail()) != null) {
			result.addError(
    				new FieldError(BuilddbConstants.DTO_REGISTER, "email"
    						, "Email address is already used")
    		);
		}
		
		
		if (result.hasErrors()) {
    		return BuilddbConstants.VIEW_REGISTER;
    	}


		try {
			// create new account
			var bCryptEncoder = new BCryptPasswordEncoder();


			AppUser newUser = new AppUser();
			newUser.setFirstName(registerDto.getFirstName());
			newUser.setLastName(registerDto.getLastName());
			newUser.setEmail(registerDto.getEmail());
			newUser.setPhone(registerDto.getPhone());
			newUser.setAddress(registerDto.getAddress());
			newUser.setRole("client");
			newUser.setCreatedAt(new Date());
			newUser.setPassword(bCryptEncoder.encode(registerDto.getPassword()));

			repo.save(newUser);


			model.addAttribute(BuilddbConstants.DTO_REGISTER, new RegisterDto());
			model.addAttribute("success", true);
		}
		catch(Exception ex) {
			result.addError(
    				new FieldError(BuilddbConstants.DTO_REGISTER, "firstName"
    						, ex.getMessage())
    		);
		}
		
		return BuilddbConstants.VIEW_REGISTER;
	}
}