Set up auth + corrected local db

This commit is contained in:
2024-11-09 18:41:41 -06:00
parent cb2e59e07e
commit 9c318b4451
6 changed files with 81 additions and 43 deletions

View File

@@ -0,0 +1,24 @@
"use client";
import type { FC, PropsWithChildren } from "react";
import { useSession, signIn, SessionProvider } from "next-auth/react";
export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
return (
<SessionProvider>
<AuthWrapper>{children}</AuthWrapper>
</SessionProvider>
);
};
const AuthWrapper: FC<PropsWithChildren> = ({ children }) => {
const session = useSession();
console.log("SESSION", session);
if (session.status === "unauthenticated") {
void signIn();
}
return children;
};

View File

@@ -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({
<html lang="en" className={`${GeistSans.variable}`}>
<body>
<TRPCReactProvider>
<HydrateClient>{children}</HydrateClient>
<HydrateClient>
<AuthProvider>{children}</AuthProvider>
</HydrateClient>
</TRPCReactProvider>
</body>
</html>

View File

@@ -1,3 +1,19 @@
import { auth } from "~/server/auth";
export default async function Home() {
return <h1>ls</h1>;
const session = await auth();
if (!session) {
<main>
<h1>Not signed in</h1>
</main>;
}
return (
<main>
<h1>ls</h1>
<pre>
<code>{JSON.stringify(session, undefined, 2)}</code>
</pre>
</main>
);
}

View File

@@ -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,
},

View File

@@ -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;

View File

@@ -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"