how to install jsonwebtoken in next js
- Install the
jsonwebtoken
library using npm:
npm install jsonwebtoken
- Import
jsonwebtoken
in your Next.js file where you intend to use it:
import jwt from 'jsonwebtoken';
- Use
jsonwebtoken
in your code to sign and verify tokens:
// Example: Creating a token
const payload = { user_id: 123 };
const secretKey = 'your_secret_key';
const token = jwt.sign(payload, secretKey);
// Example: Verifying a token
const decoded = jwt.verify(token, secretKey);
Note: Replace 'your_secret_key'
with your actual secret key. It is crucial to keep your secret key secure and never expose it in your client-side code.
Now you have successfully installed and integrated the jsonwebtoken
library into your Next.js project.