首页 > 解决方案 > 当我以角度删除对象时,令牌在nodejs中无效

问题描述

当我从前端删除一个对象时,我得到一个 httperrorresponse 状态 404:错误“el token no es valido”

nodejs中的中间件jwt

'use strict'

var jwt = require('jwt-simple');
var moment = require('moment');
var secret = "klave";

exports.authenticated = function(req, res, next) {

    // Comprobar si llega autorización
    if (!req.headers.authorization) {
        return res.status(403).send({
            message: 'La petición no tiene la cabecera de authorization'
        });
    }

    // Limpiar el token y quitar comillas
    var token = req.headers.authorization.replace(/['"]+/g, '');
    console.log(token); //line 18


    try {
        // Decodificar token
        var payload = jwt.decode(token, secret);
        // Adjuntar usuario identificado a request
        // req.user = payload;

        // Pasar a la acción
        //   next();

        // Comprobar si el token ha expirado
        if (payload.exp <= moment().unix()) {
            return res.status(404).send({
                message: 'El token ha expirado'
            });
        }

    } catch (ex) {
        return res.status(404).send({
            message: 'El token no es válido'
        });
    }

    // Adjuntar usuario identificado a request
    req.user = payload;

    // Pasar a la acción
    next();

};

在 console.log(token) 第 18 行我得到{token:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1ZjAyNWMxOGU0Y2IyMDIxYTgwNTVmZjYiLCJuYW1lIjoibWFydGEiLCJzdXJuYW1lIjoibWFydGEiLCJlbWFpbCI6Im1hcnRhQG1hcnRhLmNvbSIsInJvbGUiOiJST0xFX0FETUlOIiwiaW1hZ2UiOm51bGwsImlhdCI6MTYwMjg1MDQwM30.89vDC_PaQG733gL6aW1kV1t5HaDfPfJD-0DiyDvSUdw}

没有“”但我必须从令牌中退出 { token: 只有值 eyj0EXaIoIj.......????

标签: node.jsjwttoken

解决方案


推荐阅读