Skip to content

Commit 6bc64f4

Browse files
committed
[last commit of they day ]Fix to remove some minor bugs
1 parent 46d87d7 commit 6bc64f4

2 files changed

Lines changed: 201 additions & 0 deletions

File tree

VERCEL_ENV_FIX.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# Fix NextAuth CLIENT_FETCH_ERROR on Vercel
2+
3+
## Problem
4+
```
5+
[next-auth][error][CLIENT_FETCH_ERROR]
6+
https://next-auth.js.org/errors#client_fetch_error undefined
7+
```
8+
9+
This error occurs when NextAuth environment variables are missing or incorrectly configured on Vercel.
10+
11+
## Solution: Set Environment Variables on Vercel
12+
13+
### 1. Generate NEXTAUTH_SECRET
14+
15+
Run this command to generate a secure secret:
16+
17+
```bash
18+
openssl rand -base64 32
19+
```
20+
21+
Copy the output (something like: `jK8mN2pQ5rS7tU9vW0xY1zA3bC4dE5fF6gH7iJ8kL9mN0oP1qR2sT3u=`)
22+
23+
### 2. Add Environment Variables to Vercel
24+
25+
Go to your Vercel project settings:
26+
27+
**Vercel Dashboard → Your Project → Settings → Environment Variables**
28+
29+
Add these variables:
30+
31+
#### Required Variables
32+
33+
| Variable | Value | Notes |
34+
|----------|-------|-------|
35+
| `NEXTAUTH_URL` | `https://miku-two.vercel.app` | Your full Vercel deployment URL |
36+
| `NEXTAUTH_SECRET` | `<generated_secret>` | The secret from step 1 |
37+
| `DISCORD_CLIENT_ID` | `<your_discord_app_client_id>` | From Discord Developer Portal |
38+
| `DISCORD_CLIENT_SECRET` | `<your_discord_app_client_secret>` | From Discord Developer Portal |
39+
| `BOT_API_URL` | `https://miku-fjwa.onrender.com` | Your Render backend URL |
40+
41+
#### Important Settings
42+
43+
For each environment variable:
44+
- ✅ Check **Production**
45+
- ✅ Check **Preview**
46+
- ✅ Check **Development** (optional)
47+
48+
### 3. Update Discord OAuth2 Redirect URL
49+
50+
Go to **Discord Developer Portal → Your App → OAuth2 → Redirects**
51+
52+
Add/Update redirect URL:
53+
```
54+
https://miku-two.vercel.app/api/auth/callback/discord
55+
```
56+
57+
### 4. Redeploy
58+
59+
After adding environment variables, trigger a new deployment:
60+
61+
**Option A - Via Vercel Dashboard:**
62+
- Go to **Deployments** tab
63+
- Click **** on the latest deployment
64+
- Click **Redeploy**
65+
66+
**Option B - Via Git Push:**
67+
```bash
68+
git commit --allow-empty -m "Trigger redeploy"
69+
git push
70+
```
71+
72+
### 5. Verify
73+
74+
After deployment completes:
75+
76+
1. Visit `https://miku-two.vercel.app`
77+
2. Click "Sign in with Discord"
78+
3. You should be redirected to Discord OAuth
79+
4. After authorization, you should be redirected back and signed in
80+
81+
Check Vercel logs for any remaining errors:
82+
- Go to **Deployments** → Click latest deployment → **Runtime Logs**
83+
84+
## Common Issues
85+
86+
### Issue: Still getting CLIENT_FETCH_ERROR
87+
88+
**Solution:** Clear browser cookies and try again:
89+
```
90+
1. Open browser DevTools (F12)
91+
2. Application/Storage tab
92+
3. Clear cookies for miku-two.vercel.app
93+
4. Refresh and try signing in again
94+
```
95+
96+
### Issue: OAuth redirect mismatch
97+
98+
**Solution:** Ensure Discord redirect URL EXACTLY matches:
99+
```
100+
https://miku-two.vercel.app/api/auth/callback/discord
101+
```
102+
- No trailing slash
103+
- Must use https
104+
- Must match your NEXTAUTH_URL
105+
106+
### Issue: Session not persisting
107+
108+
**Solution:** Ensure cookies are not blocked:
109+
- Check browser privacy settings
110+
- Try in incognito/private mode
111+
- Check browser console for cookie warnings
112+
113+
## Environment Variable Checklist
114+
115+
Copy this checklist and verify each variable:
116+
117+
```
118+
Vercel Environment Variables (https://vercel.com/your-account/miku-two/settings/environment-variables):
119+
120+
[ ] NEXTAUTH_URL = https://miku-two.vercel.app
121+
[ ] NEXTAUTH_SECRET = <32+ character random string>
122+
[ ] DISCORD_CLIENT_ID = <from Discord Developer Portal>
123+
[ ] DISCORD_CLIENT_SECRET = <from Discord Developer Portal>
124+
[ ] BOT_API_URL = https://miku-fjwa.onrender.com
125+
126+
Discord OAuth2 Redirects (https://discord.com/developers/applications):
127+
128+
[ ] https://miku-two.vercel.app/api/auth/callback/discord
129+
130+
Deployment:
131+
132+
[ ] Redeployed after adding environment variables
133+
[ ] No errors in Vercel Runtime Logs
134+
[ ] Successfully signed in with Discord
135+
```
136+
137+
## Technical Details
138+
139+
### What Changed
140+
141+
Added explicit NextAuth configuration in `[...nextauth].ts`:
142+
143+
```typescript
144+
export const authOptions: NextAuthOptions = {
145+
secret: process.env.NEXTAUTH_SECRET, // Now explicitly required
146+
session: {
147+
strategy: 'jwt',
148+
maxAge: 30 * 24 * 60 * 60, // 30 days
149+
},
150+
cookies: {
151+
sessionToken: {
152+
name: `${process.env.NODE_ENV === 'production' ? '__Secure-' : ''}next-auth.session-token`,
153+
options: {
154+
httpOnly: true,
155+
sameSite: 'lax',
156+
path: '/',
157+
secure: process.env.NODE_ENV === 'production',
158+
},
159+
},
160+
},
161+
// ... rest of config
162+
}
163+
```
164+
165+
### Why This Fixes It
166+
167+
1. **Explicit secret**: NextAuth now explicitly uses `NEXTAUTH_SECRET` environment variable
168+
2. **Production cookies**: Uses secure cookies in production (`__Secure-` prefix, `secure: true`)
169+
3. **Session strategy**: Explicitly sets JWT strategy with 30-day expiration
170+
4. **Cookie settings**: Properly configured for Vercel's serverless environment
171+
172+
## Support
173+
174+
If you continue to have issues after following this guide:
175+
176+
1. Check Vercel Runtime Logs for specific errors
177+
2. Check browser console (F12) for client-side errors
178+
3. Verify all environment variables are set correctly
179+
4. Try clearing all cookies and cache
180+
5. Test in incognito/private browsing mode
181+
182+
---
183+
184+
**Last Updated:** March 8, 2026

dash/src/pages/api/auth/[...nextauth].ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ export const authOptions: NextAuthOptions = {
99
authorization: { params: { scope: 'identify guilds' } },
1010
}),
1111
],
12+
secret: process.env.NEXTAUTH_SECRET,
13+
session: {
14+
strategy: 'jwt',
15+
maxAge: 30 * 24 * 60 * 60, // 30 days
16+
},
17+
cookies: {
18+
sessionToken: {
19+
name: `${process.env.NODE_ENV === 'production' ? '__Secure-' : ''}next-auth.session-token`,
20+
options: {
21+
httpOnly: true,
22+
sameSite: 'lax',
23+
path: '/',
24+
secure: process.env.NODE_ENV === 'production',
25+
},
26+
},
27+
},
1228
callbacks: {
1329
async jwt({ token, account, profile }) {
1430
if (account && profile) {
@@ -28,6 +44,7 @@ export const authOptions: NextAuthOptions = {
2844
pages: {
2945
signIn: '/',
3046
},
47+
debug: process.env.NODE_ENV === 'development',
3148
}
3249

3350
export default NextAuth(authOptions)

0 commit comments

Comments
 (0)