Skip to content
This repository has been archived by the owner on Mar 12, 2023. It is now read-only.

ExpressJS Back-end which was originally created for a chat-app. For now, it only has authentication functionality.

License

Notifications You must be signed in to change notification settings

JefvdA/expressjs-authentication

Repository files navigation

ExpressJS-authentication

This project was originally a backend for a chat application, but it was never finished. The only functionality that is finished is authentication. Thus the name change to expressjs-authentication

Endpoints

The API endpoints are documented with swaggerUI.

The endpoint for the swagger docs is: /api-docs


As databse, this project uses mongodb, more on how to set that up later in this guide.

If you want to know more about the project structure, naming conventions... look at our wiki.

In this guide, you will be explained how to run this project on your own device.


Dev environment

It is important that everyone uses the same environment for developing the project. It is also much easiers, and saves a lot of time.

This is easily achievable with the vscode remote-containers, dev-containers that can be opened in vscode. Take a look at this extension. When the extension is installed, you should have an option to open the project in a new remote-container.

If you want to develop using our development environment, make sure to have docker installed, as this is needed.


Setup scripts

In the /bin directory, you can find some usefull scripts that make it even easier to work with docker.

MongoDb

  • startMongo.sh : start a new mongoDB container (with authentication USERNAME='admin', PASSWORD='admin')
  • stopMongo.sh : stop running mongoDB container + delete the container

Whole project

  • startAll.sh : run docker-compose, and rebuild all the images for when changes are made
  • stopAll.sh : stop docker-compose, and remove all the containers and docker network

Mongodb Setup

In this part of the guide, the setup for mongodb is explained.

Ofcourse, you can use any tutorial you want to setup a mongodb installation.

It is recommended that you install mongodb in a docker container, as it's much easier, and makes the install non-permanent. It's very easy to delete the docker container when you don't want to use mongodb anymore.

Setup mongodb without authentication (a root user account):

sudo docker run --name mongodb -d -p 27017:27017 mongo

Setup mongodb with a root user account (of course you can change the uesrname and password to your liking):

sudo docker run \
--name mongodb \
-d -p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=admin \
mongo

Mongosh is a new addition for mongodb, it's an interactive shell to interact with your database.

This can also be used to test your database connection, and to test if the authentication was succesfull.

To test the connection, run following command:

mongosh

To test a connection, and also login as the created root user:

mongosh -u [username] -p [password]

To start, stop or remove the docker container, use following commands:

sudo docker start [containerName]
sudo docker stop [containerName]
sudo docker rm [containerName]

.env file

Thourghout the application, environment variables are often used. Below, and expample of how a .env file looks, this file should be placed in the root of the project:

PORT=3000
MONGODB_URI="mongodb://localhost:27017"
MONGODB_DATABASE="chat-app"
MONGODB_USERNAME="admin"
MONGODB_PASSWORD="admin"
COOKIE_SECRET="SECRET"
JWT_SECRET="SECRET"

Of course, change the MONGODB variables for your own environment.

Secure secret strings for the cookie and the jwt can be generated by running following command in the terminal:

openssl rand -base64 64

This will generate a random string of 64 base64 characters


Setup

After cloning the repo, and setting up the database, it's very easy to run the application.

install the node modules: npm install
run the application: npm start or node index.js

Make use of hot-reloading: npm run dev or nodemon (make sure you have nodemon installed)


Docker setup

If you prefer, the project can also be ran by using docker.

The Dockerfile builds the image for this project.
This image, and the database (monogodb) are ran in seperate containers which can be ran with docker-compose.

Start the container-cluster with:

docker-compose up -d

Use the -d flag to start the cluster up in the background

Docker-compose can be stopped with:

docker-compose stop

Testing

If you are contributing to this project, you don't want to manualy test to see if everything still works, instead just run the tests this project has.

To run the tests, use following command:

npm test

For testing, we use the framework mocha, nyc is used to give test coverage when you end the tests.

About

ExpressJS Back-end which was originally created for a chat-app. For now, it only has authentication functionality.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published