Authenticating node API with passport-jwt

前端 未结 2 1528
鱼传尺愫
鱼传尺愫 2020-12-09 18:34

I\'m trying to setup JWT authentication using passport-jwt. I think I\'ve taken the right steps, but a test GET won\'t succeed and I don\'t know how to debug it.

He

2条回答
  •  心在旅途
    2020-12-09 18:47

    For any poor soul that follows me here: the passport-jwt doc implies that the auth header should look like this...

    Authorization: JWT JSON_WEB_TOKEN_STRING.....

    That turned out to be misleading (for me, anyway).

    Fortunately, thanks to this article I was able to learn how the token is built. (The token's prefix up to the first '.' is the base64 encoding of the scheme. That "JWT " at the front was noise that prevented the validation from working.

    So the fix was to change the token returned by the user controller from:

        res.send({ user: user, jwtToken: "JWT " + token });
    

    To the simpler:

        res.send({ user: user, jwtToken: token });
    

    Phew. Is it me, or is it really a bummer how inadequately these things are explained in so many node package docs??

提交回复
热议问题