This service provides REST API endpoints for user registration and authentication using JWT (JSON Web Tokens). It is built with Spring Boot and uses H2 as an in-memory database.
- Register new users with username, email, password, first name, and last name
- Authenticate users and issue JWT access tokens
- Passwords are securely hashed using BCrypt
- JWT-based stateless authentication
- Basic error handling for authentication and registration
- In-memory H2 database for development and testing
- Java 17+ (configured for Java 24)
- Spring Boot 3.x
- Spring Security
- Spring Data JPA
- H2 Database
- JWT (io.jsonwebtoken)
- Lombok
- Java Development Kit (JDK) 17 or higher
- Maven
-
Clone the repository:
git clone <repository_url> cd auth-service
-
Set environment variables for database and JWT secret key. You can use a
.envfile or set them in your environment:DATABASE_USERNAME=your_db_username DATABASE_PASSWORD=your_db_password JWT_SECRET_KEY=your_jwt_secret -
Build and run the application:
mvn spring-boot:run
- POST
/api/auth/register - Request Body:
{ "username": "johndoe", "email": "[email protected]", "password": "yourpassword", "firstname": "John", "lastname": "Doe" } - Response:
200 OKon success
- POST
/api/auth/authenticate - Request Body:
{ "username": "johndoe", "password": "yourpassword" } - Response:
{ "accessToken": "jwt_access_token_here", "expiresIn": 900 } - Notes:
- The
refreshTokenis not included in the JSON response body. - The
refreshTokenis sent as anHttpOnlycookie in theSet-Cookieresponse header. - The client should store the
accessTokenfrom the response body and rely on the browser to manage therefreshTokencookie.
- The
- GET
/api/auth/refresh - Request:
- The
refreshTokenmust be present as anHttpOnlycookie (automatically sent by the browser).
- The
- Response:
{ "accessToken": "new_jwt_access_token_here", "expiresIn": 900 } - Notes:
- The endpoint reads the
refreshTokenfrom the cookie, not from the request body. - A new access token is returned in the response body.
- The endpoint reads the
- POST
/api/auth/logout - Request:
- The
refreshTokencookie is used to identify and invalidate the session.
- The
- Response:
200 OKon success
- Accessible at:
http://localhost:8080/h2-console - JDBC URL:
jdbc:h2:mem:mydb - Username/Password: as set in your environment variables
src/main/java/com/growtivat/auth_service/- Main source codesrc/main/resources/application.properties- Application configurationsrc/test/java/com/growtivat/auth_service/- Test classes
This project is licensed under the MIT License.
Note: This service is intended for development and educational purposes. For production use, ensure to secure your secret keys and review security configurations.