[Express + Auth0] I want to redirect to another backend endpoint after the callback

Hi,
I’m building an application with:

  • Frontend: React + Vite (localhost:5173)
  • Backend: Express.js (localhost:3000)
  • Auth0: Regular web app, using the dependancy “express-openid-connect”

My Auth0 configuration in the backend is:

const config = {
  authRequired: false,
  auth0Logout: true,
  secret: process.env.AUTH0_SECRET,
  baseURL: 'http://localhost:3000',
  clientID: process.env.AUTH0_CLIENT_ID,
  issuerBaseURL: process.env.AUTH0_DOMAIN,
}

app.use(auth(config));

Currently, after login, Auth0 sends me to the callback (/callback) and then automatically redirects to the baseURL (http://localhost:3000/).
What I want is instead of sending me to /, to redirect me to another endpoint in my backend.

http://localhost:3000/auth/session

so I can create the user in my mongodb, generate access token and refresh token.

How can I make Auth0 automatically redirect to http://localhost:3000/auth/session after the callback instead of going to the baseURL?

Hi @Isaacbfuents

Welcome to the Auth0 Community!

I believe that what you’re currently missing in order to achieve your desired flow would be the afterCallback hook, which is provided by the express-openid-connect library ( please see the following documentation ).

You should be able to define it within your existing code similarly to this:

const config = {
  authRequired: false,
  auth0Logout: true,
  secret: process.env.AUTH0_SECRET,
  baseURL: 'http://localhost:3000',
  clientID: process.env.AUTH0_CLIENT_ID,
  issuerBaseURL: process.env.AUTH0_DOMAIN,
  routes:
  auth({
    afterCallback: (req, res, session) => {
      const claims = jose.JWT.decode(session.id_token); // using jose library to decode JWT
      if (claims.org_id !== 'Required Organization') {
        throw new Error('User is not a part of the Required Organization');
      }
      return session;
    },
  })

}

app.use(auth(config));

Additional resources on the afterCallback hook :

Let us know if this helped solve your issue!

Best regards,
Gerald