-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added some form components from shadcn, upgraded to newest next-auth for better error treatment, refactored auth configs a bit
- Loading branch information
Showing
10 changed files
with
475 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"use server"; | ||
|
||
import { AuthError } from "next-auth"; | ||
import { z } from "zod"; | ||
import { signIn as naSignIn } from "@sophys-web/auth"; | ||
|
||
const SigninSchema = z.object({ | ||
email: z.string().email().min(2), | ||
password: z.string().min(2), | ||
}); | ||
|
||
export async function signIn( | ||
data: z.infer<typeof SigninSchema>, | ||
callbackUrl?: string, | ||
) { | ||
const parsed = await SigninSchema.safeParseAsync(data); | ||
|
||
if (!parsed.success) { | ||
return { error: "Invalid data" }; | ||
} | ||
|
||
try { | ||
await naSignIn("credentials", { | ||
email: parsed.data.email, | ||
password: parsed.data.password, | ||
redirectTo: callbackUrl || "/", | ||
}); | ||
} catch (error) { | ||
if (error instanceof AuthError) { | ||
switch (error.type) { | ||
case "CredentialsSignin": | ||
return { error: "Invalid credentials" }; | ||
case "AccessDenied": | ||
return { error: "Access denied" }; | ||
default: | ||
return { error: "Something went wrong!" }; | ||
} | ||
} | ||
throw error; | ||
} | ||
} |
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,105 @@ | ||
"use client"; | ||
|
||
import { useSearchParams } from "next/navigation"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { useForm } from "react-hook-form"; | ||
import { z } from "zod"; | ||
import { Button } from "@sophys-web/ui/button"; | ||
import { | ||
Form, | ||
FormControl, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "@sophys-web/ui/form"; | ||
import { Input } from "@sophys-web/ui/input"; | ||
import { toast } from "@sophys-web/ui/sonner"; | ||
import { signIn } from "../../actions/auth"; | ||
|
||
const FormSchema = z.object({ | ||
email: z | ||
.string() | ||
.min(2, { message: "Email is required" }) | ||
.email({ message: "Input must be a valid e-mail" }), | ||
password: z.string().min(2, { message: "Password is required" }), | ||
}); | ||
|
||
export function SignInForm() { | ||
const params = useSearchParams(); | ||
const form = useForm<z.infer<typeof FormSchema>>({ | ||
resolver: zodResolver(FormSchema), | ||
defaultValues: { | ||
email: "", | ||
password: "", | ||
}, | ||
}); | ||
|
||
const onSubmit = async (data: z.infer<typeof FormSchema>) => { | ||
toast.info("Signing in..."); | ||
const res = await signIn( | ||
{ | ||
email: data.email, | ||
password: data.password, | ||
}, | ||
params.get("callbackUrl") || "/", | ||
); | ||
|
||
if (res?.error) { | ||
const error = res.error; | ||
toast.error(error); | ||
form.reset(); | ||
} | ||
|
||
if (!res?.error) { | ||
toast.success("Signed in successfully"); | ||
} | ||
}; | ||
|
||
return ( | ||
<Form {...form}> | ||
<form | ||
className="my-auto flex flex-col space-y-6 rounded-sm border p-24 shadow-lg" | ||
onSubmit={form.handleSubmit(onSubmit)} | ||
> | ||
<FormField | ||
control={form.control} | ||
name="email" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Email</FormLabel> | ||
<FormControl> | ||
<Input | ||
placeholder="[email protected]" | ||
{...field} | ||
disabled={form.formState.isSubmitting} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="password" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Password</FormLabel> | ||
<FormControl> | ||
<Input | ||
type="password" | ||
{...field} | ||
disabled={form.formState.isSubmitting} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<Button disabled={form.formState.isSubmitting} type="submit"> | ||
Sign in | ||
</Button> | ||
</form> | ||
</Form> | ||
); | ||
} |
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,9 @@ | ||
import { SignInForm } from "./form"; | ||
|
||
export default function SignInPage() { | ||
return ( | ||
<div className="flex items-center justify-center gap-6 p-24"> | ||
<SignInForm /> | ||
</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
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
Oops, something went wrong.