mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-05-24 17:01:09 -04:00
Refactor lib
This commit is contained in:
89
lib/server/auth.ts
Normal file
89
lib/server/auth.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import type {NextAuthOptions} from "next-auth";
|
||||
import {getServerSession} from "next-auth";
|
||||
import {PrismaAdapter} from "@auth/prisma-adapter";
|
||||
import {prisma} from "@/lib/server/prisma";
|
||||
import GoogleProvider from "next-auth/providers/google";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import bcrypt from "bcryptjs";
|
||||
|
||||
export const authOptions: NextAuthOptions = {
|
||||
adapter: PrismaAdapter(prisma),
|
||||
session: {
|
||||
strategy: "jwt",
|
||||
},
|
||||
providers: [
|
||||
GoogleProvider({
|
||||
clientId: process.env.GOOGLE_CLIENT_ID!,
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
||||
}),
|
||||
CredentialsProvider({
|
||||
name: "credentials",
|
||||
credentials: {
|
||||
email: {label: "Email", type: "email"},
|
||||
password: {label: "Password", type: "password"},
|
||||
},
|
||||
async authorize(credentials) {
|
||||
if (!credentials?.email || !credentials?.password) {
|
||||
throw new Error("Email and password are required");
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {email: credentials.email},
|
||||
});
|
||||
|
||||
if (!user || !user.password) {
|
||||
throw new Error("Invalid email or password");
|
||||
}
|
||||
|
||||
const isCorrectPassword = await bcrypt.compare(
|
||||
credentials.password,
|
||||
user.password
|
||||
);
|
||||
|
||||
if (!isCorrectPassword) {
|
||||
throw new Error("Invalid email or password");
|
||||
}
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
image: user.image,
|
||||
};
|
||||
},
|
||||
}),
|
||||
],
|
||||
pages: {
|
||||
signIn: "/login",
|
||||
error: "/login",
|
||||
},
|
||||
callbacks: {
|
||||
async jwt({token, user}) {
|
||||
if (user) {
|
||||
token.id = user.id;
|
||||
token.email = user.email;
|
||||
token.name = user.name;
|
||||
token.picture = user.image;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
async session({session, token}) {
|
||||
if (token && session.user) {
|
||||
session.user.id = token.id as string;
|
||||
session.user.name = token.name as string;
|
||||
session.user.email = token.email as string;
|
||||
session.user.image = token.picture as string;
|
||||
}
|
||||
return session;
|
||||
},
|
||||
async redirect({url, baseUrl}) {
|
||||
if (url.startsWith("/")) return `${baseUrl}${url}`;
|
||||
else if (new URL(url).origin === baseUrl) return url;
|
||||
return baseUrl;
|
||||
},
|
||||
},
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
debug: process.env.NODE_ENV === "development",
|
||||
} satisfies NextAuthOptions;
|
||||
|
||||
export const getSession = () => getServerSession(authOptions);
|
||||
14
lib/server/db-utils.ts
Normal file
14
lib/server/db-utils.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
"use server";
|
||||
|
||||
import {prisma} from "@/lib/server/prisma";
|
||||
|
||||
|
||||
export async function checkUserTableExists(): Promise<boolean> {
|
||||
try {
|
||||
await prisma.user.findFirst();
|
||||
return true;
|
||||
} catch {
|
||||
// If there's an error, the table likely doesn't exist
|
||||
return false;
|
||||
}
|
||||
}
|
||||
12
lib/server/prisma.ts
Normal file
12
lib/server/prisma.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { withAccelerate } from '@prisma/extension-accelerate'
|
||||
|
||||
const globalForPrisma = global as unknown as { prisma: PrismaClient };
|
||||
|
||||
export const prisma =
|
||||
globalForPrisma.prisma ||
|
||||
new PrismaClient({
|
||||
log: ["query"],
|
||||
}).$extends(withAccelerate());
|
||||
|
||||
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
||||
8
lib/server/supabase.ts
Normal file
8
lib/server/supabase.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
"use server";
|
||||
|
||||
// import { createClient } from '@supabase/supabase-js';
|
||||
//
|
||||
// export const supabase = createClient(
|
||||
// process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
// process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
|
||||
// );
|
||||
Reference in New Issue
Block a user