Skip to content
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

chore: bump next-auth #491

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions dashboard/final-example/app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { sql } from '@vercel/postgres';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { signIn } from '@/auth';
import { AuthError } from 'next-auth';

const FormSchema = z.object({
id: z.string(),
Expand Down Expand Up @@ -124,10 +125,15 @@ export async function authenticate(
formData: FormData,
) {
try {
await signIn('credentials', Object.fromEntries(formData));
await signIn('credentials', formData);
} catch (error) {
if ((error as Error).message.includes('CredentialsSignin')) {
return 'CredentialsSignin';
if (error instanceof AuthError) {
switch (error.type) {
case 'CredentialsSignin':
return 'Invalid credentials.';
default:
return 'Something went wrong.';
}
Comment on lines +130 to +136
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a return type outside of this condition (or in an else) to catch when error instanceof AuthError returns false?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those errors are thrown further down. It's not the task of the authenticate method. There are special throws as well that powers redirect() (used internally by signIn) which should not be handled and let Next.js/React handle them with their error boundaries.

If the error is an AuthError, we already know how to handle it.

}
throw error;
}
Expand Down
6 changes: 3 additions & 3 deletions dashboard/final-example/app/ui/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Button } from './button';
import { useFormState, useFormStatus } from 'react-dom';

export default function LoginForm() {
const [state, dispatch] = useFormState(authenticate, undefined);
const [errorMessage, dispatch] = useFormState(authenticate, undefined);

return (
<form action={dispatch} className="space-y-3">
Expand Down Expand Up @@ -67,10 +67,10 @@ export default function LoginForm() {
aria-live="polite"
aria-atomic="true"
>
{state === 'CredentialsSignin' && (
{errorMessage && (
<>
<ExclamationCircleIcon className="h-5 w-5 text-red-500" />
<p className="text-sm text-red-500">Invalid credentials</p>
<p className="text-sm text-red-500">{errorMessage}</p>
</>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion dashboard/final-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"bcrypt": "^5.1.1",
"clsx": "^2.0.0",
"next": "^14.0.2",
"next-auth": "^5.0.0-beta.3",
"next-auth": "^5.0.0-beta.4",
"postcss": "8.4.31",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
Loading