diff --git a/.env.example b/.env.example index a1565d7..dc00890 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,7 @@ DATABASE_URL="postgresql://admin:admin@localhost:5432/cooper?schema=public" -NEXTAUTH_URL="localhost:3000" \ No newline at end of file + +NEXTAUTH_URL="http://localhost:3000" +NEXTAUTH_SECRET= + +GOOGLE_CLIENT_ID= +GOOGLE_CLIENT_SECRET= \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c31eb4d..3e31a5b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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: diff --git a/README.md b/README.md index ae21254..003d640 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/src/app/page.tsx b/src/app/page.tsx index 2801364..6b8abe8 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,3 +1,51 @@ +import { redirect } from "next/navigation"; +import { getServerAuthSession } from "~/server/auth"; + export default async function Home() { - return

Cooper

; + const session = await getServerAuthSession(); + + if (!session) { + return ( +
+

+ You are not signed in! +

+ + + Sign in with Google + +
+ ); + } + + return ( +
+

+ Welcome, {session.user.name}! +

+ + Sign Out + +
+ ); } diff --git a/src/env.js b/src/env.js index 2e63d6a..e8dfd45 100644 --- a/src/env.js +++ b/src/env.js @@ -18,10 +18,9 @@ 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. @@ -29,9 +28,8 @@ export const env = createEnv({ // 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(), }, /** @@ -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 diff --git a/src/server/auth.ts b/src/server/auth.ts index 79d92bf..389a9e6 100644 --- a/src/server/auth.ts +++ b/src/server/auth.ts @@ -29,7 +29,6 @@ declare module "next-auth" { // // role: UserRole; // } } - /** * Options for NextAuth.js used to configure adapters, providers, callbacks, etc. *