This is a microservice responsible for user authentication, built using Node.js, MySQL, Docker, and Minikube. The service provides APIs for user registration, login, JWT token management (verification, refresh), and scaling via Docker or Kubernetes.
- Prerequisites
- Installation
- Endpoints
- Authorization
- Scaling with Docker and Minikube
- Run with Docker
- Run with Minikube
- Node.js (v14 or later)
- MySQL (v8.0)
- Docker (v20.x or later)
- Minikube (for Kubernetes scaling)
Ensure that you have the following installed:
- Docker Desktop or Docker Engine
- Minikube (if you plan to use Kubernetes for scalability)
docker pull mitesh2110/authimage:1.0.1Fireup the database either in docker container or in local mysql server
0.1 IN LOCAL SQL SERVER
create database auth;
use auth;
create table users(
uid int auto_increment primary key,
uname varchar(10) not null,
email varchar (30) not null unique,
password varchar (255) not null,
created_at timestamp default current_timestamp);0.2 IN DOCKER CONTAINER In docker container you just need to run the docker-compose file and it create and fire up the container.
docker-compose up- Clone the repository:
git clone https://github.com/miteshp2110/AuthService.git- Change Directory
cd .\AuthService\- Install Dependencies
npm install- Setup .env files with following data
MYSQL_HOST=localhost
MYSQL_DIALECT=mysql
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_DB=auth
JWT_SECERET=SampleSeceretKey- Run the Node Server
npm start- Build Docker Image (if not pull from DockerHub)
docker build -t authimage:latest .cd manifestkubectl apply -f ./minikube service auth-service- Method:
POST - Description: Registers a new user with email, username, and password.
- Request Body:
{
"email": "user@example.com",
"username": "user123",
"password": "password123"
}- Response:
- 201 Created : User successfully registerd.
- 409 Conflict : Email Already exists.
-
Method:
POST -
Description: Logs in a user and returns a JWT.
-
Request Body:
{
"email": "user@example.com",
"password": "password123"
}-
Response:
- 201 Created : JWT token and refresh token.
- 401 Unauthorized : Incorrect email or password.
-
Response Body
{
"message": "Correct Credentials",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RlbWFpbEBnbWFpbC5jb20iLCJpYXQiOjE3MjYwNDY2ODEsImV4cCI6MTcyNjEzMzA4MX0.z9PhJrJrm8ASikKANRQbLtaBLIl4a6d_EgaAnohtC_Q",
"username": "Mitesh21"
}-
Method:
GET -
Description: Verifies the validity of a JWT token.
-
Request Body:
{
"Authorization": "Bearer <JWT_TOKEN>"
}- Response:
- 200 Created : JWT token is valid.
- 401 Unauthorized : Token is invalid or Expired.
-
Method:
POST -
Description: Give a new Token in exchange of expired token.
-
Request Body:
{
"email":"testemail@gmail.com",
"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RlbWFpbEBnbWFpbC5jb20iLCJpYXQiOjE3MjU5OTQxNjksImV4cCI6MTcyNTk5NDIyOX0.Bhv_6re7pRnEKrqbsa4EaCe5dunFvo3ps5mgkNqu5VE"
}- Response:
- 200 Ok : New Token.
- 403 Unauthorized : Invalid Token.