Skip to content

Commit

Permalink
fix update profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
p0tat0chip committed Jan 9, 2025
1 parent 3364452 commit d1eaf76
Show file tree
Hide file tree
Showing 5 changed files with 563 additions and 427 deletions.
17 changes: 12 additions & 5 deletions Actions/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use server"
"use server";

import { signIn } from "@/auth";
import { GetUserByEmail } from "@/data/user";
Expand All @@ -25,17 +25,24 @@ export async function LoginUser(values: z.infer<typeof LoginSchema>) {
}

if (!existingUser.emailVerified) {
const verificationToken = await generateVerificationToken(existingUser.email);
await sendVerificationEmail(verificationToken.email,verificationToken.token);
return { error: "Email not verified! Please check your email for verification instructions." };
const verificationToken = await generateVerificationToken(
existingUser.email,
);
await sendVerificationEmail(
verificationToken.email,
verificationToken.token,
);
return {
error:
"Email not verified! Please check your email for verification instructions.",
};
}

await signIn("credentials", {
email: email,
password,
redirectTo: DEFAULT_LOGIN_REDIRECT,
});

} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
Expand Down
106 changes: 72 additions & 34 deletions app/api/user/route.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,107 @@
import { NextResponse } from 'next/server';
import { prisma } from '../../../lib/prisma';
import { NextResponse } from "next/server";
import { prisma } from "../../../lib/prisma";

interface User {
id: string;
name: string;
username: string;
email: string;
emailVerified: string | null;
image: string | null;
password: string;
bio: string | null;
role: string | null;
createdAt: string;
updatedAt: string;
resumeUrl: string | null;
linkedinUrl: string | null;
githubUrl: string | null;
skills: any[];
}

export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
const id = searchParams.get('id');
const email = searchParams.get('email');
const username = searchParams.get('username');
const id = searchParams.get("id");
const email = searchParams.get("email");
const username = searchParams.get("username");

try {
if (!id && !email && !username) {
return NextResponse.json({ message: 'Please provide an id, email, or username.' }, { status: 400 });
return NextResponse.json(
{ message: "Please provide an id, email, or username." },
{ status: 400 },
);
}


const conditions = [];
if (id) conditions.push({ id: id as string });
if (email) conditions.push({ email: email as string });
if (username) conditions.push({ username: username as string });


const user = await prisma.user.findFirst({
where: {
OR: conditions,
},
});

if (!user) {
return NextResponse.json({ error: 'User not found' }, { status: 404 });
return NextResponse.json({ error: "User not found" }, { status: 404 });
}

return NextResponse.json({ user: user }, { status: 200 });
} catch (error: any) {
console.error(error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 },
);
}
}


export async function POST(request: Request) {
try {
// Parse the request body
const body = await request.json();

const { id, ...userDetails } = body; // Extract id and the rest of the fields

// Check if the id is provided
if (!id) {
return NextResponse.json({ error: 'User ID is required' }, { status: 400 });
}

// If no other details are provided, return an error
if (Object.keys(userDetails).length === 0) {
return NextResponse.json({ error: 'No user details provided for update' }, { status: 400 });
// Parse the request body
const { user } = await request.json();
console.log(user);
// Check if the id is provided
if (!user.id) {
return NextResponse.json(
{ error: "User ID is required" },
{ status: 400 },
);
}
// Remove undefined values
Object.keys(user).forEach((key) => {
if (user[key] === undefined) {
delete user[key];
}
});
// If no other details are provided, return an error
if (Object.keys(user).length === 0) {
return NextResponse.json(
{ error: "No user details provided for update" },
{ status: 400 },
);
}

// Update the user in the database
const updatedUser = await prisma.user.update({
where: { id: id },
data: userDetails,
});
// Update the user in the database
const updatedUser = await prisma.user.update({
where: { id: user.id },
data: {
...user,
skills: {
set: user.skills || [],
},
},
});

// Return the updated user details
return NextResponse.json(updatedUser, { status: 200 });
// Return the updated user details
return NextResponse.json(updatedUser, { status: 200 });
} catch (error) {
console.error('Error updating user:', error);
return NextResponse.json({ error: 'Something went wrong' }, { status: 500 });
console.error("Error updating user:", error);
return NextResponse.json(
{ error: "Something went wrong" },
{ status: 500 },
);
}
}
}
Loading

0 comments on commit d1eaf76

Please sign in to comment.