Home > Blockchain >  How Could I redirect login page using Javascript
How Could I redirect login page using Javascript

Time:05-08

const RequireAuth = ({ children }) => {
    const [user, loading, error] = useAuthState(auth);

    const location = useLocation();
    if (loading) {
        return <Loading></Loading>
    }
    if (!user) {
        return <Navigate to="/login" state={{ from: location }} replace />;
    }
    return children;
};

export default RequireAuth;

CodePudding user response:

You can also do something like this:

if (!user) {
   window.location.href = `http://localhost:3000/login?from=${location}` // for example
}

and then parse query string to get a value of location

CodePudding user response:

const [user, loading] = use Auth State(auth)
const location = use Location()

if (loading) {
    return <Loading></Loading>
}
if (!user) {
    return <Navigate to="/login" state={{ from: location }} replace ></Navigate>
}
return children

};

export default Private Route;

(sorry for space)

  • Related