This project is a basic user registration and login system built with Java. It uses a simple Java HTTP server for the backend, MySQL for storing user data, and plain HTML, and JavaScript for the frontend.
- Backend: Java 25 with the built-in HTTP server
- Database: MySQL
- JSON Handling: Google Gson
- Testing: JUnit 5
- Frontend: HTML5 and Vanilla JavaScript
The server creates a simple random math question and gives it a unique token. The correct answer is stored temporarily in memory using a ConcurrentHashMap. After the user tries to answer the CAPTCHA, it is removed so the same token cannot be reused.
Users can register by sending their details as JSON. The backend checks the input fields, validates the CAPTCHA, and checks the database to make sure the email is not already registered.
Users can log in with their email and password. If the login is successful, the server creates a random session ID using UUID.randomUUID() and sends it back to the browser as a cookie.
Users can log out, which removes the session from the server and clears the cookie in the browser.
Logged-in users can view their profile using /me and update their details using /update. These routes are protected by checking the session cookie.
HttpServer.create()for starting the backend servergson.fromJson()andgson.toJson()for converting between JSON and Java objectsString.matches()for checking fields like email and password formatUUID.randomUUID()for creating session and CAPTCHA tokensConcurrentHashMapfor storing temporary CAPTCHA answers and user sessions safely
-
StaticFileHandler.java
Handles requests for the frontend files, such as the main HTML page. -
CaptchaHandler.java
Handles GET/captcharequests and returns a math CAPTCHA question with a token. -
RegisterHandler.java
Handles POST/registerrequests. It validates the submitted data and creates a new user in the database. -
LoginHandler.java
Handles POST/loginrequests. It checks the user's email and password, then creates a session cookie if the login is successful. -
LogoutHandler.java
Handles POST/logoutrequests. It removes the user's session and clears the cookie. -
MeHandler.java
Handles GET/merequests. It checks the session cookie and returns the logged-in user's profile information. -
UpdateHandler.java
Handles PUT/updaterequests. It allows logged-in users to update their profile information.
-
ValidateService.java
Contains the main validation logic for registration, login, password rules, and CAPTCHA checking. -
SessionService.java
Manages user sessions using a thread-safeConcurrentHashMap. -
CaptchaService.java
Creates random math CAPTCHA questions and stores the correct answers temporarily.
-
DatabaseConfig.java
Sets up the MySQL database connection and creates the required table if it does not already exist. -
UserRepository.java
Contains the SQL queries for finding, creating, and updating users.
- Main.java
Starts the server, sets the port, and connects each endpoint to its handler.