From ced863ec33132f0bb6883bac46c5d2a2cca0325a Mon Sep 17 00:00:00 2001 From: Kobe Date: Tue, 30 Jul 2024 17:52:30 -0400 Subject: [PATCH] adding google auth provider --- README.md | 4 ++++ src/pages/api/auth/[...nextauth].ts | 5 +++++ src/pages/sign-in.tsx | 23 +++++++++++++++++++++-- src/utils/env/server.ts | 6 ++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 98db9caf..9e28d014 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,10 @@ REGION_AWS= AZURE_CLIENT_ID= AZURE_CLIENT_SECRET= AZURE_TENANT_ID= + +# Google Auth Provider (used in the non-prod environment) +GOOGLE_CLIENT_ID= +GOOGLE_CLIENT_SECRET= ``` Then do `yarn` and `yarn dev` to get the project running. diff --git a/src/pages/api/auth/[...nextauth].ts b/src/pages/api/auth/[...nextauth].ts index 4cf03503..0c2bf190 100644 --- a/src/pages/api/auth/[...nextauth].ts +++ b/src/pages/api/auth/[...nextauth].ts @@ -4,6 +4,7 @@ import { NextAuthOptions } from "next-auth"; import { prisma } from "../../../server/db/client"; import { serverEnv } from "../../../utils/env/server"; import AzureADProvider from "next-auth/providers/azure-ad"; +import GoogleProvider from "next-auth/providers/google"; import { Adapter } from "next-auth/adapters"; const CustomPrismaAdapter = (p: typeof prisma): Adapter => { @@ -48,6 +49,10 @@ export const authOptions: NextAuthOptions = { }, adapter: CustomPrismaAdapter(prisma), providers: [ + GoogleProvider({ + clientId: serverEnv.GOOGLE_CLIENT_ID, + clientSecret: serverEnv.GOOGLE_CLIENT_SECRET, + }), AzureADProvider({ clientId: serverEnv.AZURE_CLIENT_ID, clientSecret: serverEnv.AZURE_CLIENT_SECRET, diff --git a/src/pages/sign-in.tsx b/src/pages/sign-in.tsx index 52d7021c..0f207fc7 100644 --- a/src/pages/sign-in.tsx +++ b/src/pages/sign-in.tsx @@ -30,12 +30,24 @@ export async function getServerSideProps(context: GetServerSidePropsContext) { } const SignIn: NextPage = () => { - const handleOnclick = (e: React.MouseEvent) => { + const handleOnNortheasternSignInClick = ( + e: React.MouseEvent + ) => { e.preventDefault(); signIn("azure-ad", { callbackUrl: "/", }); }; + + const handleOnGoogleSignInClick = ( + e: React.MouseEvent + ) => { + e.preventDefault(); + signIn("google", { + callbackUrl: "/", + }); + }; + return ( <> @@ -45,11 +57,18 @@ const SignIn: NextPage = () => {
- + {process.env.NODE_ENV !== "production" && ( + + )}
diff --git a/src/utils/env/server.ts b/src/utils/env/server.ts index 2aaff87f..b551815f 100644 --- a/src/utils/env/server.ts +++ b/src/utils/env/server.ts @@ -35,5 +35,11 @@ export const serverEnv = { AZURE_TENANT_ID: str({ input: process.env.AZURE_TENANT_ID, }), + GOOGLE_CLIENT_ID: str({ + input: process.env.GOOGLE_CLIENT_ID, + }), + GOOGLE_CLIENT_SECRET: str({ + input: process.env.GOOGLE_CLIENT_SECRET, + }), }), };