-
-
Notifications
You must be signed in to change notification settings - Fork 84
fix: resolve permission errors on first time admin registration #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,8 @@ import { useForm } from 'react-hook-form'; | |||||||||||||||||||||||||||||||||||||
| import { zodResolver } from '@hookform/resolvers/zod'; | ||||||||||||||||||||||||||||||||||||||
| import * as z from 'zod'; | ||||||||||||||||||||||||||||||||||||||
| import { useIsAdminRegisteredQuery } from '@/redux/services/users/authApi'; | ||||||||||||||||||||||||||||||||||||||
| import { useAppDispatch } from '@/redux/hooks'; | ||||||||||||||||||||||||||||||||||||||
| import { initializeAuth } from '@/redux/features/users/authSlice'; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const registerSchema = (t: (key: translationKey, params?: Record<string, string>) => string) => | ||||||||||||||||||||||||||||||||||||||
| z | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -36,6 +38,7 @@ type RegisterForm = z.infer<ReturnType<typeof registerSchema>>; | |||||||||||||||||||||||||||||||||||||
| function useRegister() { | ||||||||||||||||||||||||||||||||||||||
| const { t } = useTranslation(); | ||||||||||||||||||||||||||||||||||||||
| const router = useRouter(); | ||||||||||||||||||||||||||||||||||||||
| const dispatch = useAppDispatch(); | ||||||||||||||||||||||||||||||||||||||
| const [isLoading, setIsLoading] = useState(false); | ||||||||||||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||||||||||||
| data: isAdminRegistered, | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -69,7 +72,16 @@ function useRegister() { | |||||||||||||||||||||||||||||||||||||
| toast.error('Sign up is not allowed'); | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| toast.success(t('auth.register.success')); | ||||||||||||||||||||||||||||||||||||||
| router.push('/auth'); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||
| await (dispatch(initializeAuth() as any) as any).unwrap(); | ||||||||||||||||||||||||||||||||||||||
| } catch (authError) { | ||||||||||||||||||||||||||||||||||||||
| console.warn('Auth initialization after registration failed:', authError); | ||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove console.warn from production code. Console statements should be removed from production code, as noted in the developer checklist. This will be addressed by the error handling fix in the previous comment, which replaces the console.warn with a toast notification and proper navigation. 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| await new Promise((resolve) => setTimeout(resolve, 200)); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| router.push('/dashboard'); | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+76
to
+84
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix error handling to prevent permission errors on auth failure. If Apply this diff to properly handle auth initialization failure: toast.success(t('auth.register.success'));
try {
- await (dispatch(initializeAuth() as any) as any).unwrap();
+ await dispatch(initializeAuth()).unwrap();
+ await new Promise((resolve) => setTimeout(resolve, 200));
+ router.push('/dashboard');
} catch (authError) {
- console.warn('Auth initialization after registration failed:', authError);
+ toast.error('Failed to initialize authentication. Please try logging in.');
+ router.push('/auth');
+ return;
}
-
- await new Promise((resolve) => setTimeout(resolve, 200));
-
- router.push('/dashboard');
}This ensures that only successful auth initialization leads to the dashboard, while failures redirect to the auth page. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||
| toast.error(t('auth.register.errors.registerFailed')); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove type casts and fix initializeAuth typing.
The double
as anycast bypasses TypeScript's type safety and prevents catching type errors at compile time.initializeAuth()should return a properly typed async thunk.Apply this diff to remove the casts:
If TypeScript errors occur after removing the casts, ensure
initializeAuthis properly defined as an async thunk usingcreateAsyncThunkfrom@reduxjs/toolkit.📝 Committable suggestion
🤖 Prompt for AI Agents