“JWT” Respostas de código

JWT

require('dotenv').config();
const jwt = require('jsonwebtoken');

function verifyJWT(req, res, next) {
    const authHeader = req.headers.authorization;
    if (!authHeader) {
        return res.status(401).send({ message: 'unauthorized access' });
    }
    const token = authHeader.split(' ')[1];
    jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, decoded) => {
        if (err) {
            return res.status(403).send({ message: 'Forbidden access' });
        }
        console.log('decoded', decoded);
        req.decoded = decoded;
        next();
    })
}


// this is inside run()
app.post('/login', async (req, res) => {
            const user = req.body;
            const accessToken = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, {
                expiresIn: '1d'
            });
            res.send({ accessToken });
})
SHAH SOLAYMAN SINHA

JWT

jwt.sign(payload, secret, { expiresIn: expiry })

-------JWT Token Service-----------
import { JWT_SECRET } from "../config"
import jwt from "jsonwebtoken"
class jwtService {
   static sign(payload, expiry = "60s", secret = JWT_SECRET) {
        return jwt.sign(payload, secret, { expiresIn: expiry })
    }

    static verify(payload, secret = JWT_SECRET) {
        return jwt.verify(payload, secret)
    }
}

export default jwtService
Rasel Hossain

JWT

//initial
header(metadata)
payload(data)
signature( oneWayEncryption( header + payload + secretkey ) )

//output
base64(header + payload + signature);
Ganz404

Respostas semelhantes a “JWT”

Perguntas semelhantes a “JWT”

Procure respostas de código populares por idioma

Procurar outros idiomas de código