Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
how to 'redirect' with react after login with passport.js?

Newbie react question: I’m using it with passport.js and express. I’m successfully logging in to the application. But I don't know how to do a redirect.

router.post('/login',
  passport.authenticate('local'),
  function(req, res) {
    // If this function gets called, authentication was successful.
    res.redirect('/upload'); //<—— what I’d like to do is some kind of redirect here
});

Any help appreciated.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Do that on your callback route.

router.get('/facebook', passport.authenticate('facebook', { scope: ['public_profile', 'email'] }));

router.get('/facebook/callback',
    passport.authenticate('facebook', { failureRedirect: "/" }), function (req, res) {
        if (req.user || req.session.user)
            return res.redirect('/' + req.user._id || req.session.user._id);
        return res.redirect('/login');
    });

Here, I have used passport.js for Facebook Login. and in /facebook/callback I have redirected to /login and in /login I have a function to check if the session is set.

if (Helper.isLoggedIn(req)) {
    res.redirect('/');
    return;
}

Definition of isLoggedIn inside Helper.js is

function isLoggedIn(req) {
    if (req.session && req.session.user_email && req.session.user_email != null) {
        if (!req.user || req.session.user == undefined || req.session.user == null) {
            loadUserFrom(req);
        }
        return true;
    }
    return false;
}
over 4 years ago · Santiago Trujillo Report

0

May be you can consider to do the redirect in react-js after the getting the response from server. Assume you use the redux-thunk.

action.js:

import axios from 'axios';
import { browserHistory } from 'react-router';

export function signinUser({ email, password }) {
  return function(dispatch) {
    axios.post(`${ROOT_URL}/signin`, { email, password })
      .then(response => { 
        // Successful login, redirect to /upload
        browserHistory.push('/upload');

      })
      .catch(() => {
        //handle the error response from server

      });
  }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!