I'm experiencing an issue with session management when using Discord OAuth. After successfully logging in with GitHub, the user is redirected to the dashboard, and from dashboard when user tries to connect their discord account , after the oauth route redirection the session appears as undefined, leading to an error message: [Error: No session] causing user to redirect from dashboard to home. Despite this, refreshing the home redirects the user back to the dashboard, indicating that a session does exist. Interestingly, this issue does not occur when using GitHub OAuth.
"use server";
import { updateOrCreateUser, updateUserDiscordId } from "@/lib/db/user";
import { createAdminClient } from "@/lib/server/appwrite";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
export async function GET(request: NextRequest) {
const userId = request.nextUrl.searchParams.get("userId");
const secret = request.nextUrl.searchParams.get("secret");
const provider = request.nextUrl.searchParams.get("provider");
if (!userId || !secret || !provider) {
return NextResponse.redirect(`${request.nextUrl.origin}/error?message=Missing userId, secret, or provider`);
}
const { account } = await createAdminClient();
const session = await account.createSession(userId, secret);
cookies().set("my-custom-session", session.secret, {
path: "/",
httpOnly: true,
sameSite: "strict",
secure: true,
});
if (provider === "github") {
await updateOrCreateUser();
} else if (provider === "discord") {
await updateUserDiscordId();
} else {
return NextResponse.redirect(`${request.nextUrl.origin}/error?message=Unknown provider`);
}
return NextResponse.redirect(`${request.nextUrl.origin}/dashboard`);
}
Logs :
POST /dashboard 303 in 669ms
session from cookies undefined
[Error: No session]
✓ Compiled /oauth in 154ms (857 modules)
session from cookies {
name: 'my-custom-session',
value: 'eyjMmE1ZjJhMGI0M2UxMzlhNjM3ZTJkOGU1MWE1YjYwMmMzYmZkMzYyFiYjg5ZDAwMjEwMTlhMmRkMDc3ZWI3NWNkZWUxOWNiOGUwMzYyMDNkM2QxY2Y3OTM3NjI3MTk3Y2VhMSJ9',
path: '/',
httpOnly: true,
sameSite: 'strict',
secure: true
}
GET /oauth?provider=discord&secret=029714989120e312d185d03befca981&userId=66aaa7d01f8d4a 307 in 2628ms
session from cookies undefined
[Error: No session]
session from cookies undefined
[Error: No session]
session from cookies undefined
Error: No session
at createSessionClient (webpack-internal:///(rsc)/./lib/server/appwrite.ts:21:15)
at getLoggedInUser (webpack-internal:///(rsc)/./lib/db/user.ts:29:108)
Recommended threads
- Authentication on custom Websocket Serve...
Hi, I want to use a custom Websocket Server (using Bun) for my application. However I cant really figure out authentication on custom servers. Session cookies ...
- Realtime for files() works almost well, ...
I have been trying to make use of realtime, today (14.03.26) I have pulled all the latest versions of docker images, and sdk available. Whats working: - Conn...
- Function issue
Hi,idk whats wrong with my function but i made some changes to the env var and made sure they saved then i redeployed it,but then after it redeeployed it asked ...