猿问

nodejs使用JWT进行token验证时,出现 UnauthorizedError,是什么原因?

``


if(token){

    try{

        let decoded = jwt.verify(token, config.jwt.secret);

        //console.log(decoded)


        let deadline = new Date()/1000;

        if(decoded.exp <= deadline){

            console.log('expired token');

            ctx.redirect('/views/admin/login.html'); //token过期,则跳转到登陆页面

        }else{

            console.log('鉴权成功!');

            await next();

        }

    }catch(err){

        ctx.throw(401, 'expired token');      //token验证失败

    }

}else{

    ctx.redirect('/views/admin/login.html');  //缺少token,则跳转到登陆页面

}

``

上面代码,是如果存在token,则进行验证。我在验证时,可以获取token,也能正常解析,可是抛出UnauthorizedError错误,是为什么呢?

慕田峪7331174
浏览 3749回答 1
1回答

POPMUISE

else{&nbsp; &nbsp; &nbsp; &nbsp; console.log('鉴权成功!');&nbsp; &nbsp; &nbsp; &nbsp; // 下面 这个中间是什么,应该是 next 报的错。&nbsp; &nbsp; &nbsp; &nbsp; await next();}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答