Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

delete profile and user (DRAFT IN PROGRESS) #9837

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
63 changes: 57 additions & 6 deletions pages/account/onboarding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { authOptions } from "../../api/auth/[...nextauth]";
import { getServerSession } from "next-auth/next";
import { useRouter } from "next/router";
import { useSession } from "next-auth/react";
import { useState } from "react";
import {
FaGithub,
FaLink,
Expand All @@ -10,15 +11,18 @@ import {
FaTent,
FaCertificate,
FaArrowUpRightFromSquare,
FaTriangleExclamation,
} from "react-icons/fa6";

import { clientEnv } from "@config/schemas/clientSchema";
import logger from "@config/logger";
import PageHead from "@components/PageHead";
import Page from "@components/Page";
import { getUserApi } from "pages/api/profiles/[username]";
import { PROJECT_NAME } from "@constants/index";
import Card from "@components/Card";
import Button from "@components/Button";
import Modal from "@components/Modal";
import Navigation from "@components/account/manage/Navigation";
import ProgressBar from "@components/statistics/ProgressBar";
import Alert from "@components/Alert";
Expand Down Expand Up @@ -46,6 +50,7 @@ export async function getServerSideProps(context) {
"testimonials",
"events",
"repos",
"delete"
];
let progress = {
percentage: 0,
Expand All @@ -62,11 +67,11 @@ export async function getServerSideProps(context) {
).toFixed(0);

return {
props: { profile, progress },
props: { profile, progress, BASE_URL: clientEnv.NEXT_PUBLIC_BASE_URL },
};
}

export default function Onboarding({ profile, progress }) {
export default function Onboarding({ profile, progress, BASE_URL }) {
const router = useRouter();
const { data: session } = useSession();
if (typeof window !== "undefined" && window.localStorage) {
Expand All @@ -82,6 +87,7 @@ export default function Onboarding({ profile, progress }) {
router.push("/api/stripe");
}
}
const [showModal, setShowModal] = useState(false);

const cards = [
{
Expand Down Expand Up @@ -144,12 +150,32 @@ export default function Onboarding({ profile, progress }) {
},
isEdit: profile.milestones && profile.milestones.length > 0,
},
{
icon: FaTriangleExclamation,
title: "Delete Account",
description: "Delete your account",
button: {
name: "Delete Account",
},
click: true,
},
];

const alerts = {
premium: "You are now a premium user!",
cancel: "You cancelled your subscription.",
};

//TODO move this?
const deleteLinks = async () => {
await fetch(`${BASE_URL}/api/account/manage/delete/${profile.username}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
});
}

return (
<>
<PageHead
Expand Down Expand Up @@ -198,17 +224,42 @@ export default function Onboarding({ profile, progress }) {
>
{cards.map((card) => (
<li key={card.title}>
{card.click ?
<div className="flex flex-col items-center border-2 h-[14rem] overflow-hidden rounded-lg shadow-lg transition duration-350 p-4 gap-3 duration-500 ease-in-out hover:border-tertiary-medium border-tertiary-medium">
<div className="flex gap-4 content-center items-center justify-items-start grow">
<card.icon className="h-16 w-16" />
<h2 className="text-xl font-bold">{card.title}</h2>
</div>
<p>{card.description}</p>
<Button onClick={() => setShowModal(true)}>Delete Account</Button>

<Modal
show={showModal}
setShow={setShowModal}
>
{/* TODO: on click, show a indicator deltion is happening - success message after */}
<div className="mt-10 flex flex-col sm:flex-row items-center justify-center gap-6 sm:mx-24">
<Button onClick={() => deleteLinks()}>
Delete my Account
</Button>
<Button onClick={() => setShowModal(false)}>Go Back</Button>
</div>
<p className="mx-auto mt-6 max-w-xl text-lg leading-8 text-center text-primary-medium dark:text-primary-low mb-4">Warning! This cannot be reversed once you click Delete.</p>
</Modal>

</div>
:
<Card href={card.button.href} active={!card.isEdit}>
<div className="flex gap-4 content-center items-center justify-items-start grow">
<card.icon className="h-16 w-16" />
<h2 className="text-xl font-bold">{card.title}</h2>
</div>
<p>{card.description}</p>
{card.isEdit && <Button>Edit</Button>}
{!card.isEdit && (
<Button primary={true}>+ {card.button.name}</Button>
)}
{card.isEdit ? <Button>Edit</Button>
: <Button primary={true}>+ {card.button.name}</Button>
}
</Card>
}
</li>
))}
</ul>
Expand Down
71 changes: 71 additions & 0 deletions pages/api/account/manage/delete/[[...data]].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { authOptions } from "../../../auth/[...nextauth]";
import { getServerSession } from "next-auth/next";

import connectMongo from "@config/mongo";
//import logger from "@config/logger";
import { Link } from "@models/index";
import fs from 'fs';

export default async function handler(req, res) {
const session = await getServerSession(req, res, authOptions);
const username = session.username;

if (!["DELETE"].includes(req.method)) {
return res.status(400).json({
error: "Invalid request: DELETE required",
});
}

const { data } = req.query;
const context = { req, res };

let link = {};

if (req.method === "DELETE") {
link = await deleteLinkApi(context, username, data[0]);
}

if (link.error) {
return res.status(400).json({ message: link.error });
}

return res.status(200).json(link);
}


export async function deleteLinkApi(context, username) {
await connectMongo();
// const log = logger.child({ username });

// delete link
try {
await Link.deleteMany({ username: username });
} catch (e) {
const error = `failed to delete links from profile for username: ${username}`;
//log.error(e, error);
return { error };
}
checkJsonProfile(username)
return JSON.parse(JSON.stringify({}));
}

//check for json version, delete if exists
async function checkJsonProfile(username){
if(fs.existsSync(`./data/${username}.json`)){
try {

console.log("found file" + username)
fs.unlinkSync(`./data/${username}.json`)

} catch (e) {
const error = `failed to delete json profile for username: ${username}`;
//log.error(e, error);
return { error };
}

return JSON.parse(JSON.stringify({}));
}else{
console.log(`no json profile`)
return JSON.parse(JSON.stringify({}));
}
}
Loading