Home > OS >  How to redirect to React/Vue route after user authorize via oauth2 twitter or discord through passpo
How to redirect to React/Vue route after user authorize via oauth2 twitter or discord through passpo

Time:07-31

So I want to make authentication so that a user can authenticate by twitter and discord. So I created developer accounts in twitter and discord developer portal. then I made passport strategy for both socials. For reference I am providing my strategy credentials in the following(for twitter):

{
    consumerKey: process.env.TWITTER_CONSUMER_KEY,
    consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
    callbackURL: "http://localhost:9000/api/auth/twitter/callback",
    includeEmail: true,
}

the discord one is similar to twitter.

Routes for twitter are:

router.get("/auth/discord", passport.authenticate("discord"))
router.get(
  "/auth/discord/redirect",
  passport.authenticate("discord"),
  (req, res) => {
    res.json({
      success: true,
      user: req.user,
    })
  }
)

Now my question is after a user authorizes, how can I redirect the user to SPA route (React/Vue)?

CodePudding user response:

As I can see in the passport.js documentation, there are two parameters available when you call passport.authenticate function. These two are: successRedirect and failureRedirect.

The first will redirect the user to the sigin page. The second will process the authentication result when the user is redirected back.

Check this out ;) passportjs.org/tutorials/auth0/routes

CodePudding user response:

That was very simple indeed. I just needed to add redirect to my frontend url

router.get("/auth/twitter", passport.authenticate("twitter"))
router.get(
  "/auth/twitter/callback",
  passport.authenticate("twitter", {
    failureRedirect: "http://localhost:3000/login",
  }),
  (req, res) => {
    res.redirect("http://localhost:3000")
  }
)
  • Related