Skip to content

Front-end working project implementing PassportJS with ExpressJS

Notifications You must be signed in to change notification settings

hurricanemark/NodeJS-Passport-Login

Repository files navigation

Project: Node.js Passport Login

Title: PassportLogin

Description

This code is a bare minimum structure of Login using PassportJS. It demonstrates the implementation of passport-local, express-session with expressJS, NodeJS, EJS. It is without CSS. It is without the backend database, content management.

Where to go from here? Obviously, we could employ styling and data modelign, then on to JWT.

[x] PassportJS
[ ] JWT
[ ] Style CSS
[ ] Modeling using MongoDB Atlas

Screenshots

Register
register


Login
Login


Index
index page

References

EJS Embedded JavaScript templating

ExpressJS Fast, unopinionated, minimalist web framework for Node.js

nodejs JavaScript runtime built on Chrome's V8 JavaScript engine

bscript or bscryptjs bcrypt is a password hashing function designed by Niels Provos and David Mazières, based on the Blowfish cipher, and presented at USENIX in 1999.

passportJS Authentication middleware for Node.js

Getting Started

Environment vscode You need to install extension for EJS Language Support! EJS is the Embedded JavaScript templating language that lets you generate HTML markup with plain JavaScript.

npm init

npm i express ejs

npm i --save-dev nodemon dotenv

bcrypt is used for hashing passwords, ...

npm i bcrypt

passport passport-local are used for password authentication. express-session is for local session, express-flash displays invalid authentication messages

npm i passport passport-local express-session express-flash

Logic for passport,...,express-flash will be written in passport-config.js

npm i method-override

method-override alternates POST so we can call app.delete to implement logout

Then,

  • Create the following files: .env .gitignore

  • Insert into .gitignore

.env

node_modules
  • Insert into .env
SESSION_SECRET=your_secret_could_be_anything_here
  • Edit package.json

"scripts": { "devStart": "nodemon server.js" }

  • To start the express server on the terminal:

npm run devStart

Docker Build, Deploy, and Run

Verify that the application is running after you cloned it. Hint, create .env file and enter the followings:

NODE_ENV=development
SESSION_SECRET=Anything you desirer here
PORT=3322

Build Docker

Replace 'hurricanemark' with your dockerhub's username

docker build -t hurricanemark/nodejs-passport-login:1.0 .

Then, run the application:

npm start

List docker image

Find the IMAGE ID of the newly build docker image

docker image ls

Run Docker

docker run -p 9999:8080 <IMAGE_ID>

Expected console output:

> node server.js

Listening on port 3322