From 10593bd667e5c10a7d72f5795279752159d49832 Mon Sep 17 00:00:00 2001 From: Saatvik Arya Date: Tue, 23 Jul 2024 15:55:14 -0700 Subject: [PATCH 1/3] gh login --- .../(auth)/signin/_components/GitHubLogin.tsx | 25 +++++++++++++++++ .../(auth)/signin/_components/GoogleLogin.tsx | 25 +++++++++++++++++ apps/web/app/(auth)/signin/page.tsx | 28 ++++++------------- apps/web/server/auth.ts | 5 ++++ 4 files changed, 64 insertions(+), 19 deletions(-) create mode 100644 apps/web/app/(auth)/signin/_components/GitHubLogin.tsx create mode 100644 apps/web/app/(auth)/signin/_components/GoogleLogin.tsx diff --git a/apps/web/app/(auth)/signin/_components/GitHubLogin.tsx b/apps/web/app/(auth)/signin/_components/GitHubLogin.tsx new file mode 100644 index 00000000..6d35e89f --- /dev/null +++ b/apps/web/app/(auth)/signin/_components/GitHubLogin.tsx @@ -0,0 +1,25 @@ +import { signIn } from "@/server/auth"; +import { Github } from "@repo/ui/components/icons"; + +function GitHubLogin() { + return ( +
{ + "use server"; + await signIn("github", { + redirectTo: "/home", + }); + }} + > + +
+ ) +} + +export default GitHubLogin; \ No newline at end of file diff --git a/apps/web/app/(auth)/signin/_components/GoogleLogin.tsx b/apps/web/app/(auth)/signin/_components/GoogleLogin.tsx new file mode 100644 index 00000000..13b6986d --- /dev/null +++ b/apps/web/app/(auth)/signin/_components/GoogleLogin.tsx @@ -0,0 +1,25 @@ +import { signIn } from "@/server/auth"; +import { Google } from "@repo/ui/components/icons"; + +function GoogleLogin() { + return ( +
{ + "use server"; + await signIn("google", { + redirectTo: "/home", + }); + }} + > + +
+ ) +} + +export default GoogleLogin; \ No newline at end of file diff --git a/apps/web/app/(auth)/signin/page.tsx b/apps/web/app/(auth)/signin/page.tsx index a31343cd..cd01ab04 100644 --- a/apps/web/app/(auth)/signin/page.tsx +++ b/apps/web/app/(auth)/signin/page.tsx @@ -1,12 +1,12 @@ import Image from "next/image"; import Link from "next/link"; import Logo from "@/public/logo.svg"; -import { auth, signIn } from "@/server/auth"; -import { Google } from "@repo/ui/components/icons"; +import { auth } from "@/server/auth"; import gradientStyle from "./_components/TextGradient/gradient.module.css"; import { cn } from "@repo/ui/lib/utils"; import { redirect } from "next/navigation"; -import { toast } from "sonner"; +import GoogleLogin from "./_components/GoogleLogin"; +import GitHubLogin from "./_components/GitHubLogin"; export const runtime = "edge"; @@ -60,22 +60,12 @@ async function Signin({
-
{ - "use server"; - await signIn("google", { - redirectTo: "/home?firstTime=true", - }); - }} - > - -
+ +
+
+
diff --git a/apps/web/server/auth.ts b/apps/web/server/auth.ts index 78671551..9b35db2c 100644 --- a/apps/web/server/auth.ts +++ b/apps/web/server/auth.ts @@ -1,5 +1,6 @@ import NextAuth, { NextAuthResult } from "next-auth"; import Google from "next-auth/providers/google"; +import GitHub from "next-auth/providers/github"; import { DrizzleAdapter } from "@auth/drizzle-adapter"; import { db } from "./db"; import { accounts, sessions, users, verificationTokens } from "./db/schema"; @@ -31,5 +32,9 @@ export const { clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, }), + GitHub({ + clientId: process.env.GITHUB_CLIENT_ID, + clientSecret: process.env.GITHUB_CLIENT_SECRET, + }), ], }); From a9f4170489505f0b4d1bbb96af8cc6fd35cd0a9a Mon Sep 17 00:00:00 2001 From: Saatvik Arya Date: Tue, 23 Jul 2024 15:56:44 -0700 Subject: [PATCH 2/3] convert to tabs --- apps/web/app/(auth)/signin/page.tsx | 2 +- apps/web/server/auth.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/web/app/(auth)/signin/page.tsx b/apps/web/app/(auth)/signin/page.tsx index cd01ab04..68b8b0eb 100644 --- a/apps/web/app/(auth)/signin/page.tsx +++ b/apps/web/app/(auth)/signin/page.tsx @@ -62,7 +62,7 @@ async function Signin({ >
-
diff --git a/apps/web/server/auth.ts b/apps/web/server/auth.ts index 9b35db2c..71a1ff8f 100644 --- a/apps/web/server/auth.ts +++ b/apps/web/server/auth.ts @@ -32,9 +32,9 @@ export const { clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, }), - GitHub({ - clientId: process.env.GITHUB_CLIENT_ID, - clientSecret: process.env.GITHUB_CLIENT_SECRET, - }), + GitHub({ + clientId: process.env.GITHUB_CLIENT_ID, + clientSecret: process.env.GITHUB_CLIENT_SECRET, + }), ], }); From 1d0880577cd7cfc807a3aba388c12d2dddec87fa Mon Sep 17 00:00:00 2001 From: Saatvik Arya Date: Wed, 24 Jul 2024 12:20:23 -0700 Subject: [PATCH 3/3] wip --- apps/web/app/actions/fetchers.ts | 13 +++++++++++++ apps/web/app/api/ensureAuth.ts | 16 ++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/apps/web/app/actions/fetchers.ts b/apps/web/app/actions/fetchers.ts index 8d6802a7..108f5086 100644 --- a/apps/web/app/actions/fetchers.ts +++ b/apps/web/app/actions/fetchers.ts @@ -3,6 +3,7 @@ import { and, asc, eq, exists, not, or } from "drizzle-orm"; import { db } from "../../server/db"; import { + accounts, canvas, chatHistory, ChatThread, @@ -39,6 +40,18 @@ export const getUser = async (): ServerActionReturnType => { return { success: true, data: user }; }; +export const getProvider = async (userId: string): ServerActionReturnType => { + const account = await db.query.accounts.findFirst({ + where: eq(accounts.userId, userId), + }); + + if (!account) { + return { error: "No account found", success: false }; + } + + return { success: true, data: account.provider }; +} + export const getSpaces = async (): ServerActionReturnType => { const data = await auth(); diff --git a/apps/web/app/api/ensureAuth.ts b/apps/web/app/api/ensureAuth.ts index 1fcd2914..95d27a54 100644 --- a/apps/web/app/api/ensureAuth.ts +++ b/apps/web/app/api/ensureAuth.ts @@ -1,6 +1,6 @@ import { NextRequest } from "next/server"; import { db } from "../../server/db"; -import { accounts, sessions, users } from "../../server/db/schema"; +import { sessions, users } from "../../server/db/schema"; import { eq } from "drizzle-orm"; export async function ensureAuth(req: NextRequest) { @@ -31,21 +31,25 @@ export async function ensureAuth(req: NextRequest) { console.log(token, newToken); - const authUserFetch = await fetch( + const tokenInfo = await fetch( `https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=${newToken}`, ); - if (!authUserFetch.ok) { + const userInfo = await fetch( + `https://www.googleapis.com/oauth2/v1/userinfo?access_token=${newToken}`, + ); + + if (!tokenInfo.ok) { console.error( "Error fetching Google user,", - authUserFetch.statusText, - await authUserFetch.text(), + tokenInfo.statusText, + await tokenInfo.text(), ); console.log("Google user not found or error."); return undefined; } - const authUserData = (await authUserFetch.json()) as { + const authUserData = (await tokenInfo.json()) as { email: string; audience: string; issued_to: string;