Auth0 User Name Collection Feature
Overview
This feature collects the user's name on first login/signup and stores it in Auth0's user metadata. The name is then displayed throughout the app instead of the hardcoded "Mooncake Unicorn" text.
How It Works
1. First Login/Signup Flow
- User logs in or signs up via Auth0
- After successful authentication, the callback checks if user has a name in their metadata
- If no name exists, a
needs_nameflag is set in the session - User is redirected to the dashboard
2. Name Collection Modal
- When
needs_nameflag is present, a modal automatically appears - Modal cannot be dismissed (backdrop is static)
- User enters their name and clicks "Save"
- Name is sent to
/auth/update-nameendpoint via AJAX
3. Updating Auth0 Metadata
- Backend uses Auth0 Management API to update user metadata
- Name is stored in
user_metadata.namefield - Session is updated with new metadata
needs_nameflag is cleared- Page reloads to show the new name
4. Display User Name
- Context processor injects
user_nameinto all templates - Name is displayed in:
- Mobile header (top bar)
- Sidebar logo/title
- Falls back to email username if no name is set
- Falls back to "User" if no email available
Files Modified
Backend
apps/public/auth/auth0_helper.py- Added
get_user_name()- retrieves name from session/metadata -
Added
update_user_metadata(name)- updates Auth0 via Management API -
apps/public/auth/routes_auth0.py - Updated
callback()- checks for name and setsneeds_nameflag -
Added
update_name()- POST endpoint to update user metadata -
apps/public/app.py - Added
inject_user_name()context processor
Frontend
apps/public/templates/base.html- Updated sidebar and mobile header to display
{{ user_name }} - Added name collection modal with JavaScript handling
- Modal appears when
session.get('needs_name')is True
Auth0 Configuration Required
Management API Access
For the name update feature to work, your Auth0 application needs access to the Management API:
- Go to Auth0 Dashboard → Applications → Your Application → APIs tab
- Enable "Auth0 Management API"
- Grant the following permissions:
read:usersupdate:usersupdate:users_app_metadata
Alternative: Machine-to-Machine Application
If you prefer, you can create a separate Machine-to-Machine application:
- Create M2M App: Auth0 Dashboard → Applications → Create Application → Machine to Machine
- Authorize for Management API: Select "Auth0 Management API"
- Grant Scopes:
read:users,update:users,update:users_app_metadata - Update Environment Variables: Use the M2M app's credentials instead
Environment Variables
No new environment variables needed - uses existing Auth0 credentials:
- AUTH0_DOMAIN
- AUTH0_CLIENT_ID
- AUTH0_CLIENT_SECRET
Testing
Test New User Flow
- Clear your session: Visit
/clear-session - Log in with a new account
- Modal should appear asking for name
- Enter name and click Save
- Page reloads with your name displayed
Test Existing User
- Log in with existing account that has name in metadata
- No modal should appear
- Name should display immediately
Test Fallback
- Log in with account that has no name
- Modal appears
- Click Save without entering name
- Error message appears
- Enter name and save successfully
User Metadata Structure
{
"user_metadata": {
"name": "John Doe"
}
}
Future Enhancements
- Allow users to update their name from profile page
- Add profile picture upload
- Store additional preferences (timezone, currency, etc.)
- Add email verification requirement before name collection