// install dependency jwt using command npm install jsonwebtoken at your folder terminal
// import it in your app.js
// Use this code in file in which you are saving user to database or you are receiving result of successfully registered user . in my case am getting result from Indexcontroller (You can make your own controller to save data)
=====================================================================
//API to register new User with jWT Token
import jwt from 'jsonwebtoken';
router.post("/register",(req,res)=>{
IndexController.registerUser(req.body).then((result)=>{
let jwttoken;
jwttoken = jwt.sign({ _id: result._id, email: result.email },"8Zzvasu5tw0Ionm3XPZZfN0NOml3z9FMfmpgXwovR9fp6ryDIoGRM8EPHAB6iHsc0fb",{ expiresIn: "1h" });
res.status(201).json({success: true,data: { _id: result._id, email: result.email, jwttoken:jwttoken }});
}).catch((err)=>{
return res.status(401).send(err);
});
});
Now open Postman & Test This API -
your if your API test is success your result will be look like following-
Comments
Post a Comment