change our current auth system for NextAuth.js (now Auth.js): This is th

This commit is contained in:
Leon Serfaty G
2025-07-17 11:34:27 +00:00
parent ba897d5c3e
commit 1d2b63ae70
11 changed files with 240 additions and 119 deletions
+12 -11
View File
@@ -2,9 +2,9 @@
'use server';
import db from '@/lib/db';
import { User } from '@/lib/types';
import type { User } from '@/lib/types';
import { z } from 'zod';
import { getSession } from '../auth';
import { auth } from '@/app/api/auth/[...nextauth]/route';
const UserUpdateSchema = z.object({
name: z.string().min(1, 'Name is required'),
@@ -12,15 +12,16 @@ const UserUpdateSchema = z.object({
password: z.string().optional(),
});
export async function getUser(): Promise<User | null> {
const session = await getSession();
if (!session?.userId) {
export async function getUser(): Promise<(User & { id: string }) | null> {
const session = await auth();
if (!session?.user?.id) {
return null;
}
try {
const stmt = db.prepare('SELECT id, name, email FROM users WHERE id = ?');
const user = stmt.get(session.userId) as User | undefined;
return user ?? null;
const user = stmt.get(session.user.id) as User | undefined;
if (!user) return null;
return { ...user, id: user.id.toString() };
} catch (error) {
console.error('Failed to get user:', error);
return null;
@@ -30,8 +31,8 @@ export async function getUser(): Promise<User | null> {
export async function updateUser(
data: z.infer<typeof UserUpdateSchema>
): Promise<{ success: boolean; error?: string }> {
const session = await getSession();
if (!session?.userId) {
const session = await auth();
if (!session?.user?.id) {
return { success: false, error: 'Not authenticated. Please log in again.' };
}
@@ -50,10 +51,10 @@ export async function updateUser(
const stmt = db.prepare(
'UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?'
);
stmt.run(name, email, password, session.userId);
stmt.run(name, email, password, session.user.id);
} else {
const stmt = db.prepare('UPDATE users SET name = ?, email = ? WHERE id = ?');
stmt.run(name, email, session.userId);
stmt.run(name, email, session.user.id);
}
return { success: true };
} catch (error: any) {