# Production Google Auth Fix - Checklist

## Error
```
Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received
```

This error occurs when NextAuth can't properly handle the OAuth callback in production.

## Fix Checklist

### 1. Google Cloud Console Configuration

✅ **Add Production Redirect URIs:**
1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Navigate to: **APIs & Services > Credentials**
3. Click on your OAuth 2.0 Client ID
4. Under **Authorized redirect URIs**, add:
   - `https://mymorni.com/api/auth/callback/google`
   - `http://localhost:3000/api/auth/callback/google` (for development)
5. Click **Save**

### 2. Production Environment Variables

✅ **Set these in your hosting platform** (Vercel/Netlify/AWS/etc.):

```bash
NEXTAUTH_URL=https://mymorni.com
NEXTAUTH_SECRET=qH9Jqg0gHwQq5aVDiUgD8n3Qvyy7D3i7w5Z0YZzEFlw=
GOOGLE_CLIENT_ID=422332997315-b29nrd48u0si04h9jlffggt5djennkcb.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=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
```

**Important Notes:**
- `NEXTAUTH_URL` must match your production domain exactly (including https://)
- Do NOT include trailing slash in NEXTAUTH_URL
- If using Vercel, set these in: Project Settings > Environment Variables > Production

### 3. Code Changes Applied

✅ **Updated `/app/api/auth/[...nextauth]/route.ts`:**
- Added explicit `secret` configuration
- Added Google OAuth authorization parameters for better reliability
- Improved redirect handler to handle production URLs correctly
- Added debug mode for development

✅ **Updated `/app/google-login/page.tsx`:**
- Added console logging for debugging
- Improved error handling

### 4. Deployment Steps

1. **Push code changes to your repository**
   ```bash
   git add .
   git commit -m "Fix: Google OAuth production configuration"
   git push
   ```

2. **Verify environment variables** in your hosting platform

3. **Rebuild and redeploy** your application

4. **Test the flow:**
   - Visit `https://mymorni.com/auth/login`
   - Click "Sign in with Google"
   - Complete Google authentication
   - Verify redirect to `/google-login` works
   - Verify final redirect to dashboard/category works

### 5. Verification

After deployment, check:
- [ ] Google sign-in button triggers Google OAuth
- [ ] Google consent screen shows correctly
- [ ] Redirect back to your site works (no error in console)
- [ ] User is logged in successfully
- [ ] User is redirected to correct page (dashboard/category)
- [ ] No console errors about message channels

### 6. Debugging in Production

If issues persist, check:

1. **Browser Console:**
   - Look for NextAuth errors
   - Check network tab for failed requests to `/api/auth/*`

2. **Google Console:**
   - Verify redirect URI exactly matches: `https://mymorni.com/api/auth/callback/google`
   - Check OAuth consent screen is published (not in testing mode)

3. **Server Logs:**
   - Check for NextAuth errors
   - Verify NEXTAUTH_URL is set correctly

4. **Common Issues:**
   - Redirect URI mismatch (most common)
   - Missing NEXTAUTH_URL in production
   - Wrong domain in NEXTAUTH_URL
   - OAuth app in testing mode (limits users to test users only)

## Additional Recommendations

### For Better Security (Optional):

1. **Use separate Google OAuth clients for dev/prod:**
   - Create two OAuth clients in Google Console
   - Use different credentials for localhost vs production
   - Prevents accidental auth issues between environments

2. **Add proper error monitoring:**
   ```typescript
   // In your NextAuth config
   events: {
     async signIn({ user, account, profile, isNewUser }) {
       console.log(`User signed in: ${user.email}`);
     },
     async signOut({ token, session }) {
       console.log(`User signed out`);
     },
     async error(error) {
       console.error(`NextAuth error:`, error);
     },
   },
   ```

## Testing in Development

To test locally with production-like settings:
```bash
# Temporarily set production URL
NEXTAUTH_URL=http://localhost:3000 npm run dev
```

## Support Resources

- [NextAuth.js Documentation](https://next-auth.js.org/)
- [Google OAuth Documentation](https://developers.google.com/identity/protocols/oauth2)
- [Vercel Environment Variables](https://vercel.com/docs/concepts/projects/environment-variables)
