Skip to content

Multi-level authentication in Node.js-Express application, using: PostgreSQL, Node.js, Express.js, JavaScript, EJS, and OAuth2.0 πŸ”

Notifications You must be signed in to change notification settings

shanibider/Multi-Level-Authentication

Repository files navigation

Node.js & Express - Multi-Level Authentication


This guide will walk you through implementing multi-level authentication in your Node.js and Express application, using:

JavaScript, Node.js, Express.js, EJS, and PostgreSQL. .

The authentication levels covered include:

πŸ”‘ 1. Basic Authentication

πŸ”‘ 2. Hashing Authentication

πŸ”‘ 3. Google OAuth 2.0 Authentication using Passport.js and PostgreSQL

πŸ”“ 1. Basic Authentication

Basic Authentication involves prompting users to enter their credentials (username and password) for authentication.

Steps to Implement Basic Authentication πŸ“„

  1. Define routes for login and registration.
  2. Implement route handlers to handle login and registration requests.
  3. Verify user credentials against a database.
  4. Upon successful authentication, issue a token or set a session to maintain user authentication.

πŸ”“ 2. Hashing and Session Authentication

Hashing and Session Authentication involves securely storing user passwords using bcrypt.js and maintaining user sessions using Express session middleware.

Steps to Implement Hashing and Session Authentication πŸ“„

  1. Install bcryptjs.js and Express session middleware.
  2. Configure session middleware and secret.
  3. Hash user passwords before storing them in the database.
  4. Use session middleware to maintain user sessions across requests.
  5. Protect routes by verifying session authentication.

πŸ”“ 3. OAuth2 Authentication using Google

Google OAuth2 Authentication allows users to authenticate using their Google account, providing a seamless and secure login experience.

Steps to Implement Google OAuth2 Authentication πŸ“„

  1. Set up Google Developer Console and obtain OAuth client credentials.
  2. Install required dependencies: passport, passport-google-oauth20, dotenv.
  3. Configure environment variables for Google OAuth credentials.
  4. Implement Passport.js Google OAuth2 strategy.
  5. Define routes for Google authentication.
  6. Protect routes using Passport's authentication middleware.


About OAuth 2.0 πŸ›‘οΈ

OAuth 2.0 is an authorization framework that standardizes third-party access to HTTP services securely, allowing applications to act on behalf of users.

Key Roles

  • Client: Requests access to resources.
  • Resource Owner: User granting or denying access to their data.
  • Resource Server: Stores protected user data.
  • Authorization Server: Authenticates the resource owner and issues access tokens.

Process Overview πŸ”„

  • 1. User wants to access resources.
  • 2. App requests authorization.
  • 3. User inputs credentials.
  • 4. App sends credentials with OAuth keys to Authorization Server.
  • 5. Authorization Server returns Access token.
  • 6. App sends token to Resource Server.
  • 7. Resource Server serves resource to the app.
  • 8. App confirms accessibility to the user.

OAuth 2.0 Flow 🌊

OAuth offers various flows:

  • Authorization Code Flow: For server-side apps, exchanges code for token.
  • Implicit Flow: Deprecated, returns token directly but lacks security.
  • Resource Owner Password Credentials Flow: Uses user's credentials directly.
  • Client Credentials Flow: For server-to-server authentication.
  • Refresh Token Flow: Obtains new token when the old one expires.
  • Device Code Flow: Facilitates authorization on devices with limited input.
  • Extension Flow: Customizable for specific needs.

Use Cases πŸ“±

  • Third-Party Access: Users grant apps limited access without credentials.
  • Single Sign-On (SSO): Enables users to log in once for multiple services.
  • Mobile Applications: Allows secure access to services on mobile devices.

Related Links πŸ”—

Resources πŸ”—


More Info About Authentication and Security 🧠

Encryption for Data Security 🧰

Encryption involves encoding data to make it unintelligible to unauthorized users. In the context of web development, encrypting sensitive information before storing it in the database adds an extra layer of security. Tools like OpenSSL or libraries such as bcrypt.js can be used for encryption in JavaScript applications.

Hashing and Salting with bcrypt 🧰

Hashing transforms sensitive data into a fixed-size string of characters, making it challenging for attackers to reverse-engineer the original data. Salting involves adding random data (salt)πŸ§‚ to the input before hashing, further enhancing security. The bcrypt library in Node.js simplifies the process of hashing and salting passwords securely.

Session Management with Sessions and Cookies 🧰

Sessions and cookies are commonly used to persist user login sessions across multiple requests. Sessions store user-specific data on the server, while cookies store data on the client-side. Implementing session management ensures that users remain authenticated throughout their browsing session, enhancing user experience and security.

Using Passport.js for Authentication 🧰

Passport.js is a popular authentication middleware for Node.js applications, offering a wide range of authentication strategies, including local authentication, OAuth, and more. Integrating Passport.js simplifies the authentication process and provides robust security features out of the box.

Securing Secret Keys with Environment Variables 🧰

Sensitive information such as API keys, database credentials, and cryptographic keys should never be hard-coded in the application code. Instead, they should be stored as environment variables and accessed programmatically. This practice minimizes the risk of exposing sensitive information and enhances application security.

OAuth 2.0 Integration 🧰

OAuth 2.0 is an industry-standard protocol for authorization, allowing users to grant third-party applications limited access to their resources without sharing their credentials. Integrating OAuth 2.0 with popular identity providers such as Google and Facebook enables seamless and secure user authentication in JavaScript applications.

For detailed implementation instructions and code examples, please refer to the sample code provided in this repository.


Installation πŸ› οΈ

npm install express bcryptjs passport passport-local passport-google-oauth20 express-session dotenv

Usage

  1. Clone this repository.
  2. Configure your environment variables in a .env file.
  3. Run the application using npm start.
  4. Access the application in your browser.

Screenshots πŸ–ΌοΈ

secrets_home

Level 1 - Basic authentication - Registration users with email and password (password stored unencrypted in db) -

2 3

Level 2 of authentication - Encrypting and Hashing passwords -

4-hash

Level 3 Authentication - using OAuth (Open Authorisation, third party) to authenticate users, without storing their credentials -

5-outh 6


Feel free to explore my repositories and see my projects. I'm always open to collaboration and welcome feedback to improve and grow as a developer. Let's code something amazing together! πŸš€πŸ˜ŠπŸ‘©β€πŸ’»πŸ’»

πŸ“« Connect with me 😊

linkedin portfolio gmail

Copyright Β© Shani Bider, 2024

LicenseπŸ“„

This project is licensed under the MIT License.

About

Multi-level authentication in Node.js-Express application, using: PostgreSQL, Node.js, Express.js, JavaScript, EJS, and OAuth2.0 πŸ”

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published