Skip to content

Instantly share code, notes, and snippets.

@zinedkaloc
Created August 8, 2022 11:51
Show Gist options
  • Save zinedkaloc/0d366c5c8d0927a265eb3565113eba7e to your computer and use it in GitHub Desktop.
Save zinedkaloc/0d366c5c8d0927a265eb3565113eba7e to your computer and use it in GitHub Desktop.
import React from 'react';
import { useLocation, Navigate } from 'react-router-dom';
import { useAuth } from '../contexts/Auth';
export function PrivateRoute({ children }) {
const { session } = useAuth();
let location = useLocation();
return session ? (
children
) : (
<Navigate to="/login" state={{ from: location }} replace />
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment