How to authenticate Supertest requests with Passport?

Sometimes, we want to authenticate Supertest requests with Passport.

In this article, we’ll look at how to authenticate Supertest requests with Passport.

How to authenticate Supertest requests with Passport?

To authenticate Supertest requests with Passport, we can make a request to log in.

For instance, we write

const request = require('superagent');

const user1 = request.agent();
user1
  .post('http://localhost:4000/signin')
  .send({
    user: '[email protected]',
    password: 'password'
  })
  .end((err, res) => {
    // ...
  });

to call post with the login endpoint URL.

And we call send with a JSON object with the user and password parameters accepted by the login endpoint.

Finally, we call end with a callback to get the response from res.

Conclusion

To authenticate Supertest requests with Passport, we can make a request to log in.