Files
muzak/frontend/src/components/SessionChecker.tsx
T

39 lines
1.1 KiB
TypeScript

"use client";
import Link from "next/link";
import { useAuth } from "react-oidc-context";
import { useState, useEffect, use } from "react";
import { useRouter, usePathname } from "next/navigation";
import { api, Session } from "muzak/data/fetcher";
import { getUser } from "muzak/data/fetcher";
import { useSession } from "muzak/contexts/SessionContext";
import getSessionId from "muzak/data/session";
export default function SessionChecker() {
const auth = useAuth();
const router = useRouter();
const path = usePathname();
const { sessionId, setSessionId } = useSession();
async function getSessions() {
if (!auth.isLoading && !auth.error && auth.isAuthenticated) {
const { data: sessions } = await api?.sessions?.list({});
console.log(sessions);
if (sessions?.length > 0) {
const user = getUser();
setSessionId(sessions[0].session_id);
if (sessions[0].host !== user?.profile?.email) {
if (path !== "/mobile-voting") {
router.push("/mobile-voting");
}
}
}
}
}
useEffect(() => {
getSessions();
}, [auth, path]);
return <> </>;
}