Zubi🌟Original post
Web Developer
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`);
}
Summary
Developers are encountering issues with session management when using Discord OAuth, leading to the session being undefined after redirection from the dashboard. The error message "[Error: No session]" is displayed, causing redirection back to the home page. However, refreshing the page after redirecting from the home page takes the user back to the dashboard, indicating that a session exists. The problem does not occur when using GitHub OAuth.
Solution:
- Check the session creation code and ensure it is correctly handling Discord OAuth sessions.
- Investigate if any specific configurations or logic are interfering with session handling for Discord OAuth compared to GitHub OAuth.