diff --git a/components/user-auth-form.tsx b/components/user-auth-form.tsx index f4f207283ae53a0046ba00da9b57b1cfdc118c52..cb42f0702876a2360de7d2d7b3f531192f70fd4b 100644 --- a/components/user-auth-form.tsx +++ b/components/user-auth-form.tsx @@ -2,7 +2,7 @@ import { zodResolver } from "@hookform/resolvers/zod" import { signIn } from 'next-auth/react' -import { useSearchParams } from "next/navigation" +import { useRouter, useSearchParams } from "next/navigation" import { HTMLAttributes, useState } from 'react' import { useForm } from 'react-hook-form' import * as z from "zod" @@ -33,6 +33,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) { }) const [isLoading, setIsLoading] = useState<boolean>(false) const [isGitHubLoading, setIsGitHubLoading] = useState<boolean>(false) + const router = useRouter(); const searchParams = useSearchParams() async function onSubmit(data: FormData) { @@ -68,13 +69,25 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) { const signInResult = await signIn("credentials", { usernameOrEmail: data.email?.toLowerCase() || data.usernameOrEmail?.toLowerCase(), password: data.password, - redirect: true, + redirect: false, callbackUrl: searchParams?.get("from") || "/home", }); setIsLoading(false) if (signInResult?.error) { + if (signInResult.error === "user not found") { + setError('usernameOrEmail', { + type: 'manual', + message: 'Sorry, we couldn\'t find an account with the provided email / username. Please double-check your input or create a new account.' + }); + } + if (signInResult.error === "invalid password") { + setError('password', { + type: 'manual', + message: 'Sorry, but it seems like the password you entered is invalid. Please try again.' + }); + } return toast({ variant: "destructive", title: "Uh oh! Something went wrong.", @@ -83,6 +96,8 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) { }) } + router.push("/home") + if (type === "signup") { return toast({ title: "Congratulations!", @@ -90,7 +105,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) { }) } else { return toast({ - title: "Logging in.", + title: "Login successful.", description: "You will be redirected shortly.", }) } diff --git a/lib/auth.ts b/lib/auth.ts index 7763e341993a4e76ab9682097675d8f44e04f6ae..c899556b873bbd504c58a49eddad980a00124028 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -42,7 +42,7 @@ export const authOptions: NextAuthOptions = { }); if (!user || !user.password) { - return null + throw new Error('user not found') } const isPasswordValid = await compare( @@ -51,7 +51,7 @@ export const authOptions: NextAuthOptions = { ) if (!isPasswordValid) { - return null + throw new Error('invalid password') } return {