-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature_protected_router_and_subscription
- Loading branch information
1 parent
dffcbbb
commit 1a3f7ae
Showing
30 changed files
with
602 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
REACT_APP_BACKEND_URL=http://localhost:8080 | ||
REACT_APP_BACKEND_URL=http://localhost:8080 | ||
REACT_APP_STRIPE_API_KEY=pk_test_51Nv2EyKeTo38dsfeVEz53ZE9wFsAwwfQKEW5SROEb1ZbPg0rkX9xbZRi0ah7bjVQ7cuoy4dGH85rTag1TcLrhAi50043215z40 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
REACT_APP_BACKEND_URL=https://api.linguaphoto.com | ||
REACT_APP_BACKEND_URL=https://api.linguaphoto.com | ||
REACT_APP_STRIPE_API_KEY=pk_live_51Nv2EyKeTo38dsfexsIGE1kPyPq8O7GpmsS6lFfmlOdfD5m70Mgq3c8Uq7CL0LvroMLybjxy0j28ErOKHwFQtFMf00p46WUYdr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useAuth } from "contexts/AuthContext"; | ||
import { useEffect } from "react"; | ||
import { useLocation, useNavigate } from "react-router-dom"; | ||
|
||
interface PrivateRouteProps { | ||
element: JSX.Element; | ||
requiredSubscription?: boolean; // Optional flag for subscription requirement | ||
} | ||
|
||
const PrivateRoute: React.FC<PrivateRouteProps> = ({ | ||
element, | ||
requiredSubscription = false, | ||
}) => { | ||
const { auth } = useAuth(); | ||
const location = useLocation(); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
if (auth) | ||
if (!auth?.is_auth) { | ||
// Redirect to login if not authenticated | ||
navigate("/login", { replace: true, state: { from: location } }); | ||
} else if (requiredSubscription && !auth?.is_subscription) { | ||
// Redirect to subscription page if subscription is required and not active | ||
navigate("/subscription_type", { replace: true }); | ||
} | ||
}, [auth, requiredSubscription, navigate, location]); | ||
|
||
if (!auth?.is_auth || (requiredSubscription && !auth?.is_subscription)) { | ||
// Render nothing while redirecting | ||
return null; | ||
} | ||
|
||
return element; | ||
}; | ||
|
||
export default PrivateRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.