W
W
Wasya UK2020-04-08 01:24:54
JSON Web Token
Wasya UK, 2020-04-08 01:24:54

Why am I not receiving payload?

It seems that I did everything as in the instructions, but I can not log in in any way. I'm trying to enter payload to see why it doesn't find the user, but I only get results from passport.authenticate('jwt') and nothing from the strategy itself. I get a normal token. Then I pass it through postman, but I get an error: "user not found". Passed both through the body in the auth_token option and through the Headers in the Authorization . The user was found, the token was normally created, checked. What am I doing wrong?

Passport.use(new JwtStrategy(jwtOptions, function (payload, done) {
  console.log("payload:", payload) // даже не срабатывает
    User.findById(payload.id, (err, user) => {
      if (err) {
        return done(err)
      }
      if (user) {
        done(null, user)
      } else {
        done(null, false)
      }
    })
  })
);

router.get('/ss', async (ctx, next) => {
  await passport.authenticate('jwt', { session: false }, function (err, user) {
    if (user) {
      ctx.body = "hello " + user.email;
    } else {
      ctx.body = "No such user";
      console.log("err", err) // получаю вывод только здесь
    }
  } )(ctx, next);
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wasya UK, 2020-04-08
@dmc1989

The problem was that I did not specify what to check when trying to authorize:

const jwtOptions = {
  jwtFromRequest: ExtractJwt.fromHeader('authorization')
};

V
Vladimir, 2020-04-08
@HistoryART

I don’t know about you, for me it takes 2 strictly marked parameters with standard authorization email, password, you can rename it if necessary, you need to transfer exactly email / password from req.body

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question