44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import bcrypt from 'bcryptjs';
|
|
import jwt from 'jsonwebtoken';
|
|
import { cookies } from 'next/headers';
|
|
import prisma from './prisma';
|
|
|
|
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key-change-in-production';
|
|
|
|
export async function hashPassword(password: string): Promise<string> {
|
|
return bcrypt.hash(password, 10);
|
|
}
|
|
|
|
export async function verifyPassword(password: string, hash: string): Promise<boolean> {
|
|
return bcrypt.compare(password, hash);
|
|
}
|
|
|
|
export function generateToken(userId: string): string {
|
|
return jwt.sign({ userId }, JWT_SECRET, { expiresIn: '7d' });
|
|
}
|
|
|
|
export function verifyToken(token: string): { userId: string } | null {
|
|
try {
|
|
return jwt.verify(token, JWT_SECRET) as { userId: string };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function getCurrentUser() {
|
|
const cookieStore = await cookies();
|
|
const token = cookieStore.get('auth-token')?.value;
|
|
|
|
if (!token) return null;
|
|
|
|
const payload = verifyToken(token);
|
|
if (!payload) return null;
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { id: payload.userId },
|
|
select: { id: true, email: true, name: true, createdAt: true }
|
|
});
|
|
|
|
return user;
|
|
}
|