Skip to content

Commit

Permalink
Add register form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
UdhayaShan1 committed Jun 17, 2024
1 parent c89e1c2 commit 1623827
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id 'io.spring.dependency-management' version '1.1.5'
}

group = 'com.example'
group = 'com.docker3'
version = '0.0.1-SNAPSHOT'

java {
Expand Down
Binary file not shown.
Binary file modified build/classes/java/main/com/docker3/service/AuthService.class
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
3 changes: 2 additions & 1 deletion src/main/java/com/docker3/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -14,7 +15,7 @@
import com.docker3.service.AuthService;
import com.docker3.service.UserService;

@RestController
@Controller
public class UserController {
@Autowired
private AuthService authService;
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/docker3/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ public class AuthService {
@Autowired
private PasswordEncoder passwordEncoder;

public boolean isUserRegistered(String username) {
User user = userRepository.findByUsername(username);
return user != null;
}

public void registerUser(User user) throws RuntimeException {
if (userRepository.findByUsername(user.getUsername()) != null) {
throw new UserAlreadyExistsException("Username already exists!");
throw new UserAlreadyExistsException("Username already taken");
}
if (userRepository.findByEmail(user.getEmail()) != null) {
throw new UserAlreadyExistsException("Email already used");
}

user.setPassword(passwordEncoder.encode(user.getPassword()));
userRepository.save(user);
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/resources/static/js/register.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
document.getElementById('registerForm').addEventListener('submit', async function (event) {
event.preventDefault();
const username = document.getElementById('username').value.trim();
const username = document.getElementById('username').value.trim().toLowerCase();
const password = document.getElementById('password').value.trim();
const email = document.getElementById('email').value.trim();
const email = document.getElementById('email').value.trim().toLowerCase();
const errorMessageElement = document.getElementById('errorMessage');

// Clear previous error message
Expand All @@ -19,13 +19,16 @@ document.getElementById('registerForm').addEventListener('submit', async functio
return;
}

if (password.length < 8) {
errorMessageElement.textContent = 'Password must be at least 8 characters long.';
return;
}

if (!email) {
errorMessageElement.textContent = 'Email is required.';
return;
}



try {
const response = await fetch('/register', {
method: 'POST',
Expand Down
34 changes: 19 additions & 15 deletions src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link rel="stylesheet" href="/css/styles.css">
<link rel="stylesheet" href="/css/login.css">
</head>
<body>
<h2>Login</h2>
<form id="loginForm">
<div>
<label>Username:</label>
<input type="text" id="username" name="username">
<div class="container">
<h2>Login</h2>
<form id="loginForm">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" placeholder="Enter your username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Enter your password">
</div>
<div class="form-group">
<button type="submit" class="btn">Login</button>
</div>
</form>
<div class="link-container">
<a href="/register">Register</a>
</div>
<div>
<label>Password:</label>
<input type="password" id="password" name="password">
</div>
<div>
<button type="submit">Login</button>
</div>
</form>
<a href="/register">Register</a>
</div>

<div id="popup" class="popup">
<div class="popup-inner">
Expand Down

0 comments on commit 1623827

Please sign in to comment.