Skip to content

Commit b37f240

Browse files
checking user exists or not
1 parent c68c240 commit b37f240

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/main/java/com/example/treaders/controller/AllController.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,19 @@ public String Login(){
5555

5656

5757
@PostMapping("/authenticate")
58-
public String authenticate(@RequestParam String email, @RequestParam String password){
58+
public String authenticate(@RequestParam String email, @RequestParam String password,Model model){
5959
UserFormat user = UserRepo.findByEmail(email);
60+
if(user == null){
61+
model.addAttribute("errorMessage", "User doesn't exist");
62+
return "login";
63+
}
6064
if(user!=null && passwordEncoder.matches(password,user.getPassword())){
6165
UserLoggedIn = true;
6266
UserName = user.getUsername();
6367
return "redirect:/home";
6468
}else{
65-
return "redirect:/signup";
69+
model.addAttribute("errorMessage", "Wrong password");
70+
return "login";
6671
}
6772
}
6873

@@ -90,6 +95,10 @@ public String Signup(){
9095
@PostMapping("/newuser")
9196
public String addNewUser(@RequestParam String email,@RequestParam String username, @RequestParam String password,Model model){
9297
UserFormat user=new UserFormat();
98+
if(UserRepo.findByEmail(email)!=null){
99+
model.addAttribute("errorMessage","This email already exists");
100+
return "signup";
101+
}
93102
user.setEmail(email);
94103
user.setUsername(username);
95104
String regex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@#]).+$";

src/main/resources/static/css/login.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,15 @@ body {
6060
text-align: center;
6161
margin-top: 10px;
6262
}
63+
64+
/* New CSS for Error Message */
65+
.login-container .error {
66+
color: #d9534f; /* Bootstrap error color */
67+
background-color: #f2dede; /* Light red background for error messages */
68+
border: 1px solid #ebccd1; /* Border color matching the background */
69+
padding: 10px; /* Add some padding */
70+
margin-bottom: 20px; /* Space below the error message */
71+
border-radius: 5px; /* Slightly rounded corners */
72+
font-size: 0.9em; /* Slightly smaller font size */
73+
text-align: center; /* Center the error text */
74+
}

src/main/resources/templates/login.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ <h2>Login</h2>
1313
<div class="message"></div>
1414
<input type="text" name="email" placeholder="Email Id" required>
1515
<input type="password" name="password" placeholder="Password" required>
16+
<!-- Display the error message if there is one -->
17+
<div th:if="${errorMessage != null}" th:text="${errorMessage}" class="error"></div>
1618
<input type="submit" value="Login">
1719
</form>
1820
<p class="signup-text">New user? Sign up <a href="/signup">here</a>.</p>

0 commit comments

Comments
 (0)