Auth0 redirect after login issue

Hi all,

I am trying to set up a node.js server with express for the backend authentication of my React App. On the front end I have an <a> routing users to http://localhost:3001/login. My /login route runs fine and the user can login.

router.get('/login', passport.authenticate('auth0', {
	scope: 'openid email profile'
}), (req, res) => {
	res.redirect('/');
});

After login the user is routed to http://localhost:3000/callback?code=OXVrD51HyqKA&state=g6Fo2SBJM2RGNjc1FWdU8#

or some similar url with different query parameters. I don’t have a specified front-end route with “/callback” and I am assuming I don’t need one. It seems like my back-end “/callback” route should be running and rerouting me again but it is not.

router.get('/callback', function (req, res, next) {
	console.log('called')
	passport.authenticate('auth0', function (err, user, info) {
		if (err) {
			return next(err);
		}
		if (!user) {
			return res.redirect('/login');
		}
		req.logIn(user, function (err) {
			if (err) {
				return next(err);
			}
			const returnTo = req.session.returnTo;
			delete req.session.returnTo;
			res.redirect(returnTo || '/user');
		});
	})(req, res, next);
});

Hi @trevordammon,

Is the problem that the callback is on a different port? 3000 v 3001?

That looks like it fixes the initial problem. However now after the callback function runs it is routing me to localhost:3001/users not localhost:3000/users.

I will see if I can play around with this and find a solution.

Thanks Dan!

1 Like

Let me know if you figure it out!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.