import { type GetServerSideProps, type NextPage } from "next";
import Head from "next/head";
import Image from "next/image";
import { signIn, useSession } from "next-auth/react";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { FaShieldAlt } from "react-icons/fa";
import { SiGithub, SiGoogle } from "react-icons/si";
import { api } from "~/utils/api";
import { getServerAuthSession } from "../../server/auth";
import { GiStarFormation } from "react-icons/gi";
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getServerAuthSession(ctx);
// Redirect to login if not signed in
if (!session) {
return {
redirect: {
destination: `/api/auth/signin?callbackUrl=${ctx.resolvedUrl}`,
permanent: false,
},
};
}
// Return session if logged in
return {
props: { session },
};
};
const Profile: NextPage = () => {
return (
<>
Sprint Padawan - Profile
>
);
};
export default Profile;
const ProfileBody = () => {
const { data: sessionData } = useSession();
const [nameText, setNameText] = useState("");
const router = useRouter();
const { data: providers, isLoading: providersLoading } =
api.user.getProviders.useQuery();
const deleteUserMutation = api.user.delete.useMutation({});
const saveUserMutation = api.user.save.useMutation({});
const deleteCurrentUser = async () => {
await deleteUserMutation.mutateAsync();
(document.querySelector("#delete-user-modal") as HTMLInputElement).checked =
false;
router.reload();
};
const saveUser = async () => {
await saveUserMutation.mutateAsync({
name: nameText,
});
router.reload();
};
useEffect(() => {
setNameText(sessionData?.user.name || "");
}, [sessionData]);
if (sessionData) {
return (
<>
✕
This action will delete ALL data associated with your account. The
same GitHub Account can be used, but none of your existing data
will be available. If you are sure, please confirm below:
void deleteCurrentUser()}
>
I am sure!
Profile:
{sessionData.user.image && (
)}
{sessionData.user.isAdmin && (
)}
{sessionData.user.isVIP && (
)}
{providersLoading ? (
{" "}
) : (
void signIn("github")}
>
void signIn("google")}
>
)}
{sessionData.user.name && (
setNameText(event.target.value)}
/>
)}
{sessionData.user.email && (
)}
void saveUser()}
className="btn btn-secondary"
>
Save Account
{/*
Delete Account */}
Delete Account
>
);
} else {
return Error getting login session! ;
}
};