<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sukanta Das</title>
    <description>The latest articles on Forem by Sukanta Das (@cristain).</description>
    <link>https://forem.com/cristain</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F972954%2F355897ef-ec59-4d14-ad97-4ef0667090dd.png</url>
      <title>Forem: Sukanta Das</title>
      <link>https://forem.com/cristain</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cristain"/>
    <language>en</language>
    <item>
      <title>Why TypeScript is Transforming Modern Web Development</title>
      <dc:creator>Sukanta Das</dc:creator>
      <pubDate>Mon, 01 Jul 2024 05:10:48 +0000</pubDate>
      <link>https://forem.com/cristain/why-typescript-is-transforming-modern-web-development-372</link>
      <guid>https://forem.com/cristain/why-typescript-is-transforming-modern-web-development-372</guid>
      <description>&lt;p&gt;🚀 TypeScript: A Game-Changer for Modern Web Development 🚀&lt;/p&gt;

&lt;p&gt;As web development evolves, so do the tools and languages we use. One of the most impactful advancements in recent years has been TypeScript. Whether you’re a seasoned developer or just starting out, TypeScript is revolutionizing how we write, maintain, and scale code. Here’s why you should consider embracing TypeScript:&lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Type Safety:&lt;/strong&gt;&lt;br&gt;
TypeScript introduces static typing to JavaScript, helping you catch errors at compile time rather than at runtime. This means fewer bugs and a more robust codebase.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Error Detection:&lt;/strong&gt; Identify potential issues early in the development process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Quality:&lt;/strong&gt; Maintain a cleaner and more reliable codebase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔹 &lt;strong&gt;Enhanced Developer Experience:&lt;/strong&gt;&lt;br&gt;
TypeScript’s powerful type system improves the development workflow by providing better tools and features.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;IntelliSense:&lt;/strong&gt; Enjoy smarter code completions, navigation, and refactoring.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documentation:&lt;/strong&gt; Benefit from automatic documentation generation and better IDE support.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔹 &lt;strong&gt;Scalable Codebase:&lt;/strong&gt;&lt;br&gt;
As your project grows, TypeScript helps manage complexity with its advanced type system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Modular Code:&lt;/strong&gt; Write modular, reusable, and maintainable code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refactoring:&lt;/strong&gt; Safely refactor your codebase with confidence.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔹 &lt;strong&gt;Seamless Integration:&lt;/strong&gt;&lt;br&gt;
TypeScript is designed to integrate smoothly with existing JavaScript projects and libraries.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Gradual Adoption:&lt;/strong&gt; Migrate from JavaScript to TypeScript incrementally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compatibility:&lt;/strong&gt; Works seamlessly with popular frameworks and libraries like React, Angular, and Node.js.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔹 &lt;strong&gt;Community and Ecosystem:&lt;/strong&gt;&lt;br&gt;
TypeScript has a vibrant and growing community, which means continuous improvements and a rich ecosystem of tools and libraries.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Support:&lt;/strong&gt; Access extensive resources, tutorials, and community support.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Innovation:&lt;/strong&gt; Stay ahead with the latest features and updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔹 &lt;strong&gt;Real-World Impact:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Improved Code Quality:&lt;/strong&gt; Teams experience fewer bugs and easier debugging.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enhanced Collaboration:&lt;/strong&gt; Clearer code structure improves team collaboration and onboarding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TypeScript isn’t just a trend; it’s a powerful tool that enhances productivity, code quality, and maintainability. By leveraging TypeScript, we can build more reliable, scalable, and efficient applications.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>coding</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to implement JWT Authentication Using Node, Express, TypeScript ? 2023</title>
      <dc:creator>Sukanta Das</dc:creator>
      <pubDate>Tue, 25 Jul 2023 10:39:08 +0000</pubDate>
      <link>https://forem.com/cristain/how-to-implement-jwt-authentication-using-node-express-typescript-app-2023-2c5o</link>
      <guid>https://forem.com/cristain/how-to-implement-jwt-authentication-using-node-express-typescript-app-2023-2c5o</guid>
      <description>&lt;p&gt;Today We Will Learn How to Build NodeJs Authentication API using JWT, Express, Typescript .&lt;/p&gt;

&lt;p&gt;Table of contents&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What Is JSON Web Token (JWT)&lt;/li&gt;
&lt;li&gt;Initialize Project &lt;/li&gt;
&lt;li&gt;Install dependencies and devDependencies &lt;/li&gt;
&lt;li&gt;Setup express server with typescript &lt;/li&gt;
&lt;li&gt;Create User Model &lt;/li&gt;
&lt;li&gt;Create User Api To Register &lt;/li&gt;
&lt;li&gt;Crate Login Api and Implement JWT Authentication&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. What Is JSON Web Token (JWT)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;JSON web token(JWT)&lt;/strong&gt; is JSON Object which is used to securely transfer information over the web(between two parties). It is generally used for authentication systems and can also be used for information exchange.&lt;/p&gt;

&lt;p&gt;This is used to transfer data with encryption over the internet also these tokens can be more secured by using an additional signature.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Initialize Project
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir jwt-authentication
cd jwt-authentication
npm init --yes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Install dependencies and devDependencies
&lt;/h2&gt;

&lt;p&gt;3.1 Install dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express mongoose cors jsonwebtoken dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.2 Install devDependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D typescript nodemon @types/express @types/cors @types/jsonwebtoken
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.3 Add a tsconfig.json for typescript configuration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;add this cofig in tsconfig.json file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "compilerOptions": {
        /* Language and Environment */
        "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
        /* Modules */
        "module": "commonjs" /* Specify what module code is generated. */,
        "rootDir": "./",
        "outDir": "./dist",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

        /* Type Checking */
        "strict": true,
        "skipLibCheck": true /* Skip type checking all .d.ts files. */
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Setup express server with typescript
&lt;/h2&gt;

&lt;p&gt;In the root create a file name app.ts&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmiyecut0ap8dsmpu7inl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmiyecut0ap8dsmpu7inl.png" alt="folder_structure" width="288" height="354"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import express from "express";
import { Application } from "express";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
// Create the express app and  import the type of app from express;
const app: Application = express();

// Cors
app.use(cors());
//configure env;
dotenv.config();
// Parser
app.use(express.json());
app.use(
  express.urlencoded({
    extended: true,
  })
);
// Declare The PORT Like This
const PORT: number = 8000;

app.get("/", (req, res) =&amp;gt; {
  res.send("&amp;lt;h1&amp;gt;Welcome To JWT Authentication &amp;lt;/h1&amp;gt;");
});

// Listen the server
app.listen(PORT, async () =&amp;gt; {
  console.log(`🗄️  Server Fire on http:localhost//${PORT}`);

  // Connect To The Database
  try {
    await mongoose.connect(
      process.env.DATABASE_URL as string
    );
    console.log("🛢️  Connected To Database");
  } catch (error) {
    console.log("⚠️ Error to connect Database");
  }
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the Package.json add this srcipt&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "dev": "nodemon app.ts",
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After Update the package.json file open terminal and run the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the Terminal You will see &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fat0yxvgw4guomp2grpm6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fat0yxvgw4guomp2grpm6.png" alt="Termail_image" width="653" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open Your Browser and type the url "&lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;http://localhost:8000&lt;/a&gt;"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6wrvr5m14gsd8v5qpyh3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6wrvr5m14gsd8v5qpyh3.png" alt="browser_image" width="696" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Express server is now up and running!&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Create User Model
&lt;/h2&gt;

&lt;p&gt;The User model is created using using the mongoose package. This model represent each user in the database.&lt;/p&gt;

&lt;p&gt;In the root create a models folder, create the file user.ts .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqn3u4fmux4kmqb9g6aiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqn3u4fmux4kmqb9g6aiw.png" alt="user_model" width="283" height="133"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;inside user.ts create the user schema;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import mongoose from "mongoose";

const UserSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
    },
    email: {
      type: String,
      unique: true,
      required: true,
    },
    password: {
      type: String,
      required: true,
    },
  },
  { timestamps: true }
);

export const User = mongoose.model("Users", UserSchema);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating the user model we are ready to implement the jwt Authentication.&lt;/p&gt;

&lt;h2&gt;
  
  
  6.Create User Api To Register
&lt;/h2&gt;

&lt;p&gt;in the "app.ts" create a register api to create account.&lt;br&gt;
api = '/auth/register'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post("/auth/register", async (req, res) =&amp;gt; {
  try {
    // ** Get The User Data From Body ;
    const user = req.body;

    // ** destructure the information from user;
    const { name, email, password } = user;

    // ** Check the email all ready exist  in database or not ;
    // ** Import the user model from "./models/user";

    const isEmailAllReadyExist = await User.findOne({
      email: email,
    });

    // ** Add a condition if the user exist we will send the response as email all ready exist
    if (isEmailAllReadyExist) {
      res.status(400).json({
        status: 400,
        message: "Email all ready in use",
      });
       return;
    }

    // ** if not create a new user ;
    // !! Don't save the password as plain text in db . I am saving just for demonstration.
    // ** You can use bcrypt to hash the plain password.

    // now create the user;
    const newUser = await User.create({
      name,
      email,
      password,
    });

    // Send the newUser as  response;
    res.status(200).json({
      status: 201,
      success: true,
      message: " User created Successfully",
      user: newUser,
    });
  } catch (error: any) {
    // console the error to debug
    console.log(error);

    // Send the error message to the client
    res.status(400).json({
      status: 400,
      message: error.message.toString(),
    });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now test the api. I am using ThunderClient Extension available in vs code Market Place.&lt;/p&gt;

&lt;p&gt;if everything works well you will get a success message like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbqvely5yp3lr77dbpl32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbqvely5yp3lr77dbpl32.png" alt="register_api_test" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now if i  try to create the same user with the same email it will send me a error message.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwxjnclfx7ig7gqqao40g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwxjnclfx7ig7gqqao40g.png" alt="register_aapi_eror_message" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;after creating the user we can start jwt implement .&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Crate Login Api and Implement JWT Authentication
&lt;/h2&gt;

&lt;p&gt;api = '/auth/login'&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get The User Data From Body .&lt;/li&gt;
&lt;li&gt;destructure the information from user.&lt;/li&gt;
&lt;li&gt;Check the (email/user) exist  in database or not .&lt;/li&gt;
&lt;li&gt;if there is not any user with the email we send user not found.&lt;/li&gt;
&lt;li&gt;if the (user) exist  in database we will check the password  is valid or not .&lt;/li&gt;
&lt;li&gt;compare the password in database and the password in the request body.&lt;/li&gt;
&lt;li&gt;if not matched send response that wrong password.&lt;/li&gt;
&lt;li&gt;if the email and password is valid create a token .&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To create a token JsonWebToken (JWT) receive's 3 parameter&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Payload -  This contains the claims or data you want to include in the token.&lt;/li&gt;
&lt;li&gt;Secret Key - A secure key known only to the server used for signing the token.&lt;/li&gt;
&lt;li&gt;expiration -  Additional settings like token expiration or algorithm selection.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don't Provide the secret openly, This secret is very sensitive for the server . keep it in the .env file. I am Keeping it  Open just for demonstration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After creating the token send the response.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post("/auth/login", async (req, res) =&amp;gt; {
  try {
    // ** Get The User Data From Body ;
    const user = req.body;

    // ** destructure the information from user;
    const { email, password } = user;

    // ** Check the (email/user) exist  in database or not ;
    const isUserExist = await User.findOne({
      email: email,
    });

    // ** if there is not any user we will send user not found;
    if (!isUserExist) {
      res.status(404).json({
        status: 404,
        success: false,
        message: "User not found",
      });
return;
    }

    // ** if the (user) exist  in database we will check the password is valid or not ;
    // **  compare the password in db and the password sended in the request body

    const isPasswordMatched =
      isUserExist?.password === password;

    // ** if not matched send response that wrong password;

    if (!isPasswordMatched) {
      res.status(400).json({
        status: 400,
        success: false,
        message: "wrong password",
      });
        return;
    }

    // ** if the email and password is valid create a token

    /*
    To create a token JsonWebToken (JWT) receive's 3 parameter
    1. Payload -  This contains the claims or data you want to include in the token.
    2. Secret Key - A secure key known only to the server used for signing the token.
    3. expiration -  Additional settings like token expiration or algorithm selection.
    */

    // !! Don't Provide the secret openly, keep it in the .env file. I am Keeping Open just for demonstration

    // ** This is our JWT Token
    const token = jwt.sign(
      { _id: isUserExist?._id, email: isUserExist?.email },
      "YOUR_SECRET",
      {
        expiresIn: "1d",
      }
    );

    // send the response
    res.status(200).json({
      status: 200,
      success: true,
      message: "login success",
      token: token,
    });
  } catch (error: any) {
    // Send the error message to the client
    res.status(400).json({
      status: 400,
      message: error.message.toString(),
    });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now test the api. I am using ThunderClient Extension available in vs code Market Place.&lt;/p&gt;

&lt;p&gt;Now If Try To Login With Wrong Email that it will send the error of user not found &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiv8y0xoo2pvaqq4hrg3u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiv8y0xoo2pvaqq4hrg3u.png" alt="login_wrong_email_response" width="800" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now If Try To Login With right  Email but wrong password it will send error of wrong password.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn90zy6odt49h1s216gab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn90zy6odt49h1s216gab.png" alt="login_wrong_password_response" width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If I Provide The Correct Email and Password it will send me the token that we created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvzherpnl57ixrga3h8d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvzherpnl57ixrga3h8d.png" alt="login_seccess_response" width="800" height="419"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now if we copy the token that we got after the login and go to &lt;a href="https://jwt.io/" rel="noopener noreferrer"&gt;https://jwt.io/&lt;/a&gt; and past the token and press decode &lt;/p&gt;

&lt;p&gt;we can see the information that we provided on creation time .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbgn5eydygqqsydp3t6bd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbgn5eydygqqsydp3t6bd.png" alt="json_decode" width="800" height="350"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;If You Found This Help full ,Give a star ⭐&lt;br&gt;
Share With Other's as well.&lt;/p&gt;

&lt;p&gt;Github Repo  :  &lt;a href="https://github.com/CrisTain333/jwt-authentication-node-express-typescript" rel="noopener noreferrer"&gt;https://github.com/CrisTain333/jwt-authentication-node-express-typescript&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  That’s all.
&lt;/h2&gt;

</description>
      <category>express</category>
      <category>node</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to set up TypeScript with Node.js and Express (2023)</title>
      <dc:creator>Sukanta Das</dc:creator>
      <pubDate>Tue, 04 Jul 2023 04:19:15 +0000</pubDate>
      <link>https://forem.com/cristain/how-to-set-up-typescript-with-nodejs-and-express-2023-gf</link>
      <guid>https://forem.com/cristain/how-to-set-up-typescript-with-nodejs-and-express-2023-gf</guid>
      <description>&lt;p&gt;In this article, we’ll cover a Best way to set up TypeScript in an Express app, understanding the basic constraints that come with it.&lt;/p&gt;

&lt;p&gt;Table of contents&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a package.json file&lt;/li&gt;
&lt;li&gt;Installing TypeScript &amp;amp; other dependencies&lt;/li&gt;
&lt;li&gt;Generating tsconfig.json&lt;/li&gt;
&lt;li&gt;Create an Express server with a .ts extension&lt;/li&gt;
&lt;li&gt;Watching file changes and build directory&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Create initial folder and package.json
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir node-express-typescript
cd node-express-typescript
npm init --yes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After initialize a package.json file ,  The newly created file might look something like the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "Your File Name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js", // Entry Point change it from  js to .ts 
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
  "type": "module",
  "keywords": [],
  "author": "",
  "license": "ISC"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Installing TypeScript &amp;amp; other dependencies
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express mongoose cors mongodb dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install  -D typescript ts-node-dev @types/express @types/cors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Generating tsconfig.json
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command above will generate a new file called tsconfig.json with the following default compiler options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;target: es2016
module: commonjs
strict: true
esModuleInterop: true
skipLibCheck: true
forceConsistentCasingInFileNames: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After opening  the tsconfig.json file, you’ll see a lot of other compiler options that are commented out. In tsconfig.json, compilerOptions is a mandatory field that needs to be specified&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set the rootDir and outDir as src and dist folder
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "outDir": "./dist"

    // other options remain same
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Create an Express server with a .ts extension
&lt;/h2&gt;

&lt;p&gt;Create a file name index.ts  open it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import express, { Express, Request, Response , Application } from 'express';
import dotenv from 'dotenv';

//For env File 
dotenv.config();

const app: Application = express();
const port = process.env.PORT || 8000;

app.get('/', (req: Request, res: Response) =&amp;gt; {
  res.send('Welcome to Express &amp;amp; TypeScript Server');
});

app.listen(port, () =&amp;gt; {
  console.log(`Server is Fire at http://localhost:${port}`);
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5.  Watching file changes and build directory
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install  nodemon

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installing these dev dependencies, update the scripts in the package.json file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{

  "scripts": {
    "build": "npx tsc",
    "start": "node dist/index.js",
    "dev": "nodemon index.ts"
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Run The Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if everything if perfect you will see the message in console of &lt;br&gt;
Server is Fire at &lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;http://localhost:8000&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>node</category>
      <category>express</category>
    </item>
  </channel>
</rss>
