Skip to content

Commit c9557e2

Browse files
committed
feature/6 - working on connecting next-auth with prisma
1 parent cb6d58f commit c9557e2

File tree

9 files changed

+429
-80
lines changed

9 files changed

+429
-80
lines changed

.env.example

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
DATABASE_URL="postgresql://postgres:example@localhost:5432/postgres"
22

33
# Next Auth Discord Provider
4-
GITHUB_CLIENT_ID=""
5-
GITHUB_CLIENT_SECRET=""
4+
GITHUB_CLIENT_ID="63b03dd1a029181eede8"
5+
GITHUB_CLIENT_SECRET="fe61fec0056b571d01d0a1e25945904237d164e8"
66

77
NEXTAUTH_SECRET="my_ultra_secure_nextauth_secret"
88
NEXTAUTH_URL="http://localhost:3000"

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: "3.9"
22
services:
3-
bg-db:
3+
code-racer-db:
44
image: postgres
55
restart: always
66
container_name: code-racer-postgres

package-lock.json

+333-35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12+
"@auth/prisma-adapter": "^1.0.0",
13+
"@next-auth/prisma-adapter": "^1.0.7",
1214
"@prisma/client": "^4.16.2",
1315
"@radix-ui/react-slot": "^1.0.2",
14-
"@types/node": "20.4.1",
15-
"@types/react": "18.2.14",
16-
"@types/react-dom": "18.2.6",
1716
"autoprefixer": "10.4.14",
1817
"class-variance-authority": "^0.6.1",
1918
"clsx": "^1.2.1",
20-
"eslint": "8.44.0",
21-
"eslint-config-next": "13.4.9",
2219
"lucide-react": "^0.259.0",
2320
"next": "13.4.9",
2421
"next-auth": "^4.22.1",
@@ -31,6 +28,11 @@
3128
"typescript": "5.1.6"
3229
},
3330
"devDependencies": {
34-
"prisma": "^4.16.2"
31+
"prisma": "^4.16.2",
32+
"@types/node": "20.4.1",
33+
"@types/react": "18.2.14",
34+
"@types/react-dom": "18.2.6",
35+
"eslint": "8.44.0",
36+
"eslint-config-next": "13.4.9"
3537
}
3638
}

prisma/schema.prisma

+43-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,47 @@ datasource db {
88
}
99

1010
model User {
11-
id Int @id @default(autoincrement())
12-
createdAt DateTime @default(now())
13-
name String?
11+
id String @id @default(cuid())
12+
createdAt DateTime @default(now())
13+
name String?
14+
email String? @unique
15+
emailVerified DateTime?
16+
image String?
17+
accounts Account[]
18+
sessions Session[]
19+
}
20+
21+
// Necessary for Next auth
22+
model Account {
23+
id String @id @default(cuid())
24+
userId String
25+
type String
26+
provider String
27+
providerAccountId String
28+
refresh_token String? @db.Text
29+
access_token String? @db.Text
30+
expires_at Int?
31+
token_type String?
32+
scope String?
33+
id_token String? @db.Text
34+
session_state String?
35+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
36+
37+
@@unique([provider, providerAccountId])
38+
}
39+
40+
model Session {
41+
id String @id @default(cuid())
42+
sessionToken String @unique
43+
userId String
44+
expires DateTime
45+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
46+
}
47+
48+
model VerificationToken {
49+
identifier String
50+
token String @unique
51+
expires DateTime
52+
53+
@@unique([identifier, token])
1454
}
+7-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
import { prisma } from "@/lib/prisma";
2+
import { PrismaAdapter } from "@next-auth/prisma-adapter";
13
import NextAuth from "next-auth";
24
import GithubProvider from "next-auth/providers/github";
35

4-
const handler = NextAuth({
6+
export const nextAuthOptions = {
7+
adapter: PrismaAdapter(prisma),
58
providers: [
69
GithubProvider({
710
clientId: process.env.GITHUB_CLIENT_ID!,
811
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
912
}),
1013
],
11-
});
14+
};
15+
16+
const handler = NextAuth(nextAuthOptions);
1217

1318
export { handler as GET, handler as POST };

src/app/page.tsx

+24-26
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
import { Button } from "@/components/ui/button";
22
import { LoginButton, LogoutButton } from "@/components/ui/buttons";
3+
import { getSession } from "@/lib/getSession";
34
import { prisma } from "@/lib/prisma";
4-
import { getServerSession } from "next-auth";
55
import Image from "next/image";
66

77
export default async function Home() {
8-
const users = await prisma.user.findMany();
9-
console.log(users);
10-
const session = await getServerSession();
11-
return (
12-
<main className="flex min-h-screen flex-col items-center justify-between p-24">
13-
<Button>Button</Button>
14-
{session ? (
15-
<div className="flex space-x-2 items-center">
16-
<Image
17-
className="rounded-full"
18-
src={session?.user?.image!}
19-
alt="user avatar"
20-
height={40}
21-
width={40}
22-
/>
23-
<p className="text-gray-700 font-bold">
24-
{session?.user?.name}
25-
</p>
26-
<LogoutButton />
27-
</div>
28-
) : (
29-
<LoginButton />
30-
)}
31-
</main>
32-
);
8+
const users = await prisma.user.findMany();
9+
const session = await getSession();
10+
11+
return (
12+
<main className="flex min-h-screen flex-col items-center justify-between p-24">
13+
<Button>Button</Button>
14+
{session ? (
15+
<div className="flex space-x-2 items-center">
16+
<Image
17+
className="rounded-full"
18+
src={session?.user?.image!}
19+
alt="user avatar"
20+
height={40}
21+
width={40}
22+
/>
23+
<p className="text-gray-700 font-bold">{session?.user?.name}</p>
24+
<LogoutButton />
25+
</div>
26+
) : (
27+
<LoginButton />
28+
)}
29+
</main>
30+
);
3331
}

src/lib/getSession.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { nextAuthOptions } from "@/app/api/auth/[...nextauth]/route";
2+
import { getServerSession } from "next-auth";
3+
4+
export function getSession() {
5+
return getServerSession(nextAuthOptions);
6+
}

src/lib/prisma.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { PrismaClient } from '@prisma/client'
1+
import { PrismaClient } from "@prisma/client";
22

33
const globalForPrisma = globalThis as unknown as {
4-
prisma: PrismaClient | undefined
5-
}
4+
prisma: PrismaClient | undefined;
5+
};
66

7-
export const prisma = globalForPrisma.prisma ?? new PrismaClient()
7+
export const prisma = globalForPrisma.prisma ?? new PrismaClient();
88

9-
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
9+
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

0 commit comments

Comments
 (0)