Hello! So I managed to authenticate our users with auth0.js using webAuth0.login() method. But how can I see if users are authenticated on our NodeJS application? Unfortunately, there is no clear workflow on your documentations.
Client-side:
function setConfig() {
webAuth = new auth0.WebAuth({
domain: 'login.foo.com',
redirectUri: 'https://foo.com/login/callback',
clientID: 'XYZ',
responseType: 'token'
});
}
setConfig();
$('#login').click(function(e) {
e.preventDefault();
webAuth.login(
{
realm: 'Username-Password-Authentication',
email: $('#email').val(),
password: $('#password').val()
},
function(err, data) {
this.webAuth.userInfo(
data.accessToken
);
}
);
});
Server-side:
Middleware secured():
module.exports = function () {
return function secured (req, res, next) {
if (req.user) { return next(); }
req.session.returnTo = req.originalUrl;
res.redirect('/auth0/login');
};
};
Callback route:
router.get('/login/callback', function (req, res, next) {
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('/home');
});
})(req, res, next);
});