committing summative
This commit is contained in:
1 parent
73ac8e85be
commit
95b138f29e
44 files changed
+5319
-755
No files matched your search
@@ -0,0 +1,14 @@
|
||||
import bcrypt from "bcryptjs";
|
||||
|
||||
const SALT_ROUNDS = 10;
|
||||
|
||||
export async function hashPassword(password: string): Promise<string> { // hashes and salts password
|
||||
return bcrypt.hash(password, SALT_ROUNDS)
|
||||
}
|
||||
|
||||
export async function verifyPassword( // compares password to hash
|
||||
password: string,
|
||||
hash: string
|
||||
): Promise<boolean> {
|
||||
return bcrypt.compare(password, hash);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { useCallback } from "react";
|
||||
|
||||
interface User {
|
||||
id: string | null | undefined;
|
||||
email: string | null | undefined;
|
||||
name: string | null | undefined;
|
||||
}
|
||||
|
||||
interface LoginCredentials {
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface RegisterCredentials extends LoginCredentials {
|
||||
name?: string;
|
||||
}
|
||||
|
||||
interface AuthResponse {
|
||||
user: User;
|
||||
message?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export const register = async (credentials: RegisterCredentials): Promise<AuthResponse> => {
|
||||
const response = await fetch("/api/auth/register", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(credentials), // turns credentials obj into json
|
||||
credentials: "include"
|
||||
});
|
||||
|
||||
const data: AuthResponse = await response.json(); // gets response data from promise and turns response into object
|
||||
|
||||
if (!response.ok) { // boolean value depending on success code
|
||||
throw new Error(data.error || "Registration failed");
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { PrismaClient } from '@/lib/generated/prisma/client';
|
||||
import { PrismaPg } from "@prisma/adapter-pg";
|
||||
import { Pool } from "pg";
|
||||
|
||||
const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined;
|
||||
};
|
||||
|
||||
function createPrismaClient() {
|
||||
const pool = new Pool({ connectionString:
|
||||
process.env.DATABASE_URL });
|
||||
const adapter = new PrismaPg(pool);
|
||||
return new PrismaClient({ adapter });
|
||||
}
|
||||
|
||||
export const prisma = globalForPrisma.prisma ?? createPrismaClient();
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
globalForPrisma.prisma = prisma;
|
||||
}
|
||||
Reference in new issue
Block a user