Implementing Basic Authentication using Express and Typescript

  1. Project Structure, Database Connection, and Error Handling — Part 1
  2. Authentication System using Express and Typescript — Part 2

Project Structure

Once you set up the basic skeleton for the application. It’s time to understand the project structure. Here, we are going to follow a modular system.

Route Configuration

We can configure routes in many different ways. However, since we use Express and Typescript, we will go with Typescript concepts for a more structural codebase.

Database Connection

Before implementing the connection to the database, we need to install the required dependencies for it.

  • Login.
  • Signup.
  • User route to get the logged-in user.
  • Create User Model — It creates a model with which we can connect with MongoDB users collection.
  • Implement Authentication Route Configuration — route configuration for Auth. i.e., login and signup
  • Create Authentication Controller — Controller interacts with services for data fetch and update.
  • Build Authentication Service — Service interacts with DB for database operations.
  1. Using .create method
  2. Using new keyword to create an object. For example, new User().

Middleware

To validate the user based on the jwt token, create a middleware JWT.ts and add the following code,

Want to stand out from the Crowd?

Don’t get stuck in the tutorial loop. Learn a technology by practicing real world scenarios and get a job like a boss. Subscribe and get the real world problem scenarios in your inbox for free

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store