diff --git a/app/profile/page.tsx b/app/profile/page.tsx index a432b4c9..3fe2758e 100644 --- a/app/profile/page.tsx +++ b/app/profile/page.tsx @@ -12,7 +12,9 @@ import { loadAdmissionRSVP, loadProfile, loadQRCode, - updateProfile + updateProfile, + getUserInfo, + webSignOutUser } from "@/util/api"; import Loading from "@/components/Loading/Loading"; import ErrorSnackbar from "@/components/ErrorSnackbar/ErrorSnackbar"; @@ -46,6 +48,8 @@ export default function Profile() { const [showQR, setShowQR] = useState(false); const [qrInfo, setQrInfo] = useState(""); const [qrLoading, setQrLoading] = useState(false); + const [userId, setUserId] = useState(null); + const [signOutPopupActive, setSignOutPopupActive] = useState(false); const avatarUrl = `${base}/${avatarId}.png`; @@ -105,8 +109,34 @@ export default function Profile() { }; useEffect(() => { - // TODO: Remove this redirect once the rest of RSVP is finished. - // redirect("/"); + const loadUserInfo = async () => { + try { + const userInfo = await getUserInfo(); + console.log(userInfo); + + // if ( + // RSVPInfo.response !== "ACCEPTED" || + // RSVPInfo.status !== "ACCEPTED" + // ) { + // router.push("/profile-unavailable"); + // return; + // } + // const profile = await loadProfile(); + // setAvatarId( + // profile.avatarUrl.split("/").pop()!.replace(".png", "") + // ); + setUserId(userInfo.userId); + + setLoading(false); + } catch (error: any) { + console.error("Error loading user data:", error); + setLoading(false); + } + }; + loadUserInfo(); + }, []); + + useEffect(() => { const loadData = async () => { try { const RSVPInfo = await loadAdmissionRSVP(); @@ -418,7 +448,6 @@ export default function Profile() { > {name} - {track} - + { + if (signOutPopupActive) { + sessionStorage.removeItem("token"); + webSignOutUser().then(() => + window.location.reload() + ); + } + if (userId) setSignOutPopupActive(true); + }} + sx={{ + bottom: "4px", + right: "40px", + fontSize: "12px", + color: "#7bff616b" + }} + > + {signOutPopupActive + ? "sign out?" + : (userId ?? "not signed in")} + SHOW QR + { + if (signOutPopupActive) { + sessionStorage.removeItem("token"); + webSignOutUser().then(() => + window.location.reload() + ); + } + if (userId) setSignOutPopupActive(true); + }} + sx={{ + bottom: "50px", + right: "10px", + fontSize: "12px", + color: "#7bff616b" + }} + > + {signOutPopupActive + ? "sign out?" + : (userId ?? "not signed in")} + ) : ( diff --git a/util/api.ts b/util/api.ts index 38e300b5..b2a3c7cf 100644 --- a/util/api.ts +++ b/util/api.ts @@ -10,7 +10,8 @@ import { RSVPInfo, EventType, MentorProfile, - JudgeProfile + JudgeProfile, + UserInfo } from "./types"; const APIv2 = "https://adonix.hackillinois.org"; @@ -206,6 +207,10 @@ export async function postAuthRefresh(): Promise { await requestv2("POST", "/auth/refresh", {}); } +export async function webSignOutUser(): Promise { + await requestv2("POST", "/auth/logout", {}); +} + export async function getEvents(): Promise { const res = await requestv2("GET", "/event").catch(handleError); return res.events as EventType[]; @@ -227,3 +232,8 @@ export async function getJudges(): Promise { const res = await requestv2("GET", "/judge/info/").catch(handleError); return res as JudgeProfile[]; } + +export async function getUserInfo(): Promise { + const res = await requestv2("GET", "/user").catch(handleError); + return res as UserInfo; +} diff --git a/util/types.ts b/util/types.ts index 90d1f329..d4c8c34b 100644 --- a/util/types.ts +++ b/util/types.ts @@ -229,3 +229,9 @@ export type JudgeProfile = { description: string; imageUrl: string; }; + +export type UserInfo = { + userId: string; + name: string; + email: string; +};