An application for authentication using NestJS, Docker, Kubernetes, MongoDB, JWT, and Swagger.
- Node.js Node.js version
^10.23
. - Yarn for Package Manager.
- Nest.js as the framework.
- MongoDB for Database Storage.
- Mongoose for MongoDB object modeling.
- Swagger for API Docs.
- Docker for deliver in packages called containers.
- GKE for container-orchestration system.
This app contains 6 endpoints:
-
Create User
description: for create an user. User will be checked is their email already used before. then, they will get an email to verify token later, to be noted, user will have emailVerified flag to be false before user verify their account. And also don't worry, password will be hashed before insert to DB.
additional description: roles has 2 option they are admin (can do everything) and user (only can read their user info).
endpoint: /auth/create
role: admin
method: POST
body request:
{ "email": "user@gmail.com", "roles": [ "user" ], "name": "User", "password": "User@123" }
-
Verify Token by Email
description: to verify user with token has been sent to their email. Then emailVerified flag will be true, and user can login to this app.
endpoint: /auth/verify/{token}
method: GET
query params:
token = 9dd930f0-4837-11eb-a06d-6f4e05ebdd0b
-
Login User
description: for logging in an user.
endpoint: /auth/login
method: POST
body request:
{ "email": "user@gmail.com", "password": "User@123" }
-
Update User
description: for update an email and name of the user.
endpoint: /auth/update
role: admin
method: PUT
body request:
{ "email": "newuseremail@gmail.com", "name": "New User Name", }
-
Get User Login
description: to get current user login data.
endpoint: /auth/user
method: GET
-
Get All User
description: to get all user data.
endpoint: /auth/users
role: admin
method: GET
-
Delete User
description: to delete an user by their email.
endpoint: /auth/delete
role: admin
method: DELETE
body request:
{ "email": "user@gmail.com", }
First of all, please setup environtment variable file, called .env and production.env, which contain
.env file:
NODE_ENV=development
PORT=3000
JWT_SECRET=user2020
EXPIRES_IN=3600
DB_URL=YOUR_MONGODB_DEV_SRV_STRING_URL
production.env file:
NODE_ENV=production
PORT=3000
JWT_SECRET=user2020
EXPIRES_IN=3600
DB_URL=YOUR_MONGODB_PROD_SRV_STRING_URL
Please change DB_URL to yours.
Then, you have to install docker, gcloud, and kubectl.
$ yarn install
To seed your DB data for user admin, just run:
$ yarn db:seed
Then you will have one user which have:
{
"email": "admin@gmail.com",
"name": "Administrator",
"password": "qwerty",
"roles": ["admin"]
}
To run app on your local, you can run it via docker-compose:
$ docker-compose up
First of all, you have to init gcloud on your local:
gcloud init
Then, build image and deploy it to the Google Container Registry
docker build -t gcr.io/{{YOUR PROJECT ID}}/nest-mongo-auth:latest .
docker push gcr.io/{{YOUR PROJECT ID}}/nest-mongo-auth:latest
change YOUR PROJECT ID to yours.
Now our app is dockerized, then we have to setup our deployment. First create cluster on GKE by name nest-mongo-auth:
gcloud container clusters create nest-mongo-auth
after that, we have to authenticate our shell to use clusters
gcloud container clusters get-credentials nest-mongo-auth --zone {{YOUR REGION CODE}} --project {{YOUR PROJECT ID}}
in deployment.yaml file, don't forget to change YOUR GKE PROJECT ID, YOUR TAG VERSION, and YOUR MONGODB SRV CONNECTION STRING to yours.
Then run deployment & service:
kubectl run nest-mongo-auth --image gcr.io/{{YOUR PROJECT ID}}/nest-mongo-auth:latest --port 3000
kubectl apply -f deployment.yaml --record
To check deployment process:
kubectl get deployments
To check pods (containers):
kubectl get pods
To service and copy EXTERNAL IP address (LoadBalancer):
kubectl get services
Now, you can open your browser this URL to see Swagger docs for this app:
http://<EXTERNAL-IP>/docs/#/