How to authenticate Supertest requests with Passport?

后端 未结 8 1446
囚心锁ツ
囚心锁ツ 2020-11-28 21:22

I\'m using Passport.js for authentication (local strategy) and testing with Mocha and Supertest.

How can I create a session and make authenticated requests with Supe

8条回答
  •  生来不讨喜
    2020-11-28 22:03

    You should use superagent for that. It is lower level module and used by supertest. Take a look at the section Persisting an agent:

    var request = require('superagent');
    var user1 = request.agent();
    user1
      .post('http://localhost:4000/signin')
      .send({ user: 'hunter@hunterloftis.com', password: 'password' })
      .end(function(err, res) {
        // user1 will manage its own cookies
        // res.redirects contains an Array of redirects
      });
    

    Now you can use user1 to make authenticated requests.

提交回复
热议问题