Impersonation Setup
This document explains how the user impersonation feature works and how to configure it.
How It Works
- Super Admin Initiates Impersonation: A super admin clicks "Impersonate" on a user in the admin panel
- Magic Link Generation: The backend generates a Supabase magic link for the target user
- Callback Handling: The frontend callback page (
/auth/impersonate-callback) processes the magic link and sets the session - Session Switch: The admin is now signed in as the target user
- Switch Back: The admin can click "Switch Back" to return to their own account
Supabase Configuration
1. Add Redirect URL
You need to add the impersonation callback URL to Supabase's allowed redirect URLs:
- Go to Supabase Dashboard → Authentication → URL Configuration
- Add to "Redirect URLs":
- For local development:
http://localhost:3000/auth/impersonate-callback - For production:
https://yourdomain.com/auth/impersonate-callback
- For local development:
2. Verify Email Settings
For impersonation to work smoothly, ensure:
- Email confirmations are disabled for development (or users must have confirmed emails)
- Magic links are enabled in Authentication → Settings
Troubleshooting
Issue: "Just logged me out" / Redirects to sign-in
Possible causes:
- Redirect URL not added to Supabase allowed URLs
- Magic link expired (they expire after a short time)
- Session not being set properly
Solutions:
- Verify the redirect URL is in Supabase's allowed list
- Try impersonating again (generate a fresh magic link)
- Check browser console for errors
- Verify the callback page is accessible at
/auth/impersonate-callback
Issue: Session not persisting
Possible causes:
- Cookie/Session storage issues
- CORS configuration
- SameSite cookie warnings
Solutions:
- Check browser console for cookie warnings
- Verify
NEXT_PUBLIC_SUPABASE_URLis correct - Clear browser cookies and try again
- Check that cookies are being set (in DevTools → Application → Cookies)
Testing Impersonation
- Sign in as a super admin
- Go to
/dashboard/admin - Click "Impersonate" on any user
- You should be redirected and signed in as that user
- You should see a yellow banner indicating impersonation mode
- Click "Switch Back" to return to your admin account
Security Notes
- Only super admins can impersonate users
- All impersonation actions are logged in the
admin_impersonationstable - The original admin email is stored in localStorage for easy switch-back
- Impersonation sessions are clearly marked in the UI