skip to Main Content

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)?

2

Answers


  1. Chosen as BEST ANSWER

    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")
      }
    )
    

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search