punyakrit / social-share

ShareHub is a Project where users can create their public viewable profile containing their customizable social links and important links that they want to share with the world. Also having option to see their page Analytics
https://www.sharehub.xyz/
MIT License
18 stars 38 forks source link

[Bug]: Improve Error Handling and Type Checking in User Profile Update Functions #126

Closed Swapnilden closed 2 weeks ago

Swapnilden commented 2 weeks ago

What happened?

The current code for updating user profiles, saving social links, and page links lacks error handling and type checking. This can lead to silent failures and potential runtime errors.

Updated Code

Here's the updated code with improved error handling, type checking, and more detailed return messages:

"use server";
import { authOptions } from "@/lib/authOptions";
import connectMongoDb from "@/lib/dbConnect";
import { UserPage } from "@/models/Onboarding";
import { getServerSession } from "next-auth";

export async function UserProfile(formData: FormData) {
  try {
    await connectMongoDb();
    const session = await getServerSession(authOptions);

    if (!session) {
      throw new Error("User is not authenticated");
    }

    const name = formData.get("displayName")?.toString() || "";
    const location = formData.get("location")?.toString() || "";
    const bio = formData.get("bio")?.toString() || "";
    const bgType = formData.get("bgType")?.toString() || "";
    const bgColor = formData.get("bgColor")?.toString() || "";
    const bgImage = formData.get("bgImage")?.toString() || "";
    const avatarImage = formData.get("avatarImage")?.toString() || "";

    await UserPage.updateOne(
      { owner: session.user.email },
      { displayName: name, location: location, bio: bio, bgType: bgType, bgColor: bgColor, bgImage: bgImage, avatarImage: avatarImage }
    );
    return { success: true, message: "User profile updated successfully" };
  } catch (error) {
    console.error("Error updating user profile:", error);
    return { success: false, message: error.message };
  }
}

export async function saveSocials(formData: FormData){
  try {
    await connectMongoDb();
    const session = await getServerSession(authOptions);

    if (!session) {
      throw new Error("User is not authenticated");
    }

    const buttonValues: Record<string, string> = {};
    formData.forEach((value, key) => {
      buttonValues[key] = value.toString();
    });

    await UserPage.updateOne(
      { owner: session.user.email },
      { button:  buttonValues }
    );
    return { success: true, message: "Social links updated successfully" };
  } catch (error) {
    console.error("Error updating social links:", error);
    return { success: false, message: error.message };
  }
} 

export async function savePageLinks(links: any) {
  try {
    await connectMongoDb();
    const session = await getServerSession(authOptions);

    if (!session) {
      throw new Error("User is not authenticated");
    }

    await UserPage.updateOne(
      { owner: session.user.email },
      { links }
    );
    return { success: true, message: "Page links updated successfully" };
  } catch (error) {
    console.error("Error updating page links:", error);
    return { success: false, message: error.message };
  }
}

By implementing these changes, the functionality, security, and user experience of the application will be significantly enhanced. Please assign me this issue.

Record