-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Sudarsh1010/improve-sign-in
Improve sign in
- Loading branch information
Showing
14 changed files
with
316 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { z } from "zod"; | ||
|
||
import apiClient from "~/axios"; | ||
import type { emailPassSignInSchema } from "~/schema/auth"; | ||
|
||
interface SignInRes { | ||
message: string; | ||
} | ||
|
||
export const signInMutationFn = async ( | ||
data: z.infer<typeof emailPassSignInSchema>, | ||
) => | ||
await apiClient.post<SignInRes>("auth/sign-in", data).then((res) => res.data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
"use client"; | ||
|
||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { GitHubLogoIcon } from "@radix-ui/react-icons"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { useRouter } from "@tanstack/react-router"; | ||
import { AxiosError } from "axios"; | ||
import * as React from "react"; | ||
import { useForm } from "react-hook-form"; | ||
import { toast } from "sonner"; | ||
import { z } from "zod"; | ||
|
||
import { signInMutationFn } from "~/actions/auth/sign-in"; | ||
import { cn } from "~/lib/utils"; | ||
import { emailPassSignInSchema } from "~/schema/auth"; | ||
|
||
import { Button } from "../ui/button"; | ||
import { Form, FormField } from "../ui/form"; | ||
import { Input } from "../ui/input"; | ||
import { PasswordInput } from "../ui/password-input"; | ||
|
||
type UserAuthFormProps = React.HTMLAttributes<HTMLDivElement>; | ||
type EmailSignInSchema = z.infer<typeof emailPassSignInSchema>; | ||
|
||
export function SignInForm({ className, ...props }: UserAuthFormProps) { | ||
const router = useRouter(); | ||
|
||
const form = useForm<EmailSignInSchema>({ | ||
resolver: zodResolver(emailPassSignInSchema), | ||
}); | ||
|
||
const { mutate, isPending } = useMutation({ | ||
mutationFn: signInMutationFn, | ||
onSuccess: (res) => { | ||
toast.success(res.message); | ||
router.navigate({ to: "/" }); | ||
}, | ||
onError: (err) => { | ||
if (err instanceof AxiosError) { | ||
if (err.response?.data?.errors) { | ||
let shouldFocus = true; | ||
const validationErrors = err.response.data.errors; | ||
|
||
return Object.keys(validationErrors).forEach((field) => { | ||
const fieldErrors = validationErrors[field]; | ||
const errorMessage = Object.values(fieldErrors).find( | ||
(message) => message !== "", | ||
) as string; | ||
|
||
if (errorMessage) { | ||
form.setError( | ||
field as keyof EmailSignInSchema, | ||
{ message: errorMessage }, | ||
{ shouldFocus: shouldFocus }, | ||
); | ||
shouldFocus = false; | ||
} | ||
}); | ||
} | ||
|
||
return toast.error( | ||
err.response?.data?.error || "An unknown error occurred.", | ||
); | ||
} | ||
|
||
return toast.error("An unknown error occurred."); | ||
}, | ||
}); | ||
|
||
async function onSubmit(data: EmailSignInSchema) { | ||
mutate(data); | ||
} | ||
|
||
return ( | ||
<div className={cn("grid gap-6", className)} {...props}> | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="grid gap-y-4"> | ||
<FormField | ||
control={form.control} | ||
name="email" | ||
label="Email" | ||
render={({ field }) => <Input {...field} />} | ||
/> | ||
|
||
<FormField | ||
control={form.control} | ||
name="password" | ||
label="Passowrd" | ||
render={({ field }) => <PasswordInput {...field} />} | ||
/> | ||
|
||
<Button loading={isPending} type="submit" className="mt-4 w-full"> | ||
Sign In | ||
</Button> | ||
</form> | ||
</Form> | ||
|
||
<div className="relative"> | ||
<div className="absolute inset-0 flex items-center"> | ||
<span className="w-full border-t" /> | ||
</div> | ||
<div className="relative flex justify-center text-xs uppercase"> | ||
<span className="bg-background px-2 text-muted-foreground"> | ||
Or continue with | ||
</span> | ||
</div> | ||
</div> | ||
|
||
<Button disabled variant="outline" type="button"> | ||
<GitHubLogoIcon className="size-4" /> GitHub | ||
</Button> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.