Set up auth + corrected local db
This commit is contained in:
24
src/app/_components/auth-provider.tsx
Normal file
24
src/app/_components/auth-provider.tsx
Normal 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;
|
||||
};
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user