Step 14
Import the verifyToken
function to the auth.js
:
- const { createToken } = require("../util/token");
+ const { createToken, verifyToken } = require("../util/token");
Next, add a new route handler for auth.js
:
router.post("/verify", async (req, res) => {
const { token } = req.body;
const isValid = await verifyToken(token);
if (!isValid) {
return res.status(403).json({
message: "Invalid or expired token!",
});
}
return res.json({
message: "Token verified, and it is valid!",
token: token,
});
});
Save the file and try to verify an authenticated user!
Moreover, try an invalid token!