javascript - 如何使用基于koa2的koa-jwt?
PHPz
PHPz 2017-04-11 13:16:31
0
4
1133

基于koa2的koa-jwt和基于koa1有很大差别,前者去掉了对sign,verify和decode函数的支持。我想问一下,我现在把jwt引入了:

const jwt = require('koa-jwt');
app.use(jwt({ 
  secret: 'my-secret' 
}).unless({path: [/^\/backend\/login/]}));

但是因为没有sign函数,我在/login接口返回的时候应该怎样把token加到返回的body中?

PHPz
PHPz

学习是最好的投资!

全員に返信(4)
左手右手慢动作

看了一下 koa-jwt@koa2 的文档,因为之前没用过,文档写得也不是很细致,看得不是很明白。大概这里有一句话,是说默认用 ctx.state.user,所以你试下呢

The JWT authentication middleware authenticates callers using a JWT token. If the token is valid, ctx.state.user (by default) will be set with the JSON object decoded to be used by later middleware for authorization and access control.

いいねを押す +0
迷茫

不知道你的意思是不是这样的:因为没有 sign 函数,所以不知道该怎么得出 token 。

如果你的问题确实是如上所述,那请你看看 koa2 下的 kao-jwt 文档:

jsonwebtoken — JSON Web Token signing and verification

Note that in the koa-v2 branch koa-jwt no longer exports the sign, verify and decode functions from the above module. Please use the module directly.

koa-jwt 的 sign 函数其实是从 jsonwebtoken 导出的,现在它不直接导出了,你要用的话需要直接去用 jsonwebtoken。

就是这样。

いいねを押す +0
巴扎黑

koa-jwt已经说明了自己使用的是jsonwebtoken,你把这个库引一下,自己做一下sign就好了,这样我觉得满好的

いいねを押す +0
Ty80

我也没有看懂他让如何来验证,坐等大神的 DEMO 。。

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!