Skip to content

Commit

Permalink
do not throw on company not found on login (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
juancwu authored Dec 10, 2024
2 parents 7beb5b2 + 42cc0a2 commit 39e5d4a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions backend/internal/server/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
1 change: 0 additions & 1 deletion frontend/src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,3 @@ export function useAuth() {
}
return context;
}

12 changes: 9 additions & 3 deletions frontend/src/pages/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ const Register = () => {
const location = useLocation();
const { user, setAuth, clearAuth } = useAuth();
const [currentStep, setCurrentStep] = useState<RegistrationStep>(() => {
const locationState = location.state as { step?: RegistrationStep } | null;
const locationState = location.state as {
step?: RegistrationStep;
} | null;

if (locationState?.step) {
return locationState.step;
Expand Down Expand Up @@ -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');
Expand All @@ -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 });
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/services/company.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function createCompany(

export async function getCompany(
accessToken: string
): Promise<CreateCompanyResponse> {
): Promise<CreateCompanyResponse | null> {
const url = getApiUrl('/company');

const res = await fetch(url, {
Expand All @@ -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);
}
Expand Down

0 comments on commit 39e5d4a

Please sign in to comment.