Express, redirecting to /profile url after login, I'm completely stuck!

Hi everybody,

I’m new to auth0 so I figured I would start with the express quickstart guide to get started, and that’s working fine, I understand what it’s doing (or at least I think I do), and I’ve got my app running on localhost:3000 with and it works as expected.

The problem I’m having is trying to extend it further. What I’m attempting to do is to have it so that once a user finishes logging in they are redirected to localhost:3000/profile

Where I’m at right now is, I’ve got the “express-openid-connect” set up for the login page and the “express-oauth2-jwt-bearer” set up for JWT handling with the API.

My current code can be viewed on github here.

I’ve been googling around for a while and have tried a few different things, none of which seem to work, although this is probably more about me doing it wrong than the methods not working!

I’ve tried:

Using an Auth0 action

Adding in a post instruction for “/callback” because I can see after a login it makes a post to “/callback” and then a get to “/”

app.post('/callback', express.urlencoded({ extended: false }), (req, res) =>
  res.oidc.callback({
    redirectUri: 'http://localhost:3000/profile',
  })
);

Adding a route to the config so that it reads:

const config = {
  authRequired: false,
  auth0Logout: true,
  baseURL: 'http://localhost:3000',
  clientID: 'CLIENT_ID',
  issuerBaseURL: 'BASE_URL',
  secret: 'SECRET',
  routes: {
    callback: '/profile'
  }
}

No matter what I’ve tried it returns to “/” after the login is completed, nothing I do seems to make the post request actually trigger or do anything!

Can somebody please help me because I’m absolutely losing my mind on this!

What am I missing?

Thanks!

It seems like you’re facing an issue with your web application’s login functionality and redirection to a specific URL after login. Without specific details about the technologies and frameworks you’re using, I can provide you with a general guideline on how to handle this situation. Please adapt the following steps to your specific tech stack:

  1. Authentication Setup: Ensure you have a proper authentication system in place that can handle user logins and issue authentication tokens or sessions.

  2. User Authentication: When a user logs in, validate their credentials and generate an authentication token or session. This token/session will represent the user’s logged-in state.

  3. Redirection Logic: After successful authentication, your backend code should include logic to redirect the user to a specific URL. This URL is usually stored as a parameter during the login process.

  4. Session Storage: If you’re using sessions, store the authentication session details (such as user ID and other necessary information) on the server side.

  5. Redirect Response: When the login is successful, send a response from your backend that instructs the browser to redirect to the specified URL.

Here’s a basic example using a pseudo code-like structure:

# Pseudo code example using Python and Flask framework

from flask import Flask, request, redirect, url_for, session

app = Flask(__name__)
app.secret_key = 'your_secret_key'

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        # Validate user credentials
        if valid_user_credentials(request.form['username'], request.form['password']):
            # Store user session information
            session['user_id'] = get_user_id(request.form['username'])
            
            # Redirect to the desired URL (profile page in this case)
            return redirect(url_for('profile'))
    return 'Login page'

@app.route('/profile')
def profile():
    # Check if user is authenticated
    if 'user_id' in session:
        # Load user data and render the profile page
        user_data = get_user_data(session['user_id'])
        return render_template('profile.html', user_data=user_data)
    # If not authenticated, redirect to the login page
    return redirect(url_for('login'))

if __name__ == '__main__':
    app.run()

KMFusa
Remember, this is just a simplified example and doesn’t cover all possible scenarios. Depending on your technology stack (Flask, Django, Express.js, etc.), the actual implementation might differ. Make sure to adapt the code to fit your application’s structure and requirements.

If you’re still facing difficulties, provide more details about the technologies you’re using, and I’ll be happy to assist further.

Ah sorry, I forgot to say, I’m using Express.