diff --git a/backend/internal/server/company.go b/backend/internal/server/company.go index 928fe608..611b515e 100644 --- a/backend/internal/server/company.go +++ b/backend/internal/server/company.go @@ -48,6 +48,9 @@ func (s *Server) handleGetUserCompany(c echo.Context) error { company, err := s.queries.GetCompanyByUser(c.Request().Context(), claims.UserID) if err != nil { + if isNoRowsError(err) { + return echo.NewHTTPError(http.StatusNotFound, "No company found") + } log.Error().Err(err).Msg("Failed to get company by user") return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get company") } diff --git a/frontend/src/contexts/AuthContext.tsx b/frontend/src/contexts/AuthContext.tsx index 7a53692d..0707e6ae 100644 --- a/frontend/src/contexts/AuthContext.tsx +++ b/frontend/src/contexts/AuthContext.tsx @@ -60,4 +60,3 @@ export function useAuth() { } return context; } - diff --git a/frontend/src/pages/Register.tsx b/frontend/src/pages/Register.tsx index 8ef8528c..83b37d22 100644 --- a/frontend/src/pages/Register.tsx +++ b/frontend/src/pages/Register.tsx @@ -332,7 +332,9 @@ const Register = () => { const location = useLocation(); const { user, setAuth, clearAuth } = useAuth(); const [currentStep, setCurrentStep] = useState(() => { - const locationState = location.state as { step?: RegistrationStep } | null; + const locationState = location.state as { + step?: RegistrationStep; + } | null; if (locationState?.step) { return locationState.step; @@ -440,7 +442,11 @@ const Register = () => { try { const signinResp = await signin(formData.email, formData.password); const company = await getCompany(signinResp.access_token); - setAuth(signinResp.user, signinResp.access_token, company.ID); + setAuth( + signinResp.user, + signinResp.access_token, + company ? company.ID : null + ); if (!signinResp.user.isEmailVerified) { setCurrentStep('verify-email'); @@ -451,7 +457,7 @@ const Register = () => { setCurrentStep('form-details'); return; } - + // Redirect based on user role if (signinResp.user.role === 'admin') { navigate('/admin/dashboard', { replace: true }); diff --git a/frontend/src/services/company.ts b/frontend/src/services/company.ts index 6a66732a..dd0894cf 100644 --- a/frontend/src/services/company.ts +++ b/frontend/src/services/company.ts @@ -42,7 +42,7 @@ export async function createCompany( export async function getCompany( accessToken: string -): Promise { +): Promise { const url = getApiUrl('/company'); const res = await fetch(url, { @@ -54,6 +54,10 @@ export async function getCompany( const json = await res.json(); + if (res.status === HttpStatusCode.NOT_FOUND) { + return null; + } + if (res.status !== HttpStatusCode.OK) { throw new ApiError('Failed to create company', res.status, json); }