<?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>DEV Community: Mukhtar</title>
    <description>The latest articles on DEV Community by Mukhtar (@devmukhtarr).</description>
    <link>https://dev.to/devmukhtarr</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%2F578587%2Ffd2f8c9e-9fcf-41e6-9705-d2deda876aeb.jpeg</url>
      <title>DEV Community: Mukhtar</title>
      <link>https://dev.to/devmukhtarr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devmukhtarr"/>
    <language>en</language>
    <item>
      <title>A Beginner's Guide to Building Powerful APIs with GraphQL</title>
      <dc:creator>Mukhtar</dc:creator>
      <pubDate>Thu, 02 Mar 2023 23:27:44 +0000</pubDate>
      <link>https://dev.to/devmukhtarr/a-beginners-guide-to-building-powerful-apis-with-graphql-4b4k</link>
      <guid>https://dev.to/devmukhtarr/a-beginners-guide-to-building-powerful-apis-with-graphql-4b4k</guid>
      <description>&lt;p&gt;An &lt;a href="https://en.wikipedia.org/wiki/API"&gt;API&lt;/a&gt;(Application Programmable Interfaces) is a way that enables computer programs to communicate easily with each other, APIs are used for performing a lot of functions which include Data Exchange, Automation and Integrating your application with other systems.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/GraphQL#:~:text=GraphQL%20is%20an%20open%2Dsource,being%20publicly%20released%20in%202015."&gt;GraphQL&lt;/a&gt; Is a language that can be used for Data Query and manipulation in APIs. GraphQL has made a lot of work easier for developers like improved API performance, and flexibility, in the sense that it allows the client to request only the data it needs and also it is strongly typed making it easier to catch errors.&lt;/p&gt;

&lt;p&gt;In this lesson, you are going to learn how to query and manipulate data using GraphQL, I guess you are ready😉&lt;/p&gt;

&lt;h1&gt;
  
  
  Prerequisites
&lt;/h1&gt;

&lt;p&gt;This service will be built with Node.js and Express.js, Even though you don't need to know Node.js having an understanding of it is an advantage.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Code Editor, I am using VsCode&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A database, I'll be using MongoDB&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Understanding of Javascript&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Getting Started
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Setting up codebase
&lt;/h2&gt;

&lt;p&gt;In the directory you want to build the project you can type this in your terminal to create the folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir graphql-lesson

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

&lt;/div&gt;



&lt;p&gt;Then Open the folder with VsCode with this command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd graphql-lesson
code .

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

&lt;/div&gt;



&lt;p&gt;We want to create a &lt;code&gt;package.json&lt;/code&gt; file you can do that quickly by running this command in your terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y

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

&lt;/div&gt;



&lt;p&gt;Since we'll be using ES6 throughout this lesson we'll need to add &lt;code&gt;type:module&lt;/code&gt; in the &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;We need to create all files and folders we'll be using to build this project, You can easily do that by running this command in your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir model
touch index.js resolvers.js typeDefs.js model/user.js connect.js

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

&lt;/div&gt;



&lt;p&gt;And your folder should look like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LaS3wrhp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1676059277875/a23766fd-4343-4af6-b266-987e2398e044.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LaS3wrhp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1676059277875/a23766fd-4343-4af6-b266-987e2398e044.jpeg" alt="" width="273" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Dependencies
&lt;/h2&gt;

&lt;p&gt;We need to install some dependencies, you can easily do this by running the command below in your terminal&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 dotenv bcryptjs express express-graphql graphql mongoose nodemon @graphql-tools/schema

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

&lt;/div&gt;



&lt;p&gt;In the scripts section in &lt;code&gt;package.json&lt;/code&gt; we are going to add this code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
}

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

&lt;/div&gt;



&lt;p&gt;We want our server to automatically restart when we make changes, which is the main reason why i added "dev" with the value set to "nodemon index.js".&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating server
&lt;/h2&gt;

&lt;p&gt;We want to create our server with express.js, But before doing that we want to make sure we connect files that include database connection, Type definitions and resolvers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connecting to database
&lt;/h3&gt;

&lt;p&gt;We'll be connecting to our database where users details will be stored, you can easily do this by copying this code below into the &lt;code&gt;connect.js&lt;/code&gt; file :&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 mongoose from "mongoose";
import "dotenv/config";
const app = express();

mongoose.set("strictQuery", false);

mongoose
  .connect(process.env.MONGO_URI_LOCAL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() =&amp;gt; console.log("DB Connected!"))
  .catch((err) =&amp;gt; {
    console.log("not able to connect to database"+ err);
  });

export default app;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Connecting Type Definition file
&lt;/h3&gt;

&lt;p&gt;Type definitions in GraphQL define the structure of your data and the operations that can be performed on that data.&lt;/p&gt;

&lt;p&gt;Since we will be needing type definitions in GraphQL, we want to set up an example code in the &lt;code&gt;typeDefs.js&lt;/code&gt; file, you can easily do that by copying the code below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { buildSchema } from 'graphql';

export const typeDefs = buildSchema(`
  type Query {
    message: String
  }
`
);

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Connecting Resolvers file
&lt;/h3&gt;

&lt;p&gt;Resolvers in GraphQL are functions that resolve the data for a single field in a query or mutation.&lt;/p&gt;

&lt;p&gt;Since we will be needing resolvers, we want to set up an example code in the &lt;code&gt;resolvers.js&lt;/code&gt; file, you can easily do that by copying the code below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const resolvers = {
    Query : {
        message : () =&amp;gt; 'Hello!'
    }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating a server with express.js
&lt;/h3&gt;

&lt;p&gt;Now we want to create our server with express.js, You can do that easily by copying this code below into the &lt;code&gt;index.js&lt;/code&gt; file :&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 { graphqlHTTP } from 'express-graphql';
import "./connect.js";
import { typeDefs } from './typeDefs.js';
import { resolvers } from './resolvers.js';
import { makeExecutableSchema } from '@graphql-tools/schema';
const app = express();

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

app.use('/graphql', graphqlHTTP({
    schema: schema,
    graphiql: true,
}))

app.listen(8000, () =&amp;gt;{
    console.log(`Running a GraphQL API server at localhost:8000/graphql`)
})

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

&lt;/div&gt;



&lt;p&gt;You can start your server with the &lt;code&gt;npm run dev&lt;/code&gt; command, your graphQL playground should be accessible at &lt;a href="http://localhost:8000/graphql"&gt;http://localhost:8000/graphql&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding secret variables to .env file
&lt;/h3&gt;

&lt;p&gt;We need to add Secret variables to the dotenv(.env) file, firstly you can create the file with this command &lt;code&gt;touch .env&lt;/code&gt; , Then get your MongoDB Local URI from Mongo Atlas connection string section, Your Local URI should be similar to this below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGO_URI_LOCAL=mongodb://127.0.0.1:27017/your_database_name

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating user model
&lt;/h2&gt;

&lt;p&gt;The Next thing we want to do now is create a user model, this is the blueprint of how the user data will be stored, You can easily copy this code below&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 Schema = mongoose.Schema;

const UserSchema = new Schema({
    email: {
        type: String
    },
    name: {
        type: String
    },
    about: {
        type: String
    },
    password: {
        type: String
    }
})

export default mongoose.model('User', UserSchema);

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Creating a new user with Graphql
&lt;/h1&gt;

&lt;p&gt;We want to create a new user, These are the essential requirements for creating a new user :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Input Type: This is a type that allows you to pass arguments to a field, It is simply the blueprint of the input details that will be collected from the user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Object Type: As I explained above it defines the structure of your data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mutation Type: This is used to define GraphQL operations, It contains Mutations that can take input arguments and then send a return type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Resolver: This is a function that is used to resolve the data for a single field&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now Let's start according to the essentials above&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Input type
&lt;/h2&gt;

&lt;p&gt;As explained above, The Input object type is a blueprint of input details that will be collected from the user. We want to collect the name, email, password and about from the user and also name the input type UserInput, you can do that easily by defining a new type with the "input" keyword and then all the fields you want to collect and their types. This will be our UserInput :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;input UserInput {
      name: String!
      email: String!
      password: String!
      about: String
    }

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

&lt;/div&gt;



&lt;p&gt;You should insert it into typeDefs variable in &lt;code&gt;typeDefs.js&lt;/code&gt; file&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating user object Type
&lt;/h2&gt;

&lt;p&gt;An object type is a data type that represents an object, it consists of fields that define the properties of an object, It defines the structure of the data that can be returned in a graphQL API. We want our User type to be able to return the id, name, email and about of a user. Our graphQL API will be able to return all these but what a user receives is dependent on the field they insert in the query. don't fret I'll show you what I mean by that soon.&lt;/p&gt;

&lt;p&gt;You can create the user type easily by copying this code below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type User {
    _id: ID!
    name: String!
    email: String!
    about: String
  }

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

&lt;/div&gt;



&lt;p&gt;And then you should insert it into typeDefs variable in &lt;code&gt;typeDefs.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a createUser resolver
&lt;/h2&gt;

&lt;p&gt;As explained above a resolver resolves the data for a single field in a query or mutation.&lt;/p&gt;

&lt;p&gt;We want to make sure the resolver collects the name, email, password, confirm_password and about from a user then encrypts the password using bcryptjs if it matches confirm password.&lt;/p&gt;

&lt;p&gt;Now the first requirement is to specify the operation which is Mutation, you can easily do that by inserting a Mutation object into the resolvers variable in &lt;code&gt;resolvers.js&lt;/code&gt; file and then inserting the resolver function in it :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mutation : {
    createUser : async (_, { input }) =&amp;gt; {
            const { name, email, password, confirm_password, about } = input;
            if( password == confirm_password ){
                const encrypted_password = await bcrypt.hash(password, 12);
                const new_user = new User({ name, email, password: encrypted_password, about });
                await new_user.save();
                return new_user;
            }else{
                throw new Error('Password does not match');
            }
        }
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating Mutation type and Mutation operation for createUser
&lt;/h2&gt;

&lt;p&gt;What we want to do now is create a mutation type and then insert the createUser operation which will contain the input argument and then return the user.&lt;/p&gt;

&lt;p&gt;You can do that easily by copying the code below and inserting it in the typeDefs variable in &lt;code&gt;typeDefs.js&lt;/code&gt; file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Mutation {
    createUser(input: UserInput): User
  }

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Getting user details with GraphQL
&lt;/h1&gt;

&lt;p&gt;We want a user to be able to send details of a user when an Id is provided, these are the steps needed to accomplish this :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We need to create a query resolver which will be named getUserById, it will collect the Id of a user and then return the user object if found in the database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We will then create a query operation that is going to take Id as an argument and then return the user type&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Creating a resolver for getUserById
&lt;/h2&gt;

&lt;p&gt;As mentioned above this resolver will collect the user id and then return the user if found, you can easily do that by inserting the code below into the query object in the resolvers variable that is in &lt;code&gt;resolvers.js&lt;/code&gt; file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getUserById : async (_, { id }) =&amp;gt; {
            const user = await User.findById(id);
            if(user){
                return user
            }else{
                throw new Error('User does not exist');
            }
        }

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating a query operation for getUserById
&lt;/h2&gt;

&lt;p&gt;Now we need to create a query operation that will accept id as an argument and then return the user type, you can do that easily by copying the code below into the query type in the typeDefs variable which is in the &lt;code&gt;typeDefs.js&lt;/code&gt; file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getUserById(id: ID!): User

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Updating User details with GraphQL
&lt;/h1&gt;

&lt;p&gt;What we want to do now is to be able to update a user's details when an id is provided and the field they want to update. To be able to achieve this we are going to follow these steps&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We need to create a resolver that will collect user id and input and then update the data, this resolver will be named updateUser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need to create a mutation operation that collects the Id and input as arguments and then returns the user type.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Creating a resolver for updateUser
&lt;/h2&gt;

&lt;p&gt;As explained above the updateUser resolver will be taking the id of the user and the field that needs to be updated, you can easily create this resolver by copying the code below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;updateUser: async (_, { id, input }) =&amp;gt; {
            const updatedUser = await User.findByIdAndUpdate(id, input, { new: true });
            return updatedUser;
          }

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

&lt;/div&gt;



&lt;p&gt;And then insert it into the Mutation object in the resolvers variable which is in &lt;code&gt;resolvers.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a mutation operation for updateUser
&lt;/h2&gt;

&lt;p&gt;What we want to do now is create the updateUser mutation operation, this operation will collect the id, and the field the user wants to update and then return the user type, you can do this easily by copying the code below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;updateUser(id: ID!, input: UserInput!): User!

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

&lt;/div&gt;



&lt;p&gt;And then insert it into the mutation type in the typeDefs variable which is in &lt;code&gt;typeDef.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h1&gt;
  
  
  Deleting User with GraphQL
&lt;/h1&gt;

&lt;p&gt;We want to be able to delete a user with GraphQL, these are the steps you'll be following in achieving this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a resolver that will collect the user's Id and then delete the user details, this resolver will be named deleteUser&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a mutation operation that will accept the User's Id as an argument and then return the user type&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Creating a resolver for deleteUser
&lt;/h2&gt;

&lt;p&gt;This resolver function will take the user's Id and then delete the data from the database, This can be done easily by copying the code below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;deleteUser: async (_, { id }) =&amp;gt; {
        const deletedUser = await User.findByIdAndDelete(id);
        if (!deletedUser) {
            throw new Error(`User with ID ${id} not found`);
        }
        return deletedUser;
        }

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

&lt;/div&gt;



&lt;p&gt;An then inserting it into the Mutation object which is in the resolvers variable in &lt;code&gt;resolvers.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a mutation operation for deleteUser
&lt;/h2&gt;

&lt;p&gt;The next thing we want to do is create the deleteUser mutation operation, it will accept Id as an argument and then return the user type, this can be done easily by copying the code below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;deleteUser(id: ID!): User

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

&lt;/div&gt;



&lt;p&gt;And then insert it into the mutation type in the typeDefs variable which is in &lt;code&gt;typeDef.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h1&gt;
  
  
  Testing APIs
&lt;/h1&gt;

&lt;p&gt;Now we want to test the CRUD operation, we'll be using the GraphQL playground which can be accessed through &lt;a href="http://localhost:8000/graphql"&gt;http://localhost:8000/graphql&lt;/a&gt; in your browser.&lt;/p&gt;

&lt;p&gt;Note: Make sure the server is running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create User API
&lt;/h2&gt;

&lt;p&gt;To create a User we will specify the operation we are going to be using which is the mutation operation, and specify the mutation operation name which is createUser, then the argument will contain the input key and all the fields we'll be collecting from the user, It should look like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--p0RaBNt8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677792932524/bd787b42-3ea6-45ee-8b5d-472f1d543ec2.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--p0RaBNt8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677792932524/bd787b42-3ea6-45ee-8b5d-472f1d543ec2.jpeg" alt="" width="880" height="107"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then to execute query, you'll click on the execute query button that looks like a play button, we should get this as our result :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GKfO_nQ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677793143811/67afc250-7d0b-4c5c-b59b-f5cf0c8fbb6b.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GKfO_nQ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677793143811/67afc250-7d0b-4c5c-b59b-f5cf0c8fbb6b.jpeg" alt="" width="439" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It means a new user was created successfully.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get User Details API
&lt;/h2&gt;

&lt;p&gt;To get User details, we will specify the operation we are going to be using which is the query operation, and specify the mutation operation name which is getUserById, then the argument will contain the id and then the detail or details we want to get from the return type. it should look like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--elub34pn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677795437266/11edd069-f962-4ba2-855a-a45ea4c55a2c.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--elub34pn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677795437266/11edd069-f962-4ba2-855a-a45ea4c55a2c.jpeg" alt="" width="592" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After executing the query the result should look like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3I0k6Gen--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677795514398/c42cea53-c836-47a1-8efb-92861b39515b.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3I0k6Gen--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677795514398/c42cea53-c836-47a1-8efb-92861b39515b.jpeg" alt="" width="312" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Update User Details API
&lt;/h2&gt;

&lt;p&gt;To update user details, we will specify the operation we are going to be using which is the mutation operation, and specify the mutation operation name which is updateUser, then the argument will contain the id and finally the detail we want to get from the return type. it should look like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_CHZWVAr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677795851429/50c2aa7f-3e5e-46e4-bb70-72127e6f0408.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_CHZWVAr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677795851429/50c2aa7f-3e5e-46e4-bb70-72127e6f0408.jpeg" alt="" width="795" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After Executing the query the result should look like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--402_yMlI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677795936621/aca923df-11a1-4e6c-b5cc-36e2410e91d2.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--402_yMlI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677795936621/aca923df-11a1-4e6c-b5cc-36e2410e91d2.jpeg" alt="" width="319" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Delete User API
&lt;/h2&gt;

&lt;p&gt;To delete a user, we will specify the operation we'll be using which is the mutation operation, and specify the mutation operation name which is deleteUser, then the argument will contain the id and finally the detail we want to get from the return type. it should look like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AQU54SDF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677796097905/53f70b50-477e-4231-b3e6-d4f7affa02ba.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AQU54SDF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677796097905/53f70b50-477e-4231-b3e6-d4f7affa02ba.jpeg" alt="" width="558" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After executing the query the result should look like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RsTQ0A5u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677796177251/90a483dd-000a-450f-bd25-45b67a8a612d.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RsTQ0A5u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1677796177251/90a483dd-000a-450f-bd25-45b67a8a612d.jpeg" alt="" width="298" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;In conclusion, GraphQL provides a very powerful and efficient way to create CRUD APIs which allows developers to retrieve only the data they need, It helps reduce network round trips, and simplifies API versioning.&lt;/p&gt;

&lt;p&gt;The full code is available &lt;a href="https://github.com/DevMukhtarr/GraphQL-CRUD"&gt;Here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You've come this far thanks for reading, you can follow me on &lt;a href="https://twitter.com/DevMukhtar"&gt;&lt;strong&gt;Twitter&lt;/strong&gt;&lt;/a&gt;, &lt;a href="https://github.com/DevMukhtarr"&gt;&lt;strong&gt;GitHub&lt;/strong&gt;&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/abdulfatai-mukhtar-77a2131b1/"&gt;&lt;strong&gt;LinkedIn&lt;/strong&gt;&lt;/a&gt; for more backend development tips.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Using ChatGPT to build a pizza delivery service</title>
      <dc:creator>Mukhtar</dc:creator>
      <pubDate>Sat, 14 Jan 2023 09:25:59 +0000</pubDate>
      <link>https://dev.to/devmukhtarr/using-chatgpt-to-build-a-pizza-delivery-service-4860</link>
      <guid>https://dev.to/devmukhtarr/using-chatgpt-to-build-a-pizza-delivery-service-4860</guid>
      <description>&lt;p&gt;ChatGPT is a chatbot launched by OpenAI, It can generate questions to your answers within its knowledge, We'll be using it to build a Pizza delivery service, and if the results we get from it aren't what we expect, then we are going to tweak them to our liking.&lt;/p&gt;

&lt;p&gt;You can easily set up an account by following &lt;a href="https://chat.openai.com/chat"&gt;this link&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pizza delivery is a highly competitive and demanding industry, with customers expecting fast, accurate, and reliable service. The ability to quickly and efficiently fulfill orders is key to success in this market. In this guide, we will outline the steps and considerations involved in creating a pizza delivery service with chatGPT&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Setting up codebase
&lt;/h3&gt;

&lt;p&gt;We'll be using ChatGPT and Node.js to build the Pizza delivery service, You need to setup your codebase, After running &lt;code&gt;npm init -y&lt;/code&gt; in your terminal, You can easily create folders and files by running this command below in your terminal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;mkdir config controllers middlewares models routes&lt;/p&gt;

&lt;p&gt;touch config/connect.js controllers/maincontroller.js controllers/authcontroller.js middlewares/auth.js models/user.js routes/mainroute.js routes/authroute.js .gitignore app.js index.js&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R-MqP6Gu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673439945579/318622b5-2f5f-4551-a944-f2552291df74.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R-MqP6Gu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673439945579/318622b5-2f5f-4551-a944-f2552291df74.jpeg" alt="Folder Structure" width="273" height="571"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating your server
&lt;/h3&gt;

&lt;p&gt;Now you want to create a server with express.js this can be easily done by adding this code into the app.js file :&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 cors from "cors"
import connect from "./config/connect.js";
import authroutes from "./routes/authroute.js"
import mainroutes from "./routes/mainroute.js"
const app = express()

app.use(connect)
app.use(
    express.urlencoded({
      extended: false,
    })
  );

app.use(cors())
app.use(express.json());
app.use(authroutes)
app.use(mainroutes)
export default app

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

&lt;/div&gt;



&lt;p&gt;And This in your index.js file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "dotenv/config"
import app from "./app.js";
const port = process.env.PORT

app.get("/", (req, res) =&amp;gt;{
    res.send("Pizza delivery service")
})

app.listen(port, () =&amp;gt;{
    console.log(`app is running at ${port}`)
})

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installing dependencies
&lt;/h3&gt;

&lt;p&gt;You'll be installing all dependencies needed by running this command below in your terminal :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm i mongoose dotenv express jsonwebtoken bcryptjs nodemon&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now you'll add &lt;code&gt;"dev": "nodemon index.js"&lt;/code&gt; to scripts in &lt;code&gt;package.json&lt;/code&gt; file and &lt;code&gt;"type": "module"&lt;/code&gt;, the former is for the server to restart automatically and the latter is to enable ES6 modules, your &lt;code&gt;package.json&lt;/code&gt; file should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "pizza-delivery-service",
  "version": "1.0.0",
  "description": "A pizza delivery service built with chatGPT",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "jsonwebtoken": "^9.0.0",
    "mongoose": "^6.8.3",
    "nodemon": "^2.0.20"
  },
  "type": "module"
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating a .env file and adding necessary environment variables
&lt;/h3&gt;

&lt;p&gt;In your &lt;code&gt;.env&lt;/code&gt; file you are going to add&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Mongo DB URI&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JWT SECRET KEY&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PORT Number&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can add all of them by copying this code below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGO_URI_LOCAL=mongodb://127.0.0.1:27017/pizza-delivery
JWT_SECRET_KEY=e98f6a81f9d84b6d7b6a9a0a04dfc5d5f5f1761f0b827dd2db9e5b5e5fae58f
PORT=8080

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

&lt;/div&gt;



&lt;p&gt;Note: I am using Local MongoDB on My computer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connecting to your database
&lt;/h3&gt;

&lt;p&gt;You can easily connect to your database using this code below :&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 mongoose from "mongoose";
import "dotenv/config";
const app = express();

mongoose.set("strictQuery", false);

mongoose
  .connect(process.env.MONGO_URI_LOCAL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() =&amp;gt; console.log("DB Connected!"))
  .catch((err) =&amp;gt; {
    console.log("not able to connect to database"+ err);
  });

export default app;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating Schema
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Creating a user schema
&lt;/h3&gt;

&lt;p&gt;Now we want to create a schema in the &lt;code&gt;user.js&lt;/code&gt; file, I sent create a schema that contains email, name, role and address in mongodb and it gave me this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oWrUBGFg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673521104717/61027968-8919-4f95-9b9f-5efb40d56c9b.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oWrUBGFg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673521104717/61027968-8919-4f95-9b9f-5efb40d56c9b.jpeg" alt="" width="819" height="799"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we stated above that we'll be using module, you'll just need to change &lt;code&gt;const mongoose = require('mongoose');&lt;/code&gt; to &lt;code&gt;import mongoose from "mongoose";&lt;/code&gt; and the last line to &lt;code&gt;export default mongoose.model("User", UserSchema)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating an order schema
&lt;/h3&gt;

&lt;p&gt;Now we want to create an order schema, you'll create a new file and name it order.js in the models folder, you can easily do that by running this command in your terminal :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;touch models/order.js&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To Create the order schema i sent create an order schema that includes customer, pizza, size, createdAt and status where customer is referencing user and pizza has enum containing 'Neapolitan Pizza', 'Chicago Pizza', 'Sicilian Pizza', 'Greek Pizza', 'Detroit Pizza', size has enum containing 'small', 'medium', 'large' and status has enum containing 'pending', 'in progress', 'completed' To chatGPT and i got this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YowJjdz---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673530377651/c9133f5e-1d87-42ce-841b-70900825b02f.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YowJjdz---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673530377651/c9133f5e-1d87-42ce-841b-70900825b02f.jpeg" alt="" width="585" height="850"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the type is module, you'll just need to change &lt;code&gt;const mongoose = require('mongoose');&lt;/code&gt; to &lt;code&gt;import mongoose from "mongoose";&lt;/code&gt; and the last line to &lt;code&gt;export default mongoose.model("PizzaOrder", PizzaOrderSchema)&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating SignUp and SignIn controller
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Signup Controller
&lt;/h3&gt;

&lt;p&gt;Now I'll tell chatGPT to build a signup controller for me letting it know all the tools I'm using let's see how well it does.&lt;/p&gt;

&lt;p&gt;We want to make sure only two roles are available in the service that is the &lt;strong&gt;delivery personnel and the user role&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Note: We are creating these two roles alone to keep it simple.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;User: Is going to order the type of pizza they want among the five main types our service offers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Delivery Person: Is going to get the order and deliver the pizza to the user's address.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: I told chatGPT my Javascript is module based.&lt;/p&gt;

&lt;p&gt;I sent create a signup controller that checks if a user exists, if they don't exist create a new user collecting their email, name, role and address and then create a JWT with user id and email in payload and JWT_SECRET_KEY as secret key to chatGPT and it gave me this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UDdECX5B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673533086527/af7addea-d121-4eaf-bc16-32ac08edd67a.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UDdECX5B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673533086527/af7addea-d121-4eaf-bc16-32ac08edd67a.jpeg" alt="" width="594" height="814"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  SignIn Controller
&lt;/h3&gt;

&lt;p&gt;Now I'll tell chatGPT to build a SignIn controller, Let's see how well it does.&lt;/p&gt;

&lt;p&gt;Before we continue I forgot to add password to the user schema and also save the encrypted password into the database from the signup controller so I'm going to add this code to the &lt;code&gt;user.js&lt;/code&gt; file in the UserSchema declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;password: {
        type: String
    }

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

&lt;/div&gt;



&lt;p&gt;And Add this in the signup controller Below the If statement checking if the user exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const password = req.body.password;
const encryptedPassword = await bcrypt.hash(password, 12);

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

&lt;/div&gt;



&lt;p&gt;And then add &lt;code&gt;password: encryptedPassword&lt;/code&gt; into the schema instance making the complete User schema instance look like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user = new User({
            email: req.body.email,
            name: req.body.name,
            role: req.body.role,
            address: req.body.address,
            password: encryptedPassword
        });

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

&lt;/div&gt;



&lt;p&gt;I sent build a signin controller that checks if a user exists, if they do check if the password matches with bcryptjs, if does create a JWT with id and email in payload and send it as a response to chatGPT and it gave me this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v0QNl1Uf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673534045386/942091ed-7f8e-4073-ab86-7beae1635cbf.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v0QNl1Uf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673534045386/942091ed-7f8e-4073-ab86-7beae1635cbf.jpeg" alt="" width="597" height="762"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing SignUp and SignIn endpoints
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Testing Signup endpoint
&lt;/h3&gt;

&lt;p&gt;To be sure we are on the right path we'll test the controllers built so far before starting the main service.&lt;/p&gt;

&lt;p&gt;I sent Create a route for the signup controller with endpoint name set to signup using module type to chatGPT and it gave me this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---ux43FxQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673534800321/29a9bf8b-2239-41f1-a759-de1dc206b161.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---ux43FxQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673534800321/29a9bf8b-2239-41f1-a759-de1dc206b161.jpeg" alt="" width="613" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'll add this code above into my &lt;code&gt;authroute.js&lt;/code&gt; file, then in the &lt;code&gt;app.js&lt;/code&gt; I'll add these below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import authroutes from "./routes/authroute.js"

app.use(authroutes)// this below app.use(express.json())

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

&lt;/div&gt;



&lt;p&gt;I tested the endpoint and it worked fine as seen below :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E7-Kg6LU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673561585252/49a9d024-5176-4966-9116-ddfa2d931bc3.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E7-Kg6LU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673561585252/49a9d024-5176-4966-9116-ddfa2d931bc3.jpeg" alt="" width="880" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing SignIn endpoint
&lt;/h3&gt;

&lt;p&gt;I imported SignIn controller and then created &lt;code&gt;/signin&lt;/code&gt; endpoint using router this way :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {signup, signin} from '../controllers/AuthController.js';

router.post('/signin', signin);

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

&lt;/div&gt;



&lt;p&gt;I tested the endpoint and it also worked fine :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AioO5n81--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673562028799/ab7acb0c-f104-4ced-b62d-fd1fdb0b4924.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AioO5n81--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673562028799/ab7acb0c-f104-4ced-b62d-fd1fdb0b4924.jpeg" alt="" width="880" height="511"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating controllers for the Pizza service
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Get all pizzas controller
&lt;/h3&gt;

&lt;p&gt;This controller will be connected to an endpoint that shows users all the types of pizzas we have, you'll add this into the &lt;code&gt;maincontroller.js&lt;/code&gt; file&lt;/p&gt;

&lt;p&gt;I sent if all pizzas available are 'Neapolitan Pizza', 'Chicago Pizza', 'Sicilian Pizza', 'Greek Pizza', 'Detroit Pizza' create a controller to list all of them to chatGPT I got this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Iu2ap4hr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673563078912/7e201b79-e09e-40f5-ac30-ca679be3b9db.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Iu2ap4hr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673563078912/7e201b79-e09e-40f5-ac30-ca679be3b9db.jpeg" alt="" width="601" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create Order Controller
&lt;/h3&gt;

&lt;p&gt;This is the controller that'll be used to create a new pizza order, I sent Write a controller where a user can order pizza referencing the customer to userid, collecting the type of pizza they want, the size, setting the status to pending and setting createdAt to the time the order was created in module js And I got this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VHK6IDBp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673603309112/c566036b-d964-4805-a3ed-0c3c03e07ca4.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VHK6IDBp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673603309112/c566036b-d964-4805-a3ed-0c3c03e07ca4.jpeg" alt="" width="601" height="613"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This code above will also be added into the &lt;code&gt;maincontroller.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  View Pending Orders
&lt;/h3&gt;

&lt;p&gt;Before we go any further I remember I didn't add the role as a payload in the JWT, so I'm going to do that in the SignUp and SignIn controller like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const token = jwt.sign({ id: user._id, email: user.email, role: user.role }, JWT_SECRET_KEY);

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

&lt;/div&gt;



&lt;p&gt;The pending orders controller will be used to see all pending orders, I sent write a controller so the delivery man and user can see all pending orders and I got this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D0bTRPnM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673628589509/2d9b911f-4b2c-41f1-ab0a-6a0655f338e8.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D0bTRPnM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673628589509/2d9b911f-4b2c-41f1-ab0a-6a0655f338e8.jpeg" alt="" width="835" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This also will be added into the &lt;code&gt;maincontroller.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Update Status as a delivery personnel
&lt;/h3&gt;

&lt;p&gt;This is the controller the delivery personnel will use to update the order status, I sent write a controller to check if the role is delivery personnel and then update order status and I got this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9Gtuya2P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673607419885/9e79f521-a678-4129-be68-bb9e472b3b28.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9Gtuya2P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673607419885/9e79f521-a678-4129-be68-bb9e472b3b28.jpeg" alt="" width="880" height="789"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This also will be added into the &lt;code&gt;maincontroller.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating and Testing Pizza Service endpoints
&lt;/h2&gt;

&lt;p&gt;Now we want to create endpoints for all the pizza delivery service controllers we created earlier, I sent The createOrder, getPizzas, getPendingOrders and updateOrderStatus is in "../controllers/maincontroller.js", the verifyToken is in "../middlewares/auth.js" create endpoints for them according to their paths to chatGPT and i got this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wWbQFuFQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673609428932/dfdf646d-f692-42e2-8029-54f545cabfec.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wWbQFuFQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673609428932/dfdf646d-f692-42e2-8029-54f545cabfec.jpeg" alt="" width="789" height="823"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since I created endpoints for SignIn and SignUp earlier, I'll Delete the endpoints for them.&lt;/p&gt;

&lt;p&gt;I added all of these into the &lt;code&gt;mainroute.js&lt;/code&gt; file, The next thing I want to do is connect the &lt;code&gt;mainroute.js&lt;/code&gt; file to the &lt;code&gt;app.js&lt;/code&gt; file so all the endpoints can be accessible.&lt;/p&gt;

&lt;p&gt;You can do that easily by adding the lines of code below into the &lt;code&gt;app.js&lt;/code&gt; file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import mainroutes from "./routes/mainroute.js"

app.use(mainroutes)//below app.use(express.json());

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Testing Get Pizzas Endpoint
&lt;/h3&gt;

&lt;p&gt;I tested the &lt;code&gt;/pizzas&lt;/code&gt; endpoint and it worked fine as seen below :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SABfquxC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673622647504/96848ebe-b057-4d08-9b4c-c314e09b98ed.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SABfquxC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673622647504/96848ebe-b057-4d08-9b4c-c314e09b98ed.jpeg" alt="" width="880" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing Create Order Endpoint
&lt;/h3&gt;

&lt;p&gt;I inserted the JWT in the header with the x-access-token as the key, You may be wondering what function is checking the JWT, I have this code already in my "../middlewares/auth.js" file :&lt;br&gt;
&lt;/p&gt;

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

const config = process.env;

export const verifyToken = (req, res, next) =&amp;gt; {
  const token =
    req.body.token || req.query.token || req.headers["x-access-token"];

  if (!token) {
    return res.status(403).send("A token is required for authentication");
  }
  try {
    const decoded = jwt.verify(token, config.JWT_SECRET_KEY);
    req.user = decoded;
  } catch (err) {
    return res.status(401).send("Invalid Token");
  }
  return next();
};

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

&lt;/div&gt;



&lt;p&gt;I then tested the &lt;code&gt;/order&lt;/code&gt; Endpoint and it also worked fine as seen below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tA1WF5Hg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673623729736/5c9077b9-1b88-4c85-a790-3f3562528d59.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tA1WF5Hg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673623729736/5c9077b9-1b88-4c85-a790-3f3562528d59.jpeg" alt="" width="880" height="554"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing Get Pending Orders Endpoint
&lt;/h3&gt;

&lt;p&gt;I created a new account as delivery personnel and then tested the &lt;code&gt;/orders/pending&lt;/code&gt; endpoint which gets all pending orders and it also worked fine as seen below :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sD9rpOOH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673624593603/ee693235-7e23-4a27-b8dd-e18d38a91622.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sD9rpOOH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673624593603/ee693235-7e23-4a27-b8dd-e18d38a91622.jpeg" alt="" width="880" height="508"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing the update order status endpoint
&lt;/h3&gt;

&lt;p&gt;I Copied the Order Id and set the status to "in progress" then tested the &lt;code&gt;/order/:id&lt;/code&gt; and it worked fine as seen below :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--urOF1MxZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673630598229/2bff09b2-3134-485c-9005-8ddbc313925c.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--urOF1MxZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673630598229/2bff09b2-3134-485c-9005-8ddbc313925c.jpeg" alt="" width="880" height="528"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So you've come this far. In my opinion, chatGPT did well, I edited a few things like the file extensions when I received errors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I added &lt;code&gt;.js&lt;/code&gt; to files to the files I imported&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I deleted the signIn and signUp controller that was repeated&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But Generally, It did very well asides from these.&lt;/p&gt;

&lt;p&gt;You may want to play with the code or improve some sections, The complete source code is available &lt;a href="https://github.com/DevMukhtarr/chatgpt-pizza-service"&gt;Here&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Experience is the name everyone gives to their mistakes. &lt;em&gt;Oscar Wilde&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can check more on chatGPT &lt;a href="https://openai.com/blog/chatgpt/"&gt;Here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/DevMukhtar"&gt;Twitter&lt;/a&gt;, &lt;a href="https://github.com/DevMukhtarr"&gt;GitHub&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/abdulfatai-mukhtar-77a2131b1/"&gt;LinkedIn&lt;/a&gt; for more backend development tips.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Authenticate With JWT.</title>
      <dc:creator>Mukhtar</dc:creator>
      <pubDate>Sat, 05 Nov 2022 14:51:01 +0000</pubDate>
      <link>https://dev.to/devmukhtarr/how-to-authenticate-with-jwt-187p</link>
      <guid>https://dev.to/devmukhtarr/how-to-authenticate-with-jwt-187p</guid>
      <description>&lt;h1&gt;
  
  
  How to Authenticate With JSON Web Token (JWT)
&lt;/h1&gt;

&lt;p&gt;In this article you are going to learn:&lt;/p&gt;

&lt;p&gt;What JSON Web Token (JWT) is.&lt;/p&gt;

&lt;p&gt;How to create JSON Web Token (JWT).&lt;/p&gt;

&lt;p&gt;How to Authenticate and Authorize With JSON Web Token (JWT)&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/JSON_Web_Token"&gt;JSON Web Token&lt;/a&gt; is a standard for creating and sharing encrypted data.&lt;/p&gt;

&lt;p&gt;Structure of a JWT :&lt;/p&gt;

&lt;p&gt;A JWT contains a &lt;strong&gt;Header&lt;/strong&gt; , &lt;strong&gt;Payload&lt;/strong&gt; and &lt;strong&gt;Signature&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Header
&lt;/h2&gt;

&lt;p&gt;The first part of a JWT is the Header, It contains the algorithm that will be used to encrypt and token type.&lt;/p&gt;

&lt;p&gt;Here is an example of a header:&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;And here is an example of the header in JSON format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "alg": "HS256",
  "typ": "JWT"
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Payload
&lt;/h2&gt;

&lt;p&gt;The second part of JWT is the Payload, It contains the claims which are statements about the user and additional data.&lt;/p&gt;

&lt;p&gt;Here is an example of a payload:&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;And here is an example of the payload in JSON format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Signature
&lt;/h2&gt;

&lt;p&gt;The third or last part of JWT is the signature, It is used to verify the owner of the message and if it wasn't changed along the way, To create the signature the &lt;strong&gt;Base64-encoded header&lt;/strong&gt; , &lt;strong&gt;Payload&lt;/strong&gt; and &lt;strong&gt;Secret&lt;/strong&gt; are taken, then signed along with the algorithm specified in the header.&lt;/p&gt;

&lt;p&gt;An example of signature:&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;And here is how the signature is created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
 your-256-bit-secret

) secret base64 encoded

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Prerequisites
&lt;/h1&gt;

&lt;p&gt;Since we'll be building a Backend Service With Node.js and Express, Knowing All these is an advantage, Though I'll be walking you through it if you don't know them.&lt;/p&gt;

&lt;p&gt;JavaScript&lt;/p&gt;

&lt;p&gt;Express.js&lt;/p&gt;

&lt;p&gt;Some skills in version control&lt;/p&gt;

&lt;p&gt;I expect you have Node.js, Any Code Editor, API Testing platform like Postman, MongoDB or any other Database Installed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up server
&lt;/h2&gt;

&lt;p&gt;Create a file and then name it anything you like I'll name mine &lt;code&gt;fullauth&lt;/code&gt; and then initialize the folder with &lt;code&gt;git init&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Now type &lt;code&gt;code .&lt;/code&gt; in your terminal to open the file, this will only work if you are using VsCode.&lt;/p&gt;

&lt;p&gt;The next thing we want to do is initialize npm by executing &lt;code&gt;npm init -y&lt;/code&gt; in our terminal and we should have our &lt;code&gt;package.json&lt;/code&gt; in our folder.&lt;/p&gt;

&lt;p&gt;Now we need to create all folders and files we need, we can easily do that with this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir config controller middleware model routes 

touch config/connect.js controller/authcontroller.js middleware/auth.js model/user.js routes/authroute.js .gitignore app.js index.js .env

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

&lt;/div&gt;



&lt;p&gt;And our folder should look like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PFycQIBg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1666729045552/FkPSOGuLn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PFycQIBg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1666729045552/FkPSOGuLn.jpg" alt="fullauth.jpg" width="258" height="586"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing dependencies
&lt;/h2&gt;

&lt;p&gt;And now we'll be installing all dependencies we need with this 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 i mongoose dotenv express jsonwebtoken bcryptjs nodemon

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

&lt;/div&gt;



&lt;p&gt;Now we'll add &lt;code&gt;"dev": "nodemon index.js"&lt;/code&gt; to scripts in &lt;code&gt;package.json&lt;/code&gt; file and &lt;code&gt;"type": "module"&lt;/code&gt;, the former is for the server to restart automatically and latter is to enable ES6 modules, our &lt;code&gt;package.json&lt;/code&gt; file should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "fullauth",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^6.7.0",
    "nodemon": "^2.0.20"
  },
  "type": "module"
}

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Getting Started
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Creating a Node.js Server
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Connecting to local MongoDB
&lt;/h3&gt;

&lt;p&gt;We'll add this to &lt;code&gt;connect.js&lt;/code&gt; file:&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";
import "dotenv/config";

const connect = {
    connectToLocalMongoDB : () =&amp;gt;{
        mongoose
          .connect(process.env.MONGO_URI_LOCAL, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
          })
          .then(() =&amp;gt; console.log("DB Connected!"))
          .catch((err) =&amp;gt; {
            console.log("not able to connect to database"+ err);
          });
    }
}

export default connect;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding &lt;code&gt;MONGO_URI_LOCAL&lt;/code&gt; to &lt;code&gt;.env&lt;/code&gt; file
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGO_URI_LOCAL=mongodb://127.0.0.1:27017/fullauth

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Updating &lt;code&gt;app.js&lt;/code&gt; file:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import express from 'express'
import connect from "./config/connect.js";
const app = express()

app.use(
    express.urlencoded({
      extended: false,
    })
  );

app.use(express.json());

connect.connectToLocalMongoDB()

export default app

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Starting server with &lt;code&gt;index.js&lt;/code&gt; file
&lt;/h2&gt;

&lt;p&gt;We'll add this to our &lt;code&gt;index.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "dotenv/config";
import app from "./app.js";

const PORT = process.env.PORT || 8080

app.get('/', (req, res) =&amp;gt;{
    res.send("Welcome to Full authentication app")
})

app.listen(PORT, () =&amp;gt;{
    console.log(`app is listening on port ${PORT}`)
})

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding &lt;code&gt;PORT&lt;/code&gt; to &lt;code&gt;.env&lt;/code&gt; file
&lt;/h3&gt;



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

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

&lt;/div&gt;



&lt;p&gt;Now you can run &lt;code&gt;npm run dev&lt;/code&gt; to start the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a User model in &lt;code&gt;user.js&lt;/code&gt; file:
&lt;/h2&gt;

&lt;p&gt;We'll be creating a user model in the &lt;code&gt;user.js&lt;/code&gt; file, this is what we'll be calling to perform operations on the database:&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 User = new mongoose.Schema({
    first_name: {type: String, default: null},
    last_name: {type: String, default: null},
    email: {type: String, default: null},
    password: {type: String, default: null},
})

export default mongoose.model("user", User);

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Implementing SignUp and SignIn Controllers
&lt;/h2&gt;

&lt;p&gt;We want to implement the signUp controller, the &lt;code&gt;/signup&lt;/code&gt; endpoint will be connected to this controller so a new user can register.&lt;/p&gt;

&lt;p&gt;What we'll be doing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;check if user exists&lt;/li&gt;
&lt;li&gt;check if any field is empty&lt;/li&gt;
&lt;li&gt;create a new user and JWT&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Importing dependencies in &lt;code&gt;authcontroller.js&lt;/code&gt; file :
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "dotenv/config";
import user from "../model/user.js";
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating SignUp controller in &lt;code&gt;authcontroller.js&lt;/code&gt; file:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const signUp = async (req, res) =&amp;gt;{
    const { firstname, lastname, email, password } = req.body

    const oldUser = await user.findOne({email: email})

    if(oldUser){
        return res.status(424).json({
            status: false,
            message: "user already exists"
        })
    }

    if(!(firstname &amp;amp;&amp;amp; lastname &amp;amp;&amp;amp; email &amp;amp;&amp;amp; password)){
        return res.status(424).json({
            status: false,
            message: "all fields are required"
        })
    }

    if( oldUser == null ){
        const encrptedPassword = await bcrypt.hash(password, 12);

        const newUser = await user.create({
            first_name: firstname,
            last_name: lastname,
            email: email,
            password: encrptedPassword,
        })

        const token = jwt.sign({id: newUser._id, }, process.env.JWT_SECRET_KEY,{expiresIn: "2d"});

        return res.status(200).json({
            status: true,
            message: "Registration successful",
            data: {
                token: token
            }
        })
    }
}

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

&lt;/div&gt;



&lt;p&gt;What we did :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We imported all dependencies we'll be using&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We Checked if user exists &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We Created a new user if they do not exist.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We used Jwt.sign to create a new token, the &lt;code&gt;.sign&lt;/code&gt; method takes parameters which includes &lt;strong&gt;Payload&lt;/strong&gt; , this contains the claims which are statements about the user and additional data, the second parameter is the secret key we'll use to create a unique hash and then the last parameter I added is the sign option setting the expiry time to 2 days.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Creating SignIn controller in &lt;code&gt;authcontroller.js&lt;/code&gt; file:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const signIn = async (req, res) =&amp;gt;{
    try {
        const { email, password } = req.body;

        const oldUser = await user.findOne({email: email})

        if(!(email &amp;amp;&amp;amp; password)){
            return res.status(424).json({
                status: false,
                message: "all fields are required"
            })
        }

        if( oldUser == null ){
            return res.status(424).json({
                status: false,
                message: "User does not exist"
            })
        }

        if( oldUser &amp;amp;&amp;amp; (await bcrypt.compare(password, oldUser.password))){
            const token = jwt.sign({id: oldUser._id, email:email }, process.env.JWT_SECRET_KEY,{expiresIn: "2d"});
            return res.status(200).json({
                status: true,
                message: "Login Successful",
                data:{
                    token: token
                }
            })
        }else{
            return res.status(424).json({
                status: false,
                message: "Incorrect email or password"
            })  
        } 
    } catch (error) {
        return res.status(500).json({
            status: false,
            message: "An error occurred"
        })  
    }
}

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

&lt;/div&gt;



&lt;p&gt;What we did :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Check if User exist using email address provided&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If user exists and password matches the one in the Database, create a JWT with Id and email as payload , the secret in our &lt;code&gt;.env&lt;/code&gt; file as signature and setting the expiry time to 2 days.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return response that includes the JWT.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Creating a middleware that checks and decode JWT in &lt;code&gt;auth.js&lt;/code&gt; file :
&lt;/h3&gt;



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

export const verifyToken = async (req, res, next) =&amp;gt;{
    try {
        const token = req.body.token || req.query.token || req.headers['access-token']

        if(!token){
            return res.status(403).json({
                status: false,
                message: `A token is required for authentication`
            })
        }

        const decoded = jwt.verify(token, process.env.JWT_SECRET_KEY)

        req.user = decoded

    } catch (error) {
        return res.status(401).json({
            status: false,
            message: `Invalid Token`
        })
    }
    return next();
}

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

&lt;/div&gt;



&lt;p&gt;What we did :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Check for JWT in request Body, Query or Header with ' access-token ' and key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If JWT is provided decode it with &lt;code&gt;jwt.verify&lt;/code&gt; method, this method takes two parameters i.e. the JWT and the secret key that was used to sign it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assign the decoded object to &lt;code&gt;req.user&lt;/code&gt; so after authentication routes can assess it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return " Invalid code " if code is expired or tampered with.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Creating a controller that greets an authenticated user in &lt;code&gt;authcontroller.js&lt;/code&gt; file :
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const greetUser = async (req, res) =&amp;gt;{
    try {
        const id = req.user.id;

        const oldUser = await user.findById(id);

        if( oldUser ){
            return res.status(200).json({
                status: true,
                message: `Hello ${oldUser.first_name}`
            })
        }else{
            return res.status(498).json({
                status: false,
                message: `Invalid Id`
            })
        }

    } catch (error) {
        return res.status(500).json({
            status: false,
            message: "An error occurred"
        })  
    }
}

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

&lt;/div&gt;



&lt;p&gt;What we did :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Get the id from &lt;code&gt;req.user&lt;/code&gt; when user is authenticated&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find user by id from Database&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return response greeting user with name in Database.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Creating and using Routes
&lt;/h2&gt;

&lt;p&gt;The Next thing we want to do is create endpoints, connect them to controllers in &lt;code&gt;authroute.js&lt;/code&gt; file and import them in &lt;code&gt;app.js&lt;/code&gt; file so it can be accessible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating and Connecting routes in &lt;code&gt;authroute.js&lt;/code&gt; file :
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Router } from "express";
import { signUp, signIn, greetUser} from "../controller/authcontroller.js";
import { verifyToken } from "../middleware/auth.js";
const router = Router();

router.route("/signup").post(signUp);
router.route("/signIn").post(signIn);
router.route("/greet-user").get(verifyToken, greetUser)

export default router;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Import and use routes in &lt;code&gt;app.js&lt;/code&gt; file :
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import authroute from "./routes/authroute.js"

app.use(authroute)

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

&lt;/div&gt;



&lt;p&gt;Your &lt;code&gt;app.js&lt;/code&gt; file should look like this :&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 connect from "./config/connect.js";
import authroute from "./routes/authroute.js"
const app = express()

app.use(
    express.urlencoded({
      extended: false,
    })
  );

app.use(express.json());

app.use(authroute)

connect.connectToLocalMongoDB()

export default app

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing Endpoints in Postman
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Testing Signup Enpoint
&lt;/h3&gt;

&lt;p&gt;Request :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SdqigWBI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667652861301/Ph1C6k90A.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SdqigWBI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667652861301/Ph1C6k90A.jpg" alt="signup req.jpg" width="880" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Response :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q24MpmFS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667652886169/JzJ44-wbj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q24MpmFS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667652886169/JzJ44-wbj.jpg" alt="signup res.jpg" width="880" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing Signin Enpoint
&lt;/h3&gt;

&lt;p&gt;Request :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vD8K3Y-j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667653085293/pHO8RrgQV.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vD8K3Y-j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667653085293/pHO8RrgQV.jpg" alt="signin req.jpg" width="880" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Response :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TrgBcbO9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667661452372/QdhH90aY1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TrgBcbO9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667661452372/QdhH90aY1.jpg" alt="signin res.jpg" width="880" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing greet-user enpoint
&lt;/h3&gt;

&lt;p&gt;Inserting JWT in Header :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6WsV-0fs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667653270863/z_lzHMAAf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6WsV-0fs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667653270863/z_lzHMAAf.jpg" alt="Insert header.jpg" width="880" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Request :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--39CMMDb_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667653388947/pg1Qanh7I.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--39CMMDb_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667653388947/pg1Qanh7I.jpg" alt="greet req.jpg" width="880" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Response :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DtryT4S1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667653410273/Tfp2E2oif.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DtryT4S1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667653410273/Tfp2E2oif.jpg" alt="greet res.jpg" width="880" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;So we've come this far and I guess you should have learnt how to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a JWT&lt;/li&gt;
&lt;li&gt;Use JWT in signup and signin enpoints&lt;/li&gt;
&lt;li&gt;Extract JWT from header and Decode it&lt;/li&gt;
&lt;li&gt;How to use object from JWT when user passes authentication&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away. Antoine de Saint-Exupery&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can learn more about JWT &lt;strong&gt;&lt;a href="https://jwt.io/"&gt;Here&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thanks for Reading.&lt;/p&gt;

&lt;p&gt;You can get the complete code &lt;strong&gt;&lt;a href="https://github.com/DevMukhtarr/fullauth"&gt;Here&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Follow me for more backend development tips.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
