Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NextAuth authentication #26

Merged
merged 9 commits into from
Feb 5, 2024
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
DATABASE_URL="postgresql://admin:admin@localhost:5432/cooper?schema=public"
NEXTAUTH_URL="localhost:3000"

NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET=

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ env:
NODE_VERSION: 20
PNPM_VERSION: 8

# Find a workaround to this.
# Find a workaround for this.
DATABASE_URL: "postgresql://admin:admin@localhost:5432/cooper?schema=public"
NEXTAUTH_URL: "localhost:3000"
NEXTAUTH_SECRET: "sec"
GOOGLE_CLIENT_ID: "cooper"
GOOGLE_CLIENT_SECRET: "cooper"

jobs:
lint:
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,16 @@ cp .env.example .env

```env
DATABASE_URL="postgresql://admin:admin@localhost:5432/cooper?schema=public"
NEXTAUTH_URL="localhost:3000"

NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET=

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
```

To generate `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET`, see [Setting up OAuth 2.0](https://support.google.com/cloud/answer/6158849?hl=en). To generate a new `NEXTAUTH_SECRET`, run the following command in your terminal and add it to the `.env` file.

```bash
openssl rand -base64 32
```
50 changes: 49 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
import { redirect } from "next/navigation";
import { getServerAuthSession } from "~/server/auth";

export default async function Home() {
return <h1>Cooper</h1>;
const session = await getServerAuthSession();

if (!session) {
return (
<div className="flex h-screen flex-col items-center justify-center space-y-4">
<p className="text-3xl font-semibold text-red-500">
You are not signed in!
</p>
<a
href="/api/auth/signin/google"
type="button"
className="mb-2 me-2 inline-flex items-center rounded-lg bg-[#4285F4] px-5 py-2.5 text-center text-sm font-medium text-white hover:bg-[#4285F4]/90 focus:outline-none focus:ring-4 focus:ring-[#4285F4]/50 dark:focus:ring-[#4285F4]/55"
>
<svg
className="me-2 h-4 w-4"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 18 19"
>
<path
fillRule="evenodd"
d="M8.842 18.083a8.8 8.8 0 0 1-8.65-8.948 8.841 8.841 0 0 1 8.8-8.652h.153a8.464 8.464 0 0 1 5.7 2.257l-2.193 2.038A5.27 5.27 0 0 0 9.09 3.4a5.882 5.882 0 0 0-.2 11.76h.124a5.091 5.091 0 0 0 5.248-4.057L14.3 11H9V8h8.34c.066.543.095 1.09.088 1.636-.086 5.053-3.463 8.449-8.4 8.449l-.186-.002Z"
clipRule="evenodd"
/>
</svg>
Sign in with Google
</a>
</div>
);
}

return (
<div className="flex h-screen flex-col items-center justify-center space-y-4">
<p className="text-3xl font-semibold text-emerald-500">
Welcome, {session.user.name}!
</p>
<a
href="/api/auth/signout"
type="button"
className="mb-2 me-2 rounded-lg bg-red-700 px-5 py-2.5 text-sm font-medium text-white hover:bg-red-800 focus:outline-none focus:ring-4 focus:ring-red-300 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-900"
>
Sign Out
</a>
</div>
);
}
16 changes: 7 additions & 9 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ export const env = createEnv({
.enum(["development", "test", "production"])
.default("development"),
NEXTAUTH_SECRET:
// process.env.NODE_ENV === "production"
// ? z.string()
// :
z.string().optional(),
process.env.NODE_ENV === "production"
? z.string()
: z.string().optional(),
NEXTAUTH_URL: z.preprocess(
// This makes Vercel deployments not fail if you don't set NEXTAUTH_URL
// Since NextAuth.js automatically uses the VERCEL_URL if present.
(str) => process.env.VERCEL_URL ?? str,
// VERCEL_URL doesn't include `https` so it cant be validated as a URL
process.env.VERCEL ? z.string() : z.string().url(),
),
// Optional for now -- remember to change this if have decided an auth provider
GOOGLE_CLIENT_ID: z.string().optional(),
GOOGLE_CLIENT_SECRET: z.string().optional(),
GOOGLE_CLIENT_ID: z.string(),
GOOGLE_CLIENT_SECRET: z.string(),
},

/**
Expand All @@ -52,8 +50,8 @@ export const env = createEnv({
NODE_ENV: process.env.NODE_ENV,
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
GOOGLE_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
GOOGLE_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down
1 change: 0 additions & 1 deletion src/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ declare module "next-auth" {
// // role: UserRole;
// }
}

/**
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
*
Expand Down
Loading