refactor-upto-phase 6
This commit is contained in:
@@ -4,6 +4,8 @@ import { useAuthStore } from "../../store/authStore";
|
||||
import { useErrorHandler } from "../../hooks/useErrorHandler";
|
||||
import { trackLoading } from "../common/LoadingStateMonitor";
|
||||
|
||||
const PRICING_URL = "https://igny8.com/pricing";
|
||||
|
||||
interface ProtectedRouteProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
@@ -13,7 +15,7 @@ interface ProtectedRouteProps {
|
||||
* Redirects to /signin if user is not authenticated
|
||||
*/
|
||||
export default function ProtectedRoute({ children }: ProtectedRouteProps) {
|
||||
const { isAuthenticated, loading } = useAuthStore();
|
||||
const { isAuthenticated, loading, user, logout } = useAuthStore();
|
||||
const location = useLocation();
|
||||
const { addError } = useErrorHandler('ProtectedRoute');
|
||||
const [showError, setShowError] = useState(false);
|
||||
@@ -24,6 +26,24 @@ export default function ProtectedRoute({ children }: ProtectedRouteProps) {
|
||||
trackLoading('auth-loading', loading);
|
||||
}, [loading]);
|
||||
|
||||
// Validate account + plan whenever auth/user changes
|
||||
useEffect(() => {
|
||||
if (!isAuthenticated) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!user?.account) {
|
||||
setErrorMessage('This user is not linked to an account. Please contact support.');
|
||||
logout();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!user.account.plan) {
|
||||
logout();
|
||||
window.location.href = PRICING_URL;
|
||||
}
|
||||
}, [isAuthenticated, user, logout]);
|
||||
|
||||
// Immediate check on mount: if loading is true, reset it immediately
|
||||
useEffect(() => {
|
||||
if (loading) {
|
||||
|
||||
@@ -32,6 +32,10 @@ export default function SignInForm() {
|
||||
const from = (location.state as any)?.from?.pathname || "/";
|
||||
navigate(from, { replace: true });
|
||||
} catch (err: any) {
|
||||
if (err?.code === 'PLAN_REQUIRED') {
|
||||
window.location.href = 'https://igny8.com/pricing';
|
||||
return;
|
||||
}
|
||||
setError(err.message || "Login failed. Please check your credentials.");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user