Skip to content

PEAN (PostgreSQL + Express.js + Angular 13 + Node.js) stack CRUD example with JWT Authentication & Authorization.

Notifications You must be signed in to change notification settings

MkDev11/angular-pean-crud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A To-Do Application: PEAN CRUD Example

PEAN (PostgreSQL + Express.js + Angular 13 + Node.js) stack CRUD example with JWT Authentication & Authorization.

to-do-app

Intro

This project is a full-stack (Angular 13 + Node.js + PostgreSQL) To-do Application.

The back-end server uses Node.js + Express framework for creating REST APIs and JWT for Authentication & Authorization.

The front-end side is an Angular App with HttpClient and Router.

This app doesn not use ejs templating engine to render the views. The views are rendered from the client-side as a Single Page Application using Angular 13 routing

Overview of PEAN stack To-Do applicationwith JWT Authentication & Authorization

With the To-Do application:

  • User can signup to create new account, or login with username & password.
  • User information is stored in PostgreSQL database
  • The User role (admin,, user), authorizes the User to access resources
  • Authorized User can Add/delete a todo item
  • The To-Do item is stored in PostgreSQL database

Architecture

to-do-app

• Node.js exports REST APIs & interacts with PostgreSQL Database using Sequelize ORM.

• Angular Client sends HTTP Requests and retrieves HTTP Responses using HTTPClient, consume data on the components. Angular Router is used for navigating to pages.

Backend (Node.Js)

to-do-app HTTP request that matches a Express routes, is checked by CORS Middleware before reaching the Security layer of the application

Security layer includes:

  • JWT Authentication Middleware: verify SignUp, verify token
  • Authorization Middleware: check User’s roles with record in database

If these middlewares throw any error, a message is be sent as HTTP response.

Controllers interact with PostgreSQL Database via Sequelize and send HTTP response (token, user information and to-do items based on roles) to the client.

Flow for Signup & Login with JWT Authentication

The diagram shows flow of User Registration, User Login and Authorization process.

to-do-app

Technology

  • Express Js for REST APIs
  • bcryptjs for password encrytion
  • jsonwebtoken to token based authentication & authorization
  • Sequelize for database CRUD operations
  • PostgreSQL for data storage

Project Structure

to-do-app

config

  • configure PostgreSQL database & Sequelize
  • configure Auth Key

routes

  • auth.routes.js: POST signup & signin
  • user.routes.js: GET public & protected resources

middlewares

  • verifySignUp.js: check duplicate Username or Email
  • authJwt.js: verify Token, check User roles in database

controllers

  • auth.controller.js: handle signup & signin actions
  • user.controller.js: return public & protected content

models for Sequelize Models

  • user.model.js
  • role.model.js

server.js: import and initialize neccesary modules and routes, listen for connections.

Backend APIs (NodeJS):

These are APIs that Node.js Express App will export:

Methods Urls Actions
POST /api/auth/signup signup new account
POST /api/auth/signup login an account
GET /api/user retrieve all users
GET /api/user/:id retrieve user by id
GET /api/items retrieve all to-do items
GET /api/items/:id retrieve to-do item by id
DELETE /api/items/:id remove a to-do item by id

Frontend SPA (Angular)

The frontened implementation includes HttpInterceptor, Router, HttpClient, Route Guard with implements CanActivate & Form Validation).

Screenshots

Signup Page: to-do-app

Login Page: to-do-app

Validation: to-do-app

Project Structure

to-do-app

Angular SPA with Router and HttpInterceptor

to-do-app

– The App component is a container using Router. It gets user token & user information from Browser Session Storage via token-storage.service. Then the navbar now can display based on the user login state & roles.

Login & Register components have form for submitting data (with support of ReactiveForm Validation). They use token-storage.service for checking state and auth.service for sending signin/signup requests.

auth.service uses Angular HttpClient ($http service) to make authentication requests.
– every HTTP request by $http service will be inspected and transformed before being sent by auth-interceptor.

Dashboard components will be displayed depending on JWT auth token from Session Storage. In this components, we use user.service to get protected resources from API.

Technology

  • Angular 13
  • RxJS 6
  • Bootstrap 4

About

PEAN (PostgreSQL + Express.js + Angular 13 + Node.js) stack CRUD example with JWT Authentication & Authorization.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published