Skip to content

Commit

Permalink
adding google auth provider
Browse files Browse the repository at this point in the history
  • Loading branch information
KobeZ123 committed Jul 30, 2024
1 parent 21302e0 commit ced863e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 21 additions & 2 deletions src/pages/sign-in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
}

const SignIn: NextPage = () => {
const handleOnclick = (e: React.MouseEvent<HTMLButtonElement>) => {
const handleOnNortheasternSignInClick = (
e: React.MouseEvent<HTMLButtonElement>
) => {
e.preventDefault();
signIn("azure-ad", {
callbackUrl: "/",
});
};

const handleOnGoogleSignInClick = (
e: React.MouseEvent<HTMLButtonElement>
) => {
e.preventDefault();
signIn("google", {
callbackUrl: "/",
});
};

return (
<>
<Head>
Expand All @@ -45,11 +57,18 @@ const SignIn: NextPage = () => {
<div className="flex min-h-screen items-center justify-center bg-gray-100">
<div className="m-4 flex w-fit flex-col items-center justify-center space-y-4 rounded-2xl bg-white p-6 drop-shadow-lg">
<Header />
<button onClick={handleOnclick}>
<button onClick={handleOnNortheasternSignInClick}>
<div className="flex w-64 cursor-pointer items-center justify-center rounded bg-blue-500 px-4 py-3 text-center text-sm font-bold text-white shadow hover:bg-blue-700">
Sign in with Northeastern!
</div>
</button>
{process.env.NODE_ENV !== "production" && (
<button onClick={handleOnGoogleSignInClick}>
<div className="flex w-64 cursor-pointer items-center justify-center rounded bg-blue-500 px-4 py-3 text-center text-sm font-bold text-white shadow hover:bg-blue-700">
Sign in via Google!
</div>
</button>
)}
</div>
</div>
</>
Expand Down
6 changes: 6 additions & 0 deletions src/utils/env/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
}),
};

0 comments on commit ced863e

Please sign in to comment.