Skip to content

Commit e9ed06d

Browse files
committed
Changes for 1.2.x
1 parent 6731696 commit e9ed06d

3 files changed

Lines changed: 99 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dist/
4747
downloads/
4848
eggs/
4949
.eggs/
50-
lib/
50+
/backend/lib/
5151
lib64/
5252
parts/
5353
sdist/

frontend/lib/auth.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { NextAuthOptions } from "next-auth"
2+
import { PrismaAdapter } from "@next-auth/prisma-adapter"
3+
import GoogleProvider from "next-auth/providers/google"
4+
import GitHubProvider from "next-auth/providers/github"
5+
import CredentialsProvider from "next-auth/providers/credentials"
6+
import bcrypt from "bcryptjs"
7+
import { prisma } from "./prisma"
8+
9+
declare module "next-auth" {
10+
interface Session {
11+
user: {
12+
id: string
13+
name?: string | null
14+
email?: string | null
15+
image?: string | null
16+
}
17+
}
18+
}
19+
20+
export const authOptions: NextAuthOptions = {
21+
adapter: PrismaAdapter(prisma),
22+
providers: [
23+
GoogleProvider({
24+
clientId: process.env.GOOGLE_CLIENT_ID!,
25+
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
26+
}),
27+
GitHubProvider({
28+
clientId: process.env.GITHUB_ID!,
29+
clientSecret: process.env.GITHUB_SECRET!,
30+
}),
31+
CredentialsProvider({
32+
name: "credentials",
33+
credentials: {
34+
email: { label: "Email", type: "email" },
35+
password: { label: "Password", type: "password" }
36+
},
37+
async authorize(credentials) {
38+
if (!credentials?.email || !credentials?.password) {
39+
return null
40+
}
41+
42+
const user = await prisma.user.findUnique({
43+
where: {
44+
email: credentials.email
45+
}
46+
})
47+
48+
if (!user || !user.password) {
49+
return null
50+
}
51+
52+
const isPasswordValid = await bcrypt.compare(
53+
credentials.password,
54+
user.password
55+
)
56+
57+
if (!isPasswordValid) {
58+
return null
59+
}
60+
61+
return {
62+
id: user.id,
63+
email: user.email,
64+
name: user.name,
65+
}
66+
}
67+
})
68+
],
69+
session: {
70+
strategy: "jwt"
71+
},
72+
pages: {
73+
signIn: "/auth/signin",
74+
},
75+
callbacks: {
76+
async jwt({ token, user }) {
77+
if (user) {
78+
token.id = user.id
79+
}
80+
return token
81+
},
82+
async session({ session, token }) {
83+
if (token) {
84+
session.user.id = token.id as string
85+
}
86+
return session
87+
},
88+
},
89+
}

frontend/lib/prisma.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { PrismaClient } from '@prisma/client'
2+
3+
const globalForPrisma = globalThis as unknown as {
4+
prisma: PrismaClient | undefined
5+
}
6+
7+
export const prisma = globalForPrisma.prisma ?? new PrismaClient()
8+
9+
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

0 commit comments

Comments
 (0)