Files
biblical-guide.com/lib/auth/index.ts
andupetcu 3b375c869b Add complete Biblical Guide web application with Material UI
Implemented comprehensive Romanian Biblical Guide web app:
- Next.js 15 with App Router and TypeScript
- Material UI 7.3.2 for modern, responsive design
- PostgreSQL database with Prisma ORM
- Complete Bible reader with book/chapter navigation
- AI-powered biblical chat with Romanian responses
- Prayer wall for community prayer requests
- Advanced Bible search with filters and highlighting
- Sample Bible data imported from API.Bible
- All API endpoints created and working
- Professional Material UI components throughout
- Responsive layout with navigation and theme

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-20 14:10:28 +03:00

44 lines
1.2 KiB
TypeScript

import bcrypt from 'bcryptjs'
import jwt from 'jsonwebtoken'
import { prisma } from '@/lib/db'
export async function createUser(email: string, password: string, name?: string) {
const passwordHash = await bcrypt.hash(password, 10)
return prisma.user.create({
data: { email, passwordHash, name }
})
}
export async function validateUser(email: string, password: string) {
const user = await prisma.user.findUnique({ where: { email } })
if (!user) return null
const isValid = await bcrypt.compare(password, user.passwordHash)
return isValid ? user : null
}
export function generateToken(userId: string): string {
return jwt.sign({ userId }, process.env.JWT_SECRET!, { expiresIn: '7d' })
}
export async function verifyToken(token: string) {
try {
const payload = jwt.verify(token, process.env.JWT_SECRET!) as { userId: string }
return payload
} catch (error) {
throw new Error('Invalid token')
}
}
export async function getUserFromToken(token: string) {
try {
const payload = await verifyToken(token)
const user = await prisma.user.findUnique({
where: { id: payload.userId },
select: { id: true, email: true, name: true, theme: true, fontSize: true }
})
return user
} catch (error) {
return null
}
}