Skip to content

Commit

Permalink
force + check current registration step (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
juancwu authored Dec 10, 2024
2 parents f9996ae + e91b98c commit fe5c742
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 23 deletions.
38 changes: 38 additions & 0 deletions frontend/src/components/layout/RegistrationGuard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useNavigate } from "react-router-dom";
import { useAuth } from '@/contexts/AuthContext';
import { useEffect } from 'react';

const RegistrationGuard = ({ children }: { children: React.ReactNode }) => {
const navigate = useNavigate();
const { user } = useAuth();

useEffect(() => {
if (user) {
if (!user.isEmailVerified) {
navigate('/register', {
state: {
step: 'verify-email',
email: user.email
},
replace: true
});
return;
}

if (!user.firstName || !user.lastName) {
navigate('/register', {
state: {
step: 'form-details',
email: user.email
},
replace: true
});
return;
}
}
}, [user, navigate]);

return <>{ children }</>
};

export { RegistrationGuard };
70 changes: 61 additions & 9 deletions frontend/src/pages/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState, useEffect, FormEvent } from 'react';
import { Button, TextInput, TextArea } from '@components';
import { register, signin, RegisterError, ApiError } from '@services';
import { useAuth } from '@/contexts/AuthContext';
import { useNavigate } from 'react-router-dom';
import { useNavigate, useLocation } from 'react-router-dom';
import { getApiUrl } from '@/utils';

type RegistrationStep =
Expand Down Expand Up @@ -315,15 +315,34 @@ const RegistrationComplete = ({ onComplete }: RegistrationCompleteProps) => {

const Register = () => {
const navigate = useNavigate();
const { setAuth } = useAuth();
const [currentStep, setCurrentStep] = useState<RegistrationStep>('login-register');
const location = useLocation();
const { user, setAuth, clearAuth } = useAuth();
const [currentStep, setCurrentStep] = useState<RegistrationStep>(() => {
const locationState = location.state as { step?: RegistrationStep } | null;

if (locationState?.step) {
return locationState.step;
}

if (user) {
if (!user.isEmailVerified) {
return 'verify-email';
}

if (!user.firstName || !user.lastName) {
return 'form-details';
}
}

return 'login-register';
});
const [formData, setFormData] = useState<FormData>({
firstName: '',
lastName: '',
firstName: user?.firstName || '',
lastName: user?.lastName || '',
position: '',
bio: '',
linkedIn: '',
email: '',
email: user?.email || (location.state as any)?.email || '',
password: '',
});
const [errors, setErrors] = useState<FormErrors>({});
Expand Down Expand Up @@ -377,11 +396,14 @@ const Register = () => {
const handleInitialSubmit = async (e: FormEvent) => {
e.preventDefault();
setIsLoading(true);

try {
const regResp = await register(formData.email, formData.password);
setAuth(regResp.user, regResp.accessToken, 'mock-company-id');
setCurrentStep('verify-email');
} catch (error) {
clearAuth();

if (error instanceof RegisterError) {
setErrors(prev => ({
...prev,
Expand All @@ -403,16 +425,28 @@ const Register = () => {
try {
const signinResp = await signin(formData.email, formData.password);
setAuth(signinResp.user, signinResp.accessToken);

if (!signinResp.user.isEmailVerified) {
setCurrentStep('verify-email');
return;
}

if (!signinResp.user.firstName || !signinResp.user.lastName) {
setCurrentStep('form-details');
return;
}

// Redirect based on user role
if (signinResp.user.role === 'admin') {
navigate('/admin/dashboard');
navigate('/admin/dashboard', { replace: true });
} else if (signinResp.user.role === 'startup_owner') {
navigate('/dashboard');
navigate('/dashboard', { replace: true });
} else if (signinResp.user.role === 'investor') {
navigate('/dashboard'); // or a specific investor dashboard
navigate('/dashboard', { replace: true }); // or a specific investor dashboard
}
} catch (error) {
clearAuth();

if (error instanceof ApiError) {
setErrors(prev => ({
...prev,
Expand Down Expand Up @@ -481,6 +515,24 @@ const Register = () => {
}
};

useEffect(() => {
if (user && currentStep === 'login-register') {
if (!user.isEmailVerified) {
setCurrentStep('verify-email');
} else if (!user.firstName || !user.lastName) {
setCurrentStep('form-details');
} else {
if (user.role === 'admin') {
navigate('/admin/dashboard', { replace: true });
} else if (user.role === 'startup_owner') {
navigate('/dashboard', { replace: true });
} else if (user.role === 'investor') {
navigate('/dashboard', { replace: true }); // or a specific investor dashboard
}
}
}
}, [user, currentStep, navigate]);

return (
<div className="min-h-screen bg-gray-50">
<div className="p-4">{renderCurrentStep()}</div>
Expand Down
57 changes: 43 additions & 14 deletions frontend/src/utils/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { Landing, Register, DashboardPage, AdminDashboardPage, SubmitProjectPage } from '@pages';
import { AuthProvider } from '@/contexts/AuthContext';
import { ProtectedRoute } from '@/components/layout/ProtectedRoute';
import { RegistrationGuard } from '@/components/layout/RegistrationGuard';

const Router = () => (
<AuthProvider>
Expand All @@ -13,74 +14,102 @@ const Router = () => (
{/* User routes */}
<Route path="/dashboard" element={
<ProtectedRoute allowedRoles={['startup_owner', 'investor', 'admin']}>
<DashboardPage />
<RegistrationGuard>
<DashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/projects" element={
<ProtectedRoute allowedRoles={['startup_owner', 'investor', 'admin']}>
<DashboardPage />
<RegistrationGuard>
<DashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/resources" element={
<ProtectedRoute allowedRoles={['startup_owner', 'investor', 'admin']}>
<DashboardPage />
<RegistrationGuard>
<DashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/favorites" element={
<ProtectedRoute allowedRoles={['startup_owner', 'investor', 'admin']}>
<DashboardPage />
<RegistrationGuard>
<DashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/profile" element={
<ProtectedRoute allowedRoles={['startup_owner', 'investor', 'admin']}>
<DashboardPage />
<RegistrationGuard>
<DashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/drafts" element={
<ProtectedRoute allowedRoles={['startup_owner', 'investor', 'admin']}>
<DashboardPage />
<RegistrationGuard>
<DashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/submit-project" element={
<ProtectedRoute allowedRoles={['startup_owner']}>
<SubmitProjectPage />
<RegistrationGuard>
<SubmitProjectPage />
</RegistrationGuard>
</ProtectedRoute>
} />

{/* Admin routes */}
<Route path="/admin/dashboard" element={
<ProtectedRoute allowedRoles={['admin']}>
<AdminDashboardPage />
<RegistrationGuard>
<AdminDashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/admin/projects" element={
<ProtectedRoute allowedRoles={['admin']}>
<AdminDashboardPage />
<RegistrationGuard>
<AdminDashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/admin/projects/pending" element={
<ProtectedRoute allowedRoles={['admin']}>
<AdminDashboardPage />
<RegistrationGuard>
<AdminDashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/admin/projects/approved" element={
<ProtectedRoute allowedRoles={['admin']}>
<AdminDashboardPage />
<RegistrationGuard>
<AdminDashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/admin/resources" element={
<ProtectedRoute allowedRoles={['admin']}>
<AdminDashboardPage />
<RegistrationGuard>
<AdminDashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/admin/users" element={
<ProtectedRoute allowedRoles={['admin']}>
<AdminDashboardPage />
<RegistrationGuard>
<AdminDashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
<Route path="/admin/settings" element={
<ProtectedRoute allowedRoles={['admin']}>
<AdminDashboardPage />
<RegistrationGuard>
<AdminDashboardPage />
</RegistrationGuard>
</ProtectedRoute>
} />
</Routes>
Expand Down

0 comments on commit fe5c742

Please sign in to comment.