Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add session check before redirecting in private wrapper component #1261

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions layouts/private-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { useUser } from "@supabase/auth-helpers-react";
import { authSession } from "lib/hooks/authSession";
import { useRouter } from "next/router";
import React, { useEffect } from "react";

interface PrivateWrapperProps {
isPrivateRoute?: boolean;
children: React.ReactNode
children: React.ReactNode;
}

const PrivateWrapper = ({ isPrivateRoute = false, children }: PrivateWrapperProps) => {
const user = useUser();
const router = useRouter();


async function checkSession() {
const isValid = await authSession();

if (!isValid) {
router.replace("/javascript/dashboard/filter/recent");
}
}

useEffect(() => {
if (isPrivateRoute) {
if (!user) router.replace("/javascript/dashboard/filter/recent");
if (isPrivateRoute && !user) {
checkSession();
}
}, [user]);

Expand Down
Loading