ExpressJs — Authentication with JWT
Use JSON Web Tokens (JWT) for secure authentication in your Express app.
npm install jsonwebtoken
const jwt = require('jsonwebtoken');
app.post('/login', (req, res) => {
const token = jwt.sign({ userId: 1 }, 'secretKey', { expiresIn: '1h' });
res.json({ token });
});