Skip to content
Snippets Groups Projects
Commit 3580bb38 authored by Yusuf Akgül's avatar Yusuf Akgül :hatching_chick:
Browse files

validation additions for not existing user in signup

parent 09cbfbc4
No related branches found
No related tags found
1 merge request!20validation additions for not existing user in signup
Pipeline #36473 failed
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { zodResolver } from "@hookform/resolvers/zod" import { zodResolver } from "@hookform/resolvers/zod"
import { signIn } from 'next-auth/react' import { signIn } from 'next-auth/react'
import { useSearchParams } from "next/navigation" import { useRouter, useSearchParams } from "next/navigation"
import { HTMLAttributes, useState } from 'react' import { HTMLAttributes, useState } from 'react'
import { useForm } from 'react-hook-form' import { useForm } from 'react-hook-form'
import * as z from "zod" import * as z from "zod"
...@@ -33,6 +33,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) { ...@@ -33,6 +33,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
}) })
const [isLoading, setIsLoading] = useState<boolean>(false) const [isLoading, setIsLoading] = useState<boolean>(false)
const [isGitHubLoading, setIsGitHubLoading] = useState<boolean>(false) const [isGitHubLoading, setIsGitHubLoading] = useState<boolean>(false)
const router = useRouter();
const searchParams = useSearchParams() const searchParams = useSearchParams()
async function onSubmit(data: FormData) { async function onSubmit(data: FormData) {
...@@ -68,13 +69,25 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) { ...@@ -68,13 +69,25 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
const signInResult = await signIn("credentials", { const signInResult = await signIn("credentials", {
usernameOrEmail: data.email?.toLowerCase() || data.usernameOrEmail?.toLowerCase(), usernameOrEmail: data.email?.toLowerCase() || data.usernameOrEmail?.toLowerCase(),
password: data.password, password: data.password,
redirect: true, redirect: false,
callbackUrl: searchParams?.get("from") || "/home", callbackUrl: searchParams?.get("from") || "/home",
}); });
setIsLoading(false) setIsLoading(false)
if (signInResult?.error) { 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({ return toast({
variant: "destructive", variant: "destructive",
title: "Uh oh! Something went wrong.", title: "Uh oh! Something went wrong.",
...@@ -83,6 +96,8 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) { ...@@ -83,6 +96,8 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
}) })
} }
router.push("/home")
if (type === "signup") { if (type === "signup") {
return toast({ return toast({
title: "Congratulations!", title: "Congratulations!",
...@@ -90,7 +105,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) { ...@@ -90,7 +105,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
}) })
} else { } else {
return toast({ return toast({
title: "Logging in.", title: "Login successful.",
description: "You will be redirected shortly.", description: "You will be redirected shortly.",
}) })
} }
......
...@@ -42,7 +42,7 @@ export const authOptions: NextAuthOptions = { ...@@ -42,7 +42,7 @@ export const authOptions: NextAuthOptions = {
}); });
if (!user || !user.password) { if (!user || !user.password) {
return null throw new Error('user not found')
} }
const isPasswordValid = await compare( const isPasswordValid = await compare(
...@@ -51,7 +51,7 @@ export const authOptions: NextAuthOptions = { ...@@ -51,7 +51,7 @@ export const authOptions: NextAuthOptions = {
) )
if (!isPasswordValid) { if (!isPasswordValid) {
return null throw new Error('invalid password')
} }
return { return {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment