Commit dd2525eb authored by Balasuriya D.A.M.'s avatar Balasuriya D.A.M.

JSON Web Token(JWT)

parent 372c0ddf
const jwt = require("jsonwebtoken");
//create generatetoken function
const generateToken = (id) => {
//add jwt secret from .env
return jwt.sign({ id }, process.env.JWT_SECRET, {
//can change the count of days as i want (secret days)
expiresIn: "30d",
});
};
module.exports = generateToken;
//JWT use for pass user data to backend through JWT
\ No newline at end of file
const asyncHandler = require("express-async-handler");
const User = require("../models/userModel");
const generateToken = require("../config/generateToken");
const registerUser = asyncHandler(async (req, res) => {
const { name, email, password, pic } = req.body;
......@@ -23,13 +24,14 @@ const registerUser = asyncHandler(async (req, res) => {
password,
pic,
});
//after register..also want send JWT token
if (user) {
res.status(201).json({
_id: user._id,
name: user.name,
email: user.email,
pic: user.pic,
token:generateToken(user._id),
});
} else {
res.status(400);
......
......@@ -12,7 +12,7 @@ dotenv.config();
connectDB();
const app = express();
app.use(express.json()); //to accept JSON data
app.get("/", (req, res) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment