<?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: kanishk soni</title>
    <description>The latest articles on DEV Community by kanishk soni (@kanishk1122).</description>
    <link>https://dev.to/kanishk1122</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%2F1470788%2F6efbc293-1f42-45d2-8700-50b0ee6621f8.jpeg</url>
      <title>DEV Community: kanishk soni</title>
      <link>https://dev.to/kanishk1122</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kanishk1122"/>
    <language>en</language>
    <item>
      <title>solve an error of node.js</title>
      <dc:creator>kanishk soni</dc:creator>
      <pubDate>Sun, 05 May 2024 19:01:48 +0000</pubDate>
      <link>https://dev.to/kanishk1122/solve-an-error-of-nodejs-4h0p</link>
      <guid>https://dev.to/kanishk1122/solve-an-error-of-nodejs-4h0p</guid>
      <description>&lt;p&gt;hi 👋 everyone my name is Kanishk and am new to this community. i face an error at noed.js &lt;br&gt;
this is my code&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 userModel from "./model/user.js";
import jwt from "jsonwebtoken";
import bcrypt from "bcrypt";
import cookieParser from "cookie-parser";
const app = express();

app.use(cors({ origin: true, credentials: true }));
app.use(express.json());
app.use(express.urlencoded({extended : true}));
app.use(cookieParser());


app.get("/", (req, res) =&amp;gt; {
  // res.cookie("passkey",token )
  res.send("Done");
});

app.post("/register", (req,res,next)=&amp;gt;{

  bcrypt.genSalt(10,(err,salt) =&amp;gt;{
      bcrypt.hash(req.body.password , salt , async (err,hash)=&amp;gt;{
        let token  = jwt.sign("req.body.email" , "secret");

              let createusers = await userModel.create({
                  username:req.body.username,
                  email:req.body.email,
                  password: hash,
                  passkey:token,

              })

              res.cookie("token",token);
              res.redirect("/");
              next();
      })
  })

});

app.post("/login", async (req, res) =&amp;gt; {
  try {
      const user = await usermodel.findOne({ email: req.body.email });
      if (!user) {
          console.log("User not found");
          return res.status(404).send("User not found");
      }

      // Compare the provided password with the hashed password from the database
      const passwordMatch = await bcrypt.compare(req.body.password, user.password);
      if (passwordMatch) {
          // Generate JWT token
          const token = jwt.sign({ email: user.email }, "secret");

          // Set the token in a cookie
          res.cookie("token", token); // Example cookie options, adjust as needed

          console.log("Login successful");
          return res.status(200).send("Login successful");
      } else {
          console.log("something went wrong");
          return res.status(401).send("something went wrong");
      }
  } catch (error) {
      console.error("Error:", error.message);
      return res.status(500).send("Internal Server Error");
  }
  res.redirect("/")
});

app.listen(5000, () =&amp;gt; {
  console.log("Your server is hosted on port 5000");
});

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

&lt;/div&gt;



&lt;p&gt;I want when a user registers a cookie is sent to him or her on its browser&lt;/p&gt;

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