<?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: Kamruzzaman</title>
    <description>The latest articles on DEV Community by Kamruzzaman (@kamruzzzaman).</description>
    <link>https://dev.to/kamruzzzaman</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%2F905368%2Fe0298439-697a-4b6e-8219-3e3ca9cf0b0f.jpg</url>
      <title>DEV Community: Kamruzzaman</title>
      <link>https://dev.to/kamruzzzaman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kamruzzzaman"/>
    <language>en</language>
    <item>
      <title>An authentication process with AWS Cognito and node, express Graphql</title>
      <dc:creator>Kamruzzaman</dc:creator>
      <pubDate>Sun, 25 Aug 2024 11:28:00 +0000</pubDate>
      <link>https://dev.to/kamruzzzaman/an-authentication-process-with-aws-cognito-and-node-express-graphql-1n0</link>
      <guid>https://dev.to/kamruzzzaman/an-authentication-process-with-aws-cognito-and-node-express-graphql-1n0</guid>
      <description>&lt;p&gt;To create an authentication process using AWS Cognito with a Node.js, Express, and GraphQL setup, you can follow these steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Set Up AWS Cognito User Pool&lt;/strong&gt;&lt;br&gt;
Create a User Pool:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. Go to the AWS Cognito dashboard and create a new User Pool.&lt;/li&gt;
&lt;li&gt;2. Configure sign-up and sign-in options (email, phone number, etc.).&lt;/li&gt;
&lt;li&gt;3. Set up security policies (password strength, MFA, etc.).&lt;/li&gt;
&lt;li&gt;4. Add app clients (for example, web or mobile applications).&lt;/li&gt;
&lt;li&gt;5. Save the pool ID and app client ID.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Install Necessary Packages&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;npm install express express-graphql graphql aws-sdk amazon-cognito-identity-js jsonwebtoken&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Set Up Express Server&lt;/strong&gt;&lt;br&gt;
Create a basic Express server with GraphQL integration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');

const app = express();

// GraphQL schema
const schema = buildSchema(`
  type Query {
    hello: String
  }

  type Mutation {
    signUp(email: String!, password: String!): String
    signIn(email: String!, password: String!): String
  }
`);

// Root resolver
const root = {
  hello: () =&amp;gt; 'Hello world!',
  signUp: async ({ email, password }) =&amp;gt; {
    // Sign up logic here
  },
  signIn: async ({ email, password }) =&amp;gt; {
    // Sign in logic here
  },
};

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

app.listen(4000, () =&amp;gt; console.log('Server running on http://localhost:4000/graphql'));

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Integrate AWS Cognito for Authentication&lt;/strong&gt;&lt;br&gt;
Set up the AWS Cognito logic for sign-up and sign-in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const AWS = require('aws-sdk');
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
const jwt = require('jsonwebtoken');

const poolData = {
  UserPoolId: 'your-user-pool-id', // e.g., us-east-1_ExaMPle
  ClientId: 'your-client-id', // e.g., 1h57kf5cpq17m0emlq5hd17dil
};
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

const signUp = async ({ email, password }) =&amp;gt; {
  return new Promise((resolve, reject) =&amp;gt; {
    const attributeList = [
      new AmazonCognitoIdentity.CognitoUserAttribute({
        Name: 'email',
        Value: email,
      }),
    ];

    userPool.signUp(email, password, attributeList, null, (err, result) =&amp;gt; {
      if (err) {
        reject(err);
        return;
      }
      resolve(`User ${result.user.getUsername()} signed up successfully`);
    });
  });
};

const signIn = async ({ email, password }) =&amp;gt; {
  return new Promise((resolve, reject) =&amp;gt; {
    const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({
      Username: email,
      Password: password,
    });

    const userData = {
      Username: email,
      Pool: userPool,
    };

    const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
      onSuccess: (result) =&amp;gt; {
        const accessToken = result.getAccessToken().getJwtToken();
        resolve(accessToken);
      },
      onFailure: (err) =&amp;gt; {
        reject(err);
      },
    });
  });
};

// Modify root resolver to use the signUp and signIn functions
const root = {
  hello: () =&amp;gt; 'Hello world!',
  signUp: async ({ email, password }) =&amp;gt; {
    try {
      return await signUp({ email, password });
    } catch (error) {
      throw new Error(error.message);
    }
  },
  signIn: async ({ email, password }) =&amp;gt; {
    try {
      return await signIn({ email, password });
    } catch (error) {
      throw new Error(error.message);
    }
  },
};

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Testing the Authentication Process&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sign Up:
&lt;code&gt;mutation {
signUp(email: "user@example.com", password: "StrongPassword123")
}
&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Sign In:
&lt;code&gt;mutation {
signIn(email: "user@example.com", password: "StrongPassword123")
}
&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6. Secure GraphQL Endpoints&lt;/strong&gt;&lt;br&gt;
Use JWT tokens obtained from Cognito to secure your GraphQL endpoints.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const authenticate = (req, res, next) =&amp;gt; {
  const token = req.headers.authorization;
  if (!token) {
    return res.status(401).send('Unauthorized');
  }

  jwt.verify(token, 'your-aws-cognito-jwt-verification-key', (err, decoded) =&amp;gt; {
    if (err) {
      return res.status(401).send('Unauthorized');
    }
    req.user = decoded;
    next();
  });
};

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

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7. Handling Token Expiry and Refresh&lt;/strong&gt;&lt;br&gt;
Implement token refresh logic to handle token expiry using Cognito's refresh token.&lt;/p&gt;

&lt;p&gt;This setup provides a basic framework for handling authentication using AWS Cognito in a Node.js, Express, and GraphQL environment. You can expand on this by adding more complex features such as MFA, user attributes, and more.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Open AI, Chat with files like(pdf, excel, CSV, doc, etc) With Node and React js.</title>
      <dc:creator>Kamruzzaman</dc:creator>
      <pubDate>Wed, 22 Nov 2023 18:18:01 +0000</pubDate>
      <link>https://dev.to/kamruzzzaman/open-ai-chat-with-files-likepdf-excel-csv-doc-etc-with-node-and-react-js-58jj</link>
      <guid>https://dev.to/kamruzzzaman/open-ai-chat-with-files-likepdf-excel-csv-doc-etc-with-node-and-react-js-58jj</guid>
      <description>&lt;p&gt;If you want to use Openai in your existing project it helps you to respond according to the files that you share. The below steps will help you.&lt;/p&gt;

&lt;p&gt;First, you need to set up your express server.&lt;br&gt;
Then do with this code,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Packages Needed&lt;/strong&gt; - express, dot env, cors, fs, openai, multer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const cors = require("cors");
const fs = require('fs')
const PORT = process.env.PORT || 5000;
const app = express();
const OpenAI = require('openai');
const multer = require('multer');


// User Middlewares
app.use(cors());
app.use(express.json());

app.get("/", (req, res) =&amp;gt; {
  res.send(
    "&amp;lt;h2 style='color:green;box-sizing:border-box; margin:0; background: #f3f3f9; height: 95vh;'&amp;gt;Server is Running!&amp;lt;h2&amp;gt;"
  );
});

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY, // defaults to process.env["OPENAI_API_KEY"]
});

const upload = multer({ dest: 'uploads/' }); // Set the destination folder for uploads

app.post('/upload', upload.single('file'), async (req, res) =&amp;gt; {
  const { file } = req;
  // Upload the file to OpenAI
  const { question } = req.body;
  try {
    const openaiFile = await openai.files.create({
      file: fs.createReadStream(file.path),
      purpose: 'assistants',
    });
    const assistant = await openai.beta.assistants.create({
      name: "you are a manual instructor",
      instructions: "based on file you will provide the relavant ans.",
      model: "gpt-3.5-turbo-1106",
      tools: [{ "type": "retrieval" }],
      file_ids: [openaiFile.id]
    });
    const thread = await openai.beta.threads.create();


    // Pass in the user question into the existing thread
    await openai.beta.threads.messages.create(thread.id, {
      role: "user",
      content: question,
    });

    // Use runs to wait for the assistant response and then retrieve it
    const run = await openai.beta.threads.runs.create(thread.id, {
      assistant_id: assistant.id,
    });

    let runStatus = await openai.beta.threads.runs.retrieve(
      thread.id,
      run.id
    );

    // Polling mechanism to see if runStatus is completed
    // This should be made more robust.
    while (runStatus.status !== "completed") {
      await new Promise((resolve) =&amp;gt; setTimeout(resolve, 2000));
      runStatus = await openai.beta.threads.runs.retrieve(thread.id, run.id);
    }

    // Get the last assistant message from the messages array
    const messages = await openai.beta.threads.messages.list(thread.id);

    // Find the last message for the current run
    const lastMessageForRun = messages.data
      .filter(
        (message) =&amp;gt; message.run_id === run.id &amp;amp;&amp;amp; message.role === "assistant"
      )
      .pop();

    // If an assistant message is found, console.log() it
    if (lastMessageForRun) {
      res.status(200).json({ success: true, data: `${lastMessageForRun.content[0].text.value} \n` });
    }


  } catch (error) {
    console.error('Error uploading to OpenAI:', error);
    res.status(500).json({ success: false, error: 'Internal Server Error' });
  } finally {
    // Delete the uploaded file from the server
    fs.unlinkSync(file.path);
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After setting up and running the server, connect the existing API with React JS.&lt;/p&gt;

&lt;p&gt;To design and run code successfully please set your environment with Tailwind css for design and Axios for API calls.&lt;/p&gt;

&lt;p&gt;The React js code example is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from "axios";
import { useRef, useState } from "react";

function App() {
  const [usedFiles, setUsedFiles] = useState([]);
  const [chatHistory, setChatHistory] = useState([]);
  const [selectedChat, setSelectedChat] = useState(
    chatHistory?.length &amp;gt; 0 &amp;amp;&amp;amp; chatHistory[0]
  );

  const fileInputRef = useRef(null);

  const handleButtonClick = () =&amp;gt; {
    if (fileInputRef.current) {
      fileInputRef.current.click();
    }
  };

  const handleCreateNewChat = () =&amp;gt; {
    if (usedFiles?.length &amp;gt; 0) {
      setChatHistory([
        ...chatHistory,
        {
          id: chatHistory?.length + 1,
          messeges: [],
        },
      ]);
      setSelectedChat({
        id: chatHistory?.length + 1,
        messeges: [],
      });
    } else {
      alert("Please Select a file");
    }
  };

  // ..................................................................... Restricted ...........................................//

  const [file, setFile] = useState(null);
  const [question, setQuestion] = useState("");
  const [loading, setLoading] = useState(false);
  const handleFilesChange = async (event) =&amp;gt; {
    setFile(event.target.files[0]);
    setUsedFiles([...usedFiles, event.target.files[0]]);
  };

  const handleQuestionChange = (event) =&amp;gt; {
    setQuestion(event.target.value);
  };

  const handleChatSubmit = async (e) =&amp;gt; {
    e.preventDefault();
    const formData = new FormData();
    formData.append("file", file);
    formData.append("question", question);
    const updateSelectedChat = {
      ...selectedChat,
      messeges: [
        ...selectedChat.messeges,
        {
          user: question,
          chatbot: "",
          loading: true,
        },
      ],
    };
    setSelectedChat(updateSelectedChat);

    const updatedChatHistory = chatHistory.map((chat) =&amp;gt; {
      if (chat.id === selectedChat.id) {
        return updateSelectedChat;
      } else {
        return chat;
      }
    });
    setChatHistory(updatedChatHistory);
    setQuestion("");
    try {
      const response = await axios.post(
        "http://localhost:5000/upload",
        formData
      );
      const data = response.data;
      if (data.success) {
        const updateSelectedChat = {
          ...selectedChat,
          messeges: [
            ...selectedChat.messeges,
            {
              user: question,
              chatbot: data.data,
              loading: false,
            },
          ],
        };
        setSelectedChat(updateSelectedChat);

        const updatedChatHistory = chatHistory.map((chat) =&amp;gt; {
          if (chat.id === selectedChat.id) {
            return updateSelectedChat;
          } else {
            return chat;
          }
        });
        setChatHistory(updatedChatHistory);
        setQuestion("");
        // setResponse(data.data);
      } else {
        console.error("Error generating response:", data.error);
      }
    } catch (error) {
      console.error("Error generating response:", error);
    }
  };

  return (
    &amp;lt;&amp;gt;
      &amp;lt;div className="flex h-screen antialiased text-gray-800"&amp;gt;
        &amp;lt;div className="flex flex-row h-full w-full overflow-x-hidden"&amp;gt;
          &amp;lt;div className="flex flex-col py-8 pl-6 pr-2 w-64 bg-gray-100 flex-shrink-0"&amp;gt;
            &amp;lt;div className="flex flex-row items-center justify-center h-12 w-full"&amp;gt;
              &amp;lt;div className="flex items-center justify-center rounded-2xl text-indigo-700 bg-indigo-100 h-10 w-10"&amp;gt;
                &amp;lt;svg
                  className="w-6 h-6"
                  fill="none"
                  stroke="currentColor"
                  viewBox="0 0 24 24"
                  xmlns="http://www.w3.org/2000/svg"
                &amp;gt;
                  &amp;lt;path
                    strokeLinecap="round"
                    strokeLinejoin="round"
                    strokeWidth="2"
                    d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z"
                  &amp;gt;&amp;lt;/path&amp;gt;
                &amp;lt;/svg&amp;gt;
              &amp;lt;/div&amp;gt;
              &amp;lt;div className="ml-2 font-bold text-2xl"&amp;gt;My Manuals&amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;input
              type="file"
              style={{ display: "none" }}
              ref={fileInputRef}
              onChange={handleFilesChange}
            /&amp;gt;
            &amp;lt;div
              onClick={handleButtonClick}
              className="flex border-b-2 border-black pb-3 mt-10 cursor-pointer"
            &amp;gt;
              &amp;lt;div&amp;gt;
                &amp;lt;svg
                  className="w-6 h-6 text-gray-800 "
                  aria-hidden="true"
                  xmlns="http://www.w3.org/2000/svg"
                  fill="none"
                  viewBox="0 0 16 16"
                &amp;gt;
                  &amp;lt;path
                    stroke="currentColor"
                    strokeLinecap="round"
                    strokeLinejoin="round"
                    strokeWidth="2"
                    d="M8 12V1m0 0L4 5m4-4 4 4m3 5v3a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2v-3"
                  /&amp;gt;
                &amp;lt;/svg&amp;gt;
              &amp;lt;/div&amp;gt;
              &amp;lt;div className="text-xl font-bold text-center ml-2"&amp;gt;
                New Upload
              &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div className="flex flex-col mt-8"&amp;gt;
              &amp;lt;div className="flex flex-row items-center justify-between text-xs"&amp;gt;
                &amp;lt;span className="font-bold"&amp;gt;Files&amp;lt;/span&amp;gt;
                &amp;lt;span className="flex items-center justify-center bg-gray-300 h-4 w-4 rounded-full"&amp;gt;
                  {usedFiles?.length}
                &amp;lt;/span&amp;gt;
              &amp;lt;/div&amp;gt;
              &amp;lt;div className="mt-3 h-full overflow-y-auto over"&amp;gt;
                {usedFiles?.map((res, i) =&amp;gt; (
                  &amp;lt;div key={i} className="flex items-center mt-2"&amp;gt;
                    {res?.type?.includes("pdf") ? (
                      &amp;lt;svg
                        className="w-6 h-6 text-gray-800 "
                        aria-hidden="true"
                        xmlns="http://www.w3.org/2000/svg"
                        fill="none"
                        viewBox="0 0 16 20"
                      &amp;gt;
                        &amp;lt;path
                          stroke="currentColor"
                          strokeLinecap="round"
                          strokeLinejoin="round"
                          strokeWidth="2"
                          d="M1 18a.969.969 0 0 0 .933 1h12.134A.97.97 0 0 0 15 18M1 7V5.828a2 2 0 0 1 .586-1.414l2.828-2.828A2 2 0 0 1 5.828 1h8.239A.97.97 0 0 1 15 2v5M6 1v4a1 1 0 0 1-1 1H1m0 9v-5h1.5a1.5 1.5 0 1 1 0 3H1m12 2v-5h2m-2 3h2m-8-3v5h1.375A1.626 1.626 0 0 0 10 13.375v-1.75A1.626 1.626 0 0 0 8.375 10H7Z"
                        /&amp;gt;
                      &amp;lt;/svg&amp;gt;
                    ) : (
                      &amp;lt;svg
                        className="w-6 h-6 text-gray-800 "
                        aria-hidden="true"
                        xmlns="http://www.w3.org/2000/svg"
                        fill="none"
                        viewBox="0 0 16 20"
                      &amp;gt;
                        &amp;lt;path
                          stroke="currentColor"
                          strokeLinecap="round"
                          strokeLinejoin="round"
                          strokeWidth="2"
                          d="M4.828 10h6.239m-6.239 4h6.239M6 1v4a1 1 0 0 1-1 1H1m14-4v16a.97.97 0 0 1-.933 1H1.933A.97.97 0 0 1 1 18V5.828a2 2 0 0 1 .586-1.414l2.828-2.828A2 2 0 0 1 5.828 1h8.239A.97.97 0 0 1 15 2Z"
                        /&amp;gt;
                      &amp;lt;/svg&amp;gt;
                    )}
                    &amp;lt;span className="ml-2"&amp;gt;{res?.name}&amp;lt;/span&amp;gt;
                  &amp;lt;/div&amp;gt;
                ))}
              &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
          &amp;lt;div className="flex flex-col py-8 pl-6 pr-2 w-64 bg-white flex-shrink-0"&amp;gt;
            &amp;lt;div className="flex flex-row items-center justify-center h-12 w-full"&amp;gt;
              &amp;lt;div
                onClick={handleCreateNewChat}
                className="font-bold text-xl border-2 px-7 py-1 rounded cursor-pointer"
              &amp;gt;
                Start New Chat
              &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div className="flex flex-col mt-8"&amp;gt;
              &amp;lt;div className="flex flex-row items-center justify-between text-xs"&amp;gt;
                &amp;lt;span className="font-bold"&amp;gt;History&amp;lt;/span&amp;gt;
                &amp;lt;span className="flex items-center justify-center bg-gray-300 h-4 w-4 rounded-full"&amp;gt;
                  {chatHistory?.length}
                &amp;lt;/span&amp;gt;
              &amp;lt;/div&amp;gt;
              &amp;lt;div className="flex flex-col space-y-1 mt-4 -mx-2 h-full overflow-y-auto"&amp;gt;
                {chatHistory?.map((res, i) =&amp;gt; (
                  &amp;lt;&amp;gt;
                    {res?.messeges?.length &amp;gt; 0 ? (
                      &amp;lt;button
                        key={i}
                        className={`flex flex-row items-center ${
                          res?.id == selectedChat.id ? "bg-gray-50" : ""
                        } hover:bg-gray-100 rounded-xl p-2`}
                        onClick={() =&amp;gt; setSelectedChat(res)}
                      &amp;gt;
                        &amp;lt;div className="text-sm font-semibold"&amp;gt;
                          {res?.messeges[0]?.user == undefined
                            ? "New Messege"
                            : res?.messeges[0]?.user}
                        &amp;lt;/div&amp;gt;
                      &amp;lt;/button&amp;gt;
                    ) : (
                      &amp;lt;button
                        key={i}
                        className={`flex flex-row items-center ${
                          res?.id == selectedChat.id ? "bg-gray-50" : ""
                        } hover:bg-gray-100 rounded-xl p-2`}
                        onClick={() =&amp;gt; setSelectedChat(res)}
                      &amp;gt;
                        &amp;lt;div className="text-sm font-semibold"&amp;gt;New Messege&amp;lt;/div&amp;gt;
                      &amp;lt;/button&amp;gt;
                    )}
                  &amp;lt;/&amp;gt;
                ))}
              &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
          &amp;lt;div className="flex flex-col flex-auto h-full p-6"&amp;gt;
            &amp;lt;div className="flex flex-col flex-auto flex-shrink-0 rounded-2xl bg-gray-100 h-full p-4"&amp;gt;
              &amp;lt;div className="flex flex-col h-full overflow-x-auto mb-4"&amp;gt;
                &amp;lt;div className="flex flex-col h-full"&amp;gt;
                  &amp;lt;div className="grid grid-cols-12 gap-y-2"&amp;gt;
                    {selectedChat?.messeges?.map((res, i) =&amp;gt; (
                      &amp;lt;&amp;gt;
                        {res?.user !== undefined &amp;amp;&amp;amp; (
                          &amp;lt;div
                            key={i}
                            className="col-start-6 col-end-13 p-3 rounded-lg"
                          &amp;gt;
                            &amp;lt;div className="flex items-center justify-start flex-row-reverse"&amp;gt;
                              &amp;lt;div className="flex items-center text-white justify-center h-10 w-10 rounded-full bg-indigo-500 flex-shrink-0"&amp;gt;
                                Me
                              &amp;lt;/div&amp;gt;
                              &amp;lt;div className="relative mr-3 text-sm bg-indigo-100 py-2 px-4 shadow rounded-xl"&amp;gt;
                                &amp;lt;div&amp;gt;{res?.user}&amp;lt;/div&amp;gt;
                              &amp;lt;/div&amp;gt;
                            &amp;lt;/div&amp;gt;
                          &amp;lt;/div&amp;gt;
                        )}

                        {res?.chatbot !== undefined &amp;amp;&amp;amp; (
                          &amp;lt;div className="col-start-1 col-end-8 p-3 rounded-lg"&amp;gt;
                            &amp;lt;div className="flex flex-row items-center"&amp;gt;
                              &amp;lt;div className="flex items-center text-white justify-center h-10 w-10 rounded-full bg-indigo-500 flex-shrink-0"&amp;gt;
                                Bot
                              &amp;lt;/div&amp;gt;
                              &amp;lt;div className="relative ml-3 text-sm bg-white py-2 px-4 shadow rounded-xl"&amp;gt;
                                &amp;lt;div&amp;gt;
                                  {res?.loading
                                    ? "..."
                                    : res?.chatbot.replace(
                                        /&amp;amp;#8203;``【oaicite:1】``&amp;amp;#8203;/g,
                                        ""
                                      )}
                                &amp;lt;/div&amp;gt;
                              &amp;lt;/div&amp;gt;
                            &amp;lt;/div&amp;gt;
                          &amp;lt;/div&amp;gt;
                        )}
                      &amp;lt;/&amp;gt;
                    ))}
                  &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
              &amp;lt;/div&amp;gt;
              {
                &amp;lt;form onSubmit={handleChatSubmit}&amp;gt;
                  &amp;lt;div className="flex flex-row items-center h-16 rounded-xl bg-white w-full px-4"&amp;gt;
                    &amp;lt;div className="flex-grow"&amp;gt;
                      &amp;lt;div className="relative w-full"&amp;gt;
                        &amp;lt;input
                          type="text"
                          value={question}
                          disabled={chatHistory?.length === 0}
                          onChange={handleQuestionChange}
                          className="flex w-full border rounded-xl focus:outline-none focus:border-indigo-300 pl-4 h-10"
                        /&amp;gt;
                      &amp;lt;/div&amp;gt;
                    &amp;lt;/div&amp;gt;
                    &amp;lt;div className="ml-4"&amp;gt;
                      &amp;lt;button
                        type="submit"
                        className={`flex items-center justify-center  rounded-xl text-white px-4 py-1 flex-shrink-0 bg-indigo-500 hover:bg-indigo-600 ${
                          chatHistory?.length === 0
                            ? "opacity-50"
                            : "opacity-100"
                        }`}
                        disabled={chatHistory?.length === 0}
                      &amp;gt;
                        &amp;lt;span&amp;gt;Send&amp;lt;/span&amp;gt;
                        &amp;lt;span className="ml-2"&amp;gt;
                          &amp;lt;svg
                            className="w-4 h-4 transform rotate-45 -mt-px"
                            fill="none"
                            stroke="currentColor"
                            viewBox="0 0 24 24"
                            xmlns="http://www.w3.org/2000/svg"
                          &amp;gt;
                            &amp;lt;path
                              strokeLinecap="round"
                              strokeLinejoin="round"
                              strokeWidth="2"
                              d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"
                            &amp;gt;&amp;lt;/path&amp;gt;
                          &amp;lt;/svg&amp;gt;
                        &amp;lt;/span&amp;gt;
                      &amp;lt;/button&amp;gt;
                    &amp;lt;/div&amp;gt;
                  &amp;lt;/div&amp;gt;
                &amp;lt;/form&amp;gt;
              }
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is React full code you can use for your existing project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hope it will help you. Thanks.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>fileupload</category>
      <category>openai</category>
      <category>nodeopenai</category>
      <category>reactopenai</category>
    </item>
    <item>
      <title>React-pdf send pdf to server without download pdf.</title>
      <dc:creator>Kamruzzaman</dc:creator>
      <pubDate>Tue, 17 Oct 2023 06:56:06 +0000</pubDate>
      <link>https://dev.to/kamruzzzaman/react-pdf-send-pdf-to-server-without-download-pdf-1k36</link>
      <guid>https://dev.to/kamruzzzaman/react-pdf-send-pdf-to-server-without-download-pdf-1k36</guid>
      <description>&lt;p&gt;So we all are know that why we use react-pdf. If no im explaing basics.&lt;br&gt;
Basically react-pdf is used for make pdf from front-end. Like you making a website that need to download the pdf. React-pdf is handling that so much easy way. They have Prefined component which we can use and design the pdf. If you need More details explanation please &lt;a href="https://react-pdf.org/"&gt;go here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So how react-pdf package acctually work?&lt;/strong&gt;&lt;br&gt;
In React-PDF, when you generate a PDF document, it's typically stored as a Blob (Binary Large Object) in memory. You can then use this Blob to display the PDF in your application or save it to the user's device.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So now the question is why i can't send the file directly?&lt;/strong&gt;&lt;br&gt;
Beacause the file is not store anywhere in your device if you don't download. And the process is not good like first you need to download and then send it to the server.The invoice pdf that you designed if you need to send that one email. That means you need to connect the email server and need to send the file. So it's not easy to upload or post file directly.&lt;/p&gt;

&lt;p&gt;So the solution is,&lt;/p&gt;

&lt;p&gt;if you go through the &lt;a href="https://react-pdf.org/advanced"&gt;react-pdf&lt;/a&gt; document you can see &lt;/p&gt;

&lt;p&gt;they have a hook call usePDF()&lt;br&gt;
and its by default receive the document that you have designed.&lt;br&gt;
For example I designed the document:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const MyDoc = (&lt;br&gt;
  &amp;lt;Document&amp;gt;&lt;br&gt;
    &amp;lt;Page&amp;gt;&lt;br&gt;
      // My document data&lt;br&gt;
    &amp;lt;/Page&amp;gt;&lt;br&gt;
  &amp;lt;/Document&amp;gt;&lt;br&gt;
);&lt;/code&gt;&lt;br&gt;
Now i can call that with usePDF like&lt;br&gt;
&lt;code&gt;const [instance, updateInstance] = usePDF({ document: MyDoc });&lt;/code&gt;&lt;br&gt;
So here the instace give us an object. and there we have the blob url. Now we need to convert this blob url to bas64 data. For that we can easily send it to the server.&lt;/p&gt;

&lt;p&gt;The Full Code example is:&lt;br&gt;
&lt;code&gt;import { usePDF, Document, Page } from '@react-pdf/renderer';&lt;br&gt;
const MyDoc = (&lt;br&gt;
  &amp;lt;Document&amp;gt;&lt;br&gt;
    &amp;lt;Page&amp;gt;&lt;br&gt;
      // My document data&lt;br&gt;
    &amp;lt;/Page&amp;gt;&lt;br&gt;
  &amp;lt;/Document&amp;gt;&lt;br&gt;
);&lt;/code&gt;&lt;br&gt;
&lt;code&gt;const App = () =&amp;gt; {&lt;br&gt;
  const [pdfBase64, setPdfBase64] = useState()&lt;br&gt;
  const [instance, updateInstance] = usePDF({ document: MyDoc });&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// get pdf file invoice&lt;br&gt;
  function blobToBase64(blobURL) {&lt;br&gt;
    return new Promise((resolve, reject) =&amp;gt; {&lt;br&gt;
      const xhr = new XMLHttpRequest();&lt;br&gt;
      xhr.responseType = 'blob';&lt;br&gt;
      xhr.onload = () =&amp;gt; {&lt;/code&gt;&lt;br&gt;
        &lt;code&gt;const reader = new FileReader();&lt;br&gt;
        reader.onloadend = () =&amp;gt; {&lt;br&gt;
          if (reader.result) {&lt;br&gt;
            resolve(reader.result.split(',')[1]);&lt;br&gt;
          } else {&lt;br&gt;
            reject("Error reading the blob.");&lt;br&gt;
          }&lt;br&gt;
        };&lt;br&gt;
        reader.readAsDataURL(xhr.response);&lt;br&gt;
      };&lt;/code&gt;&lt;br&gt;
      &lt;code&gt;xhr.onerror = reject;&lt;br&gt;
      xhr.open('GET', blobURL);&lt;br&gt;
      xhr.send();&lt;br&gt;
    });&lt;br&gt;
  }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// Usage&lt;br&gt;
  const blobURL = instance?.url;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useEffect(() =&amp;gt; {&lt;br&gt;
    if (blobURL) {&lt;br&gt;
      blobToBase64(blobURL)&lt;br&gt;
        .then(base64String =&amp;gt; {&lt;br&gt;
          setPdfBase64(base64String);&lt;br&gt;
        })&lt;br&gt;
        .catch(error =&amp;gt; {&lt;br&gt;
          console.error(error);&lt;br&gt;
        });&lt;br&gt;
    }&lt;br&gt;
  }, [blobURL])&lt;/code&gt;&lt;br&gt;
&lt;code&gt;function onSubmit(data) {&lt;br&gt;
         axios.post(${server_url_here},pdfBase64)        &lt;br&gt;
.then(res =&amp;gt; {&lt;br&gt;
          console.log(res);&lt;br&gt;
        })&lt;br&gt;
  }&lt;br&gt;
  return (&lt;br&gt;
     &amp;lt;button onClick={onSubmit}&amp;gt;&lt;br&gt;
                Cancel&lt;br&gt;
              &amp;lt;/button&amp;gt;&lt;br&gt;
  );&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So if you open your network tab and see the result. You send the pdf data with base64.&lt;/p&gt;

&lt;p&gt;Thanks for getting into touch.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactpdf</category>
      <category>sendpdftoserver</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
