From 9c318b4451e09c9a9b7ee92c348fa19d4c26837f Mon Sep 17 00:00:00 2001 From: Zeke Abshire Date: Sat, 9 Nov 2024 18:41:41 -0600 Subject: [PATCH] Set up auth + corrected local db --- src/app/_components/auth-provider.tsx | 24 +++++++++++++++ src/app/layout.tsx | 9 ++++-- src/app/page.tsx | 18 +++++++++++- src/env.js | 8 ++--- src/server/auth/config.ts | 23 ++++++++------- start-database.sh | 42 ++++++++++++--------------- 6 files changed, 81 insertions(+), 43 deletions(-) create mode 100644 src/app/_components/auth-provider.tsx diff --git a/src/app/_components/auth-provider.tsx b/src/app/_components/auth-provider.tsx new file mode 100644 index 0000000..469d37d --- /dev/null +++ b/src/app/_components/auth-provider.tsx @@ -0,0 +1,24 @@ +"use client"; + +import type { FC, PropsWithChildren } from "react"; +import { useSession, signIn, SessionProvider } from "next-auth/react"; + +export const AuthProvider: FC = ({ children }) => { + return ( + + {children} + + ); +}; + +const AuthWrapper: FC = ({ children }) => { + const session = useSession(); + + console.log("SESSION", session); + + if (session.status === "unauthenticated") { + void signIn(); + } + + return children; +}; diff --git a/src/app/layout.tsx b/src/app/layout.tsx index ee48964..35cec5c 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -5,10 +5,11 @@ import { type Metadata } from "next"; import { TRPCReactProvider } from "~/trpc/react"; import { HydrateClient } from "~/trpc/server"; +import { AuthProvider } from "~/app/_components/auth-provider"; export const metadata: Metadata = { - title: "Create T3 App", - description: "Generated by create-t3-app", + title: "ls", + description: "quick note taking application", icons: [{ rel: "icon", url: "/favicon.ico" }], }; @@ -19,7 +20,9 @@ export default function RootLayout({ - {children} + + {children} + diff --git a/src/app/page.tsx b/src/app/page.tsx index ebffb00..ced12e2 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,3 +1,19 @@ +import { auth } from "~/server/auth"; + export default async function Home() { - return

ls

; + const session = await auth(); + if (!session) { +
+

Not signed in

+
; + } + + return ( +
+

ls

+
+        {JSON.stringify(session, undefined, 2)}
+      
+
+ ); } diff --git a/src/env.js b/src/env.js index 4809809..bf52369 100644 --- a/src/env.js +++ b/src/env.js @@ -11,8 +11,8 @@ export const env = createEnv({ process.env.NODE_ENV === "production" ? z.string() : z.string().optional(), - // AUTH_DISCORD_ID: z.string(), - // AUTH_DISCORD_SECRET: z.string(), + AUTH_GOOGLE_ID: z.string(), + AUTH_GOOGLE_SECRET: z.string(), DATABASE_URL: z.string().url(), NODE_ENV: z .enum(["development", "test", "production"]) @@ -34,8 +34,8 @@ export const env = createEnv({ */ runtimeEnv: { AUTH_SECRET: process.env.AUTH_SECRET, - // AUTH_DISCORD_ID: process.env.AUTH_DISCORD_ID, - // AUTH_DISCORD_SECRET: process.env.AUTH_DISCORD_SECRET, + AUTH_GOOGLE_ID: process.env.AUTH_GOOGLE_ID, + AUTH_GOOGLE_SECRET: process.env.AUTH_GOOGLE_SECRET, DATABASE_URL: process.env.DATABASE_URL, NODE_ENV: process.env.NODE_ENV, }, diff --git a/src/server/auth/config.ts b/src/server/auth/config.ts index 3ef82a2..bbd27fe 100644 --- a/src/server/auth/config.ts +++ b/src/server/auth/config.ts @@ -1,6 +1,6 @@ import { DrizzleAdapter } from "@auth/drizzle-adapter"; import { type DefaultSession, type NextAuthConfig } from "next-auth"; -import DiscordProvider from "next-auth/providers/discord"; +import Google from "next-auth/providers/google"; import { db } from "~/server/db"; import { @@ -38,16 +38,16 @@ declare module "next-auth" { */ export const authConfig = { providers: [ - DiscordProvider, - /** - * ...add more providers here. - * - * Most other providers require a bit more work than the Discord provider. For example, the - * GitHub provider requires you to add the `refresh_token_expires_in` field to the Account - * model. Refer to the NextAuth.js docs for the provider you want to use. Example: - * - * @see https://next-auth.js.org/providers/github - */ + Google({ + // Should only be necessary in development when the auth session bugs out + authorization: { + params: { + prompt: "consent", + access_type: "offline", + response_type: "code", + }, + }, + }), ], adapter: DrizzleAdapter(db, { usersTable: users, @@ -64,4 +64,5 @@ export const authConfig = { }, }), }, + debug: true, } satisfies NextAuthConfig; diff --git a/start-database.sh b/start-database.sh index 398c1c6..6c41344 100644 --- a/start-database.sh +++ b/start-database.sh @@ -9,41 +9,38 @@ # On Linux and macOS you can run this script directly - `./start-database.sh` -DB_CONTAINER_NAME="ls-postgres" +COMPOSE_FILE="docker-compose.yml" +DB_CONTAINER_NAME="lf-postgres" +COMPOSE_SERVICE_NAME="postgres" -if ! [ -x "$(command -v docker)" ]; then - echo -e "Docker is not installed. Please install docker and try again.\nDocker install guide: https://docs.docker.com/engine/install/" +if ! [ -x "$(command -v docker-compose)" ]; then + echo -e "Docker Compose is not installed. Please install Docker Compose and try again.\nDocker Compose install guide: https://docs.docker.com/compose/install/" exit 1 fi -if ! docker info > /dev/null 2>&1; then - echo "Docker daemon is not running. Please start Docker and try again." - exit 1 -fi - -if [ "$(docker ps -q -f name=$DB_CONTAINER_NAME)" ]; then - echo "Database container '$DB_CONTAINER_NAME' already running" +if [ "$(docker-compose -f $COMPOSE_FILE ps -q $COMPOSE_SERVICE_NAME)" ]; then + echo "Database container '$DB_CONTAINER_NAME' already running via Docker Compose" exit 0 fi -if [ "$(docker ps -q -a -f name=$DB_CONTAINER_NAME)" ]; then - docker start "$DB_CONTAINER_NAME" - echo "Existing database container '$DB_CONTAINER_NAME' started" +# Check if the service exists but is not running +if [ "$(docker-compose -f $COMPOSE_FILE ps -a -q $COMPOSE_SERVICE_NAME)" ] && [ ! "$(docker-compose -f $COMPOSE_FILE ps -q $COMPOSE_SERVICE_NAME)" ]; then + docker-compose -f $COMPOSE_FILE start $COMPOSE_SERVICE_NAME + echo "Existing database container '$DB_CONTAINER_NAME' started via Docker Compose" exit 0 fi -# import env variables from .env +# Import env variables from .env set -a source .env -DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') -DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}') +DB_PASSWORD=$(echo "$POSTGRES_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') if [ "$DB_PASSWORD" = "password" ]; then echo "You are using the default database password" read -p "Should we generate a random password for you? [y/N]: " -r REPLY if ! [[ $REPLY =~ ^[Yy]$ ]]; then - echo "Please change the default password in the .env file and try again" + echo "Please set a password in the .env file and try again" exit 1 fi # Generate a random URL-safe password @@ -51,10 +48,7 @@ if [ "$DB_PASSWORD" = "password" ]; then sed -i -e "s#:password@#:$DB_PASSWORD@#" .env fi -docker run -d \ - --name $DB_CONTAINER_NAME \ - -e POSTGRES_USER="postgres" \ - -e POSTGRES_PASSWORD="$DB_PASSWORD" \ - -e POSTGRES_DB=d__dev_ls \ - -p "$DB_PORT":5432 \ - docker.io/postgres && echo "Database container '$DB_CONTAINER_NAME' was successfully created" +# Update the environment variables in the Docker Compose file if necessary +# Assuming environment variables in the docker-compose.yml file match those in .env + +docker-compose -f $COMPOSE_FILE up -d && echo "Database and proxy services were successfully created via Docker Compose"