Create a Simple and Secure Node Express App

Ash, I want to give you an update on the status of this blog post.

We have released a Beta of our new Express OIDC SDK, Release v2.0.0-beta.0 · auth0/express-openid-connect · GitHub.

We plan to migrate our guidance from using Passport in Express web apps to using this new Auth0 Express OIDC SDK. We believe that this new SDK will simplify the implementation of authentication in Express web apps, making it easier for you to secure the user experience.

If you’d like, my recommendation to you would be to give the new Express OIDC SDK a try and see if you still face any issues. If you do, please let us know in the Issues section of that repo, please, so that we can document and tackle them :pray:

Hi there

I can’t find the right place to post this so hopefully this will do… This tutorial is out of date:

Specifically,

  1. getConnectionString() is deprecated so we now use getUri()

  2. the latest version of MongoMemoryServer (v7) has a breaking change compared to the version used in the tutorial. The details can be found here.

But for brevity, the change required is as follows:

// this no longer works
const mongo = new MongoMemoryServer();
const mongoDBURL = await mongo.getConnectionString(); // ERROR: instance not started

// it is now
const mongo = await MongoMemoryServer.create();
const mongoDBURL = mongo.getUri();

Very nice tutorial. I ran into a small issue when following the tutorial as written but was able to easily find the solution.

The tutorial uses req.logOut() in the file auth.js when defining the “/logout” route.
It was giving me the following error: Error: req#logout requires a callback function

Apparently req.logOut() was updated and is no longer synchronous but requires a callback function.

The solution for me was to modify the router definition so that the code after req.logOut(); becomes the callback function:

router.get(“/logout”, (req, res) => {
req.logOut((err) => {

if (err) { return next(err); }

let returnTo = req.protocol + "://" + req.hostname;
const port = req.connection.localPort;

if (port !== undefined && port !== 80 && port !== 443) {
  returnTo =
    process.env.NODE_ENV === "production"
      ? `${returnTo}/`
      : `${returnTo}:${port}/`;
}

const logoutURL = new URL(
  `https://${process.env.AUTH0_DOMAIN}/v2/logout`
);

const searchString = querystring.stringify({
  client_id: process.env.AUTH0_CLIENT_ID,
  returnTo: returnTo
});
logoutURL.search = searchString;

res.redirect(logoutURL);

});
});

1 Like

Hello and welcome to the Auth0 Community!

Thank you for reading this blog post. I am glad that it has been useful to you.

Being frank, I have not looked at this blog post for some time. We are planning on releasing a new code sample and future guide that shows how to secure Express web app using our latest developer toolset.

The new guide will be similar to this blog post: The Complete Guide to Node.js User Authentication with Auth0

I recommend using the approach on that complete guide more than that one used in this blog post. The new express-openid-connect library simplifies greatly the authentication implementation in Express.js web apps. :zap:

Does your project require the usage of Passport.js?

The authentication solution of this current blog post uses Passport, which I no longer have direct experience with. However, Passport is still an important and widely used library. We can take a look at this code sample next week to determine what is the update path for it :slight_smile: How does that sound?

1 Like

Hello Dan , please i really want to better understand the following , how to secure a login portal for company using passpot.js with Auth0 ? i tried yours but i a little confused and nothing work , it seems that is too advanced . Please if you are more options i will be very happy to learn about that1 another thing i would like you to write like this:
step1:
step2:
step3:

something like that . i would like to learn more
Thanks in advance

KAB

1 Like

Hello! Welcome to the Auth0 Community. You don’t need to use Passport.js any longer! We have a simpler library that makes adding authentication to Express web apps much easier. Please follow this other guide: The Complete Guide to Node.js User Authentication with Auth0

Please let us know if you run into any issues with that guide. Thank you for your interest in securing your users with Auth0. :muscle:

hello I am running into an issue when I trying to log out I have followed all the steps in the blog in the proper order please let me know what I can do to fix my problem
This is what is happening when I am trying to log out:
Error: req#logout requires a callback function

1 Like

Hello and welcome to the Auth0 Community!

Do you need to use Passport.js? It would be better to use our latest library to add user authentication to Express.js web apps, please:

1 Like

Oh thats great i will do it without using passport then. Thank you for the help

1 Like

You are welcome. Happy to help! Let us know if you have any questions about our library :slight_smile: