From 613957c5eb5f6d5908d84ea0c0994e9a9f1b92e0 Mon Sep 17 00:00:00 2001 From: Alexandru Eduard Farcas Date: Sun, 18 May 2025 10:24:54 +0300 Subject: [PATCH] fix build --- src/app/admin/page.tsx | 2 +- src/app/api/auth/[...nextauth]/authOptions.ts | 70 ++++++++++++++++++ src/app/api/auth/[...nextauth]/route.ts | 71 +------------------ src/app/api/youtube-handle/route.ts | 2 +- src/app/giveaways/page.tsx | 2 +- src/app/profile/page.tsx | 2 +- 6 files changed, 76 insertions(+), 73 deletions(-) create mode 100644 src/app/api/auth/[...nextauth]/authOptions.ts diff --git a/src/app/admin/page.tsx b/src/app/admin/page.tsx index f0e9acf..0d1b0b0 100644 --- a/src/app/admin/page.tsx +++ b/src/app/admin/page.tsx @@ -1,5 +1,5 @@ import { getServerSession } from "next-auth"; -import { authOptions } from "../api/auth/[...nextauth]/route"; +import { authOptions } from "../api/auth/[...nextauth]/authOptions"; import { redirect } from "next/navigation"; import AdminClient from "./AdminClient"; // Adjust the import path as necessary diff --git a/src/app/api/auth/[...nextauth]/authOptions.ts b/src/app/api/auth/[...nextauth]/authOptions.ts new file mode 100644 index 0000000..1f970d8 --- /dev/null +++ b/src/app/api/auth/[...nextauth]/authOptions.ts @@ -0,0 +1,70 @@ +import { PrismaAdapter } from "@next-auth/prisma-adapter"; +import GoogleProvider from "next-auth/providers/google"; +import { SessionStrategy } from "next-auth"; +import { db } from "@/lib/db"; +import axios from "axios"; + +export const authOptions = { + adapter: PrismaAdapter(db), + providers: [ + GoogleProvider({ + clientId: process.env.GOOGLE_CLIENT_ID!, + clientSecret: process.env.GOOGLE_CLIENT_SECRET!, + allowDangerousEmailAccountLinking: true, + authorization: { + params: { + scope: "openid email profile https://www.googleapis.com/auth/youtube.readonly", + }, + }, + }), + ], + secret: process.env.NEXTAUTH_SECRET, + session: { + strategy: "database" as SessionStrategy, + }, + callbacks: { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async signIn({ user, account, profile }: { user: any; account: any; profile?: any }) { + if (!user || !account || !profile) { + console.error("Sign-in failed: Missing user, account, or profile data."); + return false; + } + + if (account.provider === "google" && account.access_token) { + try { + console.log("Fetching YouTube handle for user:", user.email); + const { data } = await axios.get("https://www.googleapis.com/youtube/v3/channels?part=snippet&mine=true", { + params: { + part: "brandingSettings", + mine: "true", + }, + headers: { + Authorization: `Bearer ${account.access_token}`, + }, + }); + + console.log("YouTube API response:", JSON.stringify(data)); + + const customUrl = data.items?.[0]?.snippet?.customUrl; + + const youtubeHandle = customUrl ; + + console.log("Fetched YouTube handle:", youtubeHandle); + user.youtubeHandle = youtubeHandle; + + } catch (error) { + console.error("Error fetching YouTube handle:", error); + } + } + + return true; + }, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async session({ session, user }: { session: any; user: any }) { + session.user.id = user.id; + session.user.youtubeHandle = user.youtubeHandle; // Only attach YouTube handle + return session; + }, + }, + debug: true, // Enable debug mode for detailed error messages +}; \ No newline at end of file diff --git a/src/app/api/auth/[...nextauth]/route.ts b/src/app/api/auth/[...nextauth]/route.ts index 170343d..8c3dbb9 100644 --- a/src/app/api/auth/[...nextauth]/route.ts +++ b/src/app/api/auth/[...nextauth]/route.ts @@ -1,78 +1,11 @@ -import NextAuth, { SessionStrategy } from "next-auth"; -import GoogleProvider from "next-auth/providers/google"; -import { PrismaAdapter } from "@next-auth/prisma-adapter"; +import NextAuth from "next-auth"; import { db } from "@/lib/db"; -import axios from "axios"; +import { authOptions } from "./authOptions"; if (!db) { throw new Error("Prisma client is not initialized. Check your database configuration."); } -export const authOptions = { - adapter: PrismaAdapter(db), - providers: [ - GoogleProvider({ - clientId: process.env.GOOGLE_CLIENT_ID!, - clientSecret: process.env.GOOGLE_CLIENT_SECRET!, - allowDangerousEmailAccountLinking: true, - authorization: { - params: { - scope: "openid email profile https://www.googleapis.com/auth/youtube.readonly", - }, - }, - }), - ], - secret: process.env.NEXTAUTH_SECRET, - session: { - strategy: "database" as SessionStrategy, - }, - callbacks: { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - async signIn({ user, account, profile }: { user: any; account: any; profile?: any }) { - if (!user || !account || !profile) { - console.error("Sign-in failed: Missing user, account, or profile data."); - return false; - } - - if (account.provider === "google" && account.access_token) { - try { - console.log("Fetching YouTube handle for user:", user.email); - const { data } = await axios.get("https://www.googleapis.com/youtube/v3/channels?part=snippet&mine=true", { - params: { - part: "brandingSettings", - mine: "true", - }, - headers: { - Authorization: `Bearer ${account.access_token}`, - }, - }); - - console.log("YouTube API response:", JSON.stringify(data)); - - const customUrl = data.items?.[0]?.snippet?.customUrl; - - const youtubeHandle = customUrl ; - - console.log("Fetched YouTube handle:", youtubeHandle); - user.youtubeHandle = youtubeHandle; - - } catch (error) { - console.error("Error fetching YouTube handle:", error); - } - } - - return true; - }, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - async session({ session, user }: { session: any; user: any }) { - session.user.id = user.id; - session.user.youtubeHandle = user.youtubeHandle; // Only attach YouTube handle - return session; - }, - }, - debug: true, // Enable debug mode for detailed error messages -}; - const handler = NextAuth(authOptions); export { handler as GET, handler as POST }; diff --git a/src/app/api/youtube-handle/route.ts b/src/app/api/youtube-handle/route.ts index 71b8968..2ff4c93 100644 --- a/src/app/api/youtube-handle/route.ts +++ b/src/app/api/youtube-handle/route.ts @@ -1,6 +1,6 @@ import { NextResponse } from "next/server"; import { getServerSession } from "next-auth"; -import { authOptions } from "../auth/[...nextauth]/route"; +import { authOptions } from "../auth/[...nextauth]/authOptions"; import { db } from "@/lib/db"; export async function POST(req: Request) { diff --git a/src/app/giveaways/page.tsx b/src/app/giveaways/page.tsx index 8a78db9..d96e286 100644 --- a/src/app/giveaways/page.tsx +++ b/src/app/giveaways/page.tsx @@ -1,5 +1,5 @@ import { getServerSession } from "next-auth"; -import { authOptions } from "../api/auth/[...nextauth]/route"; +import { authOptions } from "../api/auth/[...nextauth]/authOptions"; import { db } from "@/lib/db"; import { redirect } from "next/navigation"; import GiveawayCard from "@/components/GiveawayCard"; diff --git a/src/app/profile/page.tsx b/src/app/profile/page.tsx index 54a40d3..c497672 100644 --- a/src/app/profile/page.tsx +++ b/src/app/profile/page.tsx @@ -1,5 +1,5 @@ import { getServerSession } from "next-auth"; -import { authOptions } from "@/app/api/auth/[...nextauth]/route"; +import { authOptions } from "@/app/api/auth/[...nextauth]/authOptions"; import { db } from "@/lib/db"; import { redirect } from "next/navigation"; import ProfileClient from "./ProfileClient";