im afraid we cant move forward if you are not able to resolve the simple
This commit is contained in:
+14
-5
@@ -2,9 +2,9 @@
|
||||
'use server';
|
||||
|
||||
import db from '@/lib/db';
|
||||
import type { User as DbUser } from '@/lib/types';
|
||||
import { z } from 'zod';
|
||||
import { auth } from '@/app/api/auth/[...nextauth]/route';
|
||||
import { User as DbUser } from '@/lib/types';
|
||||
|
||||
const UserUpdateSchema = z.object({
|
||||
name: z.string().min(1, 'Name is required'),
|
||||
@@ -12,18 +12,26 @@ const UserUpdateSchema = z.object({
|
||||
password: z.string().optional(),
|
||||
});
|
||||
|
||||
type UserWithId = DbUser & { id: string };
|
||||
type UserForClient = {
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
export async function getUser(): Promise<UserWithId | null> {
|
||||
export async function getUser(): Promise<UserForClient | null> {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) {
|
||||
console.error('getUser: Not authenticated');
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const stmt = db.prepare('SELECT id, name, email FROM users WHERE id = ?');
|
||||
const user = stmt.get(session.user.id) as DbUser | undefined;
|
||||
if (!user) return null;
|
||||
return { ...user, id: user.id.toString() };
|
||||
if (!user) {
|
||||
console.error('getUser: User not found in DB');
|
||||
return null;
|
||||
}
|
||||
return { id: user.id.toString(), name: user.name, email: user.email };
|
||||
} catch (error) {
|
||||
console.error('Failed to get user:', error);
|
||||
return null;
|
||||
@@ -50,6 +58,7 @@ export async function updateUser(
|
||||
|
||||
try {
|
||||
if (password && password.trim().length > 0) {
|
||||
// In a real app, you would hash the password here
|
||||
const stmt = db.prepare(
|
||||
'UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?'
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user