Categories
JavaScript Answers

How to authenticate Supertest requests with Passport?

Spread the love

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: 'abc@example.com',
    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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *