Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
How should I redirect after authenticating a user using passportjs

I looked in several posts, but I cannot find something that meets my situation.

To login with google on my website (or signup) with google, you have to go to mydomain.com/login/google

You then, well, login, and then the callback is handled on mydomain.com/auth/google

Here is the code responsible for this.

app.get('/login/google', passport.authenticate('google'));
app.get('/auth/google',
  passport.authenticate('google', { failureRedirect: '/login', failureMessage: false, session: false, failureFlash: true }),
  function(req, res) {
    res.redirect('/');
  });

Here is where I store the users:

passport.use(new GoogleStrategy({
    clientID: no,
    clientSecret: definitely not,
    callbackURL: 'https://no cuz privacy/auth/google'
  },
  async function(issuer, profile, cb) {

    var user = await User.find({ google: true, googleid: profile.id })

      if (!user[0]) {
        // The Google account has not logged in to this app before.  Create a
        // new user record and link it to the Google account.
        
        const newUser = await new User({ username: generateUsername(), google: true, googleid: profile.id, googleProfile: profile })
        await newUser.save()

            return cb(null, newUser);
      } else {
        // The Google account has previously logged in to the app.  Get the
        // user record linked to the Google account and log the user in.
        console.log('exists')
        return cb(null, newUser);
      }
  }
));

I think you have to do something with the callback function (cb()) to somehow go to app.get('/auth/google') for the redirect, but, all it does is print either exists or new in the console, spinning forever on the client side. Not sure how to redirect after the code determines either account exists or new account.

Thanks in advance!

P.S. I just want to point out that the cb() function could also be done() too. For example:

function(accessToken, refreshToken, profile, done){
   console.log("strategy");
   console.log(profile);
   console.log(accessToken);
   console.log(refreshToken);

   done(null, profile);
 }

^^ Not my code --> PassportJS in Nodejs never call the callback function ^^

Please tell me if I did anything wrong!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda