import { signOut } from "next-auth/react"; import { ChangeEvent, Key, useContext, useState } from "react"; import Menu from "../components/Menu"; import Head from "next/head"; import { Button, Icon, InputLabel, MenuItem, Select, SelectChangeEvent, TextField, useTheme, } from "@mui/material"; import { GetServerSideProps, GetServerSidePropsContext, InferGetServerSidePropsType, } from "next"; import { Session, getServerSession } from "next-auth"; import { NotifyContext, NotifyType, TranslateContext, UserContext, } from "../Modules/context"; import { getProviders, Providers } from "../types/providers"; import db from "../Modules/database"; import { useRouter } from "next/router"; import { authOptions } from "#/pages/api/auth/[...nextauth]"; import { MUIThemeCodetoJSONString } from "#/components/theme"; import Link from "#components/Link"; import { getData } from "./api/theme"; interface ProviderProps { provider: Providers; notify: NotifyType; user: Session["user"]; } // Shows the ways to connect and disconnect from a provider function ProviderShow({ provider, notify, user }: ProviderProps) { const t = useContext(TranslateContext); const [email, setEmail] = useState(user[provider]); const [input, setInput] = useState(""); function handleInputChange( e: ChangeEvent, ) { setInput(e.target.value); } // Used to connect to the provider function connect() { notify(t("Connecting to {provider}", { provider })); fetch(`/api/user`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ provider, email: input, }), }).then(async (response) => { notify( t(await response.text(), { provider }), response.ok ? "success" : "error", ); setEmail(input); }); } // Used to disconnect from the provider function disconnect() { notify(t("Disconnecting from {provider}", { provider })); fetch(`/api/user`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ provider, email: "", }), }).then(async (response) => { notify( t(await response.text(), { provider }), response.ok ? "success" : "error", ); setInput(""); setEmail(""); }); } // Checks if connected or not if (email === "") { return ( <>

); } else { return ( <>

); } } // A place to change your username and other settings export default function Home({ user, providers, setColorMode, deleteable, }: InferGetServerSidePropsType) { const t = useContext(TranslateContext); const [getUser] = useContext(UserContext); const [username, setUsername] = useState(user.username); const [password, setPassword] = useState(""); const [passwordExists, setPasswordExists] = useState(user.password); const [customTheme, setCustomTheme] = useState(""); const theme = useTheme(); const notify = useContext(NotifyContext); const oppositeColor = theme.palette.mode === "dark" ? "light" : "dark"; // Alternates the color mode function alternateColorMode() { setColorMode(oppositeColor, true); localStorage.theme = oppositeColor; } // Resets the color mode function resetColorMode() { setColorMode(theme.palette.mode, true); localStorage.theme = theme.palette.mode; } // Used to change the users username function changeUsername() { notify(t("Saving")); fetch(`/api/user`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ username, }), }).then(async (response) => { notify(t(await response.text()), response.ok ? "success" : "error"); // Makes sure to update the username getUser(user.id, true); }); } // Used to change the users password function changePassword() { notify(t("Saving")); fetch(`/api/user`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ password: password, }), }).then(async (response) => { notify(t(await response.text()), response.ok ? "success" : "error"); setPasswordExists(password !== ""); }); } // Used to change the locale function changeLocale(event: SelectChangeEvent) { const locale = event.target.value; notify(t("Saving")); fetch("/api/user", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ locale: locale, }), }).then(async (response) => { notify(t(await response.text()), response.ok ? "success" : "error"); localStorage.locale = locale; const event = new Event("visibilitychange"); document.dispatchEvent(event); }); } // Used to delete the user const router = useRouter(); function deleteUser() { notify(t("Deleting user")); fetch(`/api/user`, { method: "DELETE", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user: user.id, }), }).then(async (response) => { notify(t(await response.text()), response.ok ? "success" : "error"); signOut(); }); } function saveCustomTheme() { setColorMode(customTheme, true); localStorage.theme = customTheme; } // Parses custom theme when pasted if (customTheme.includes("import")) { setCustomTheme(MUIThemeCodetoJSONString(customTheme)); } return ( <> {t("Usermenu")}

{t("Usermenu")}

Your userID is {user.id}.

{t("Language")}

{ // Used to change the username setUsername(e.target.value); }} value={username} />

{t("Password Auth is currently {enabled}. ", { enabled: passwordExists ? t("enabled") : t("disabled"), })} {t( "It is recommended against using password authorization unless strictly necessary. ", )}

{ setPassword(e.target.value); }} /> {!deleteable && (

{t("You can not be in any leagues if you want to delete your user. ")}

)} {deleteable && ( <>

)}

{t("OAuth Providers")}

{providers.map((provider: Providers) => ( ))}

{t("Advanced Customization")}

{t( "You can customize the theme here, but note that this is only for advanced users. ", )} {t("First you will want to go to the MUI Theme Creator. ")} {t( "There you can customize the theme using their UI, and copy the code to paste it below. Note that clicking the switch to light/dark mode button will reset your theme and that this textbox is cleared on page refresh. ", )}

setCustomTheme(e.target.value)} multiline fullWidth >
); } // Returns all the user data if logged in and if not logged in redirects to the login page export const getServerSideProps: GetServerSideProps = async ( ctx: GetServerSidePropsContext, ) => { const session = await getServerSession(ctx.req, ctx.res, authOptions); if (session) { const user = session.user; // Checks if the user is in any leagues const anyLeagues = (await db .selectFrom("leagueUsers") .select("user") .where("user", "=", user.id) .executeTakeFirst()) !== undefined; // Checks what providers are supported return { props: { user, providers: getProviders(), deleteable: !anyLeagues, t: await getData(ctx), }, }; } else { return { redirect: { destination: `/api/auth/signin?callbackUrl=${encodeURIComponent( ctx.resolvedUrl, )}`, permanent: false, }, }; } };