auto connects user to session if available

This commit is contained in:
2025-08-20 19:53:32 +02:00
parent 2a23f1dedc
commit 5bdf1ed940
2 changed files with 19 additions and 39 deletions
+18 -34
View File
@@ -11,44 +11,28 @@ import getSessionId from "muzak/data/session";
export default function SessionChecker() {
const auth = useAuth();
const router = useRouter();
const path = usePathname();
const [isSession, setIsSession] = useState(false);
const [link, setLink] = useState("/lobby");
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) {
// setIsSession(true);
// const user = getUser();
// setSessionId(sessions[0].session_id);
// if (sessions[0].host === user?.profile?.email) {
// setLink("/lobby");
// } else {
// setLink("/session");
// }
// } else {
// setIsSession(false);
// }
// }
// }
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(() => {
//getSessionId();
console.log("hheufheufh");
}, [auth]);
if (sessionId) {
return (
<Link
href={link}
className="bold text-l absolute top-2 left-2 rounded-[50%] bg-red-500 p-3 font-bold text-white"
>
Session available!
</Link>
);
}
getSessions();
}, [auth, path]);
return <> </>;
}