Files
sign/apps/web/components/login.tsx

181 lines
6.2 KiB
TypeScript
Raw Normal View History

2022-12-06 19:16:34 +01:00
import { LockClosedIcon } from "@heroicons/react/20/solid";
2022-12-08 16:04:22 +01:00
import Link from "next/link";
2023-03-03 20:22:14 +01:00
import { FormProvider, useForm } from "react-hook-form";
2023-01-11 14:36:59 +01:00
2022-12-06 19:16:34 +01:00
import Logo from "./logo";
2023-03-03 20:22:14 +01:00
import { signIn } from "next-auth/react";
2023-01-11 14:36:59 +01:00
import { useState } from "react";
import { useRouter } from "next/router";
2023-03-03 20:22:14 +01:00
import { toast } from "react-hot-toast";
2023-01-16 14:25:55 +01:00
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib/constants";
2023-02-02 14:46:24 +01:00
import { Button } from "@documenso/ui";
2023-01-11 14:36:59 +01:00
interface LoginValues {
email: string;
password: string;
totpCode: string;
csrfToken: string;
}
2023-03-19 14:59:10 +01:00
export default function Login(props: any) {
2023-01-11 14:36:59 +01:00
const router = useRouter();
const methods = useForm<LoginValues>();
const { register, formState } = methods;
const [errorMessage, setErrorMessage] = useState<string | null>(null);
let callbackUrl =
typeof router.query?.callbackUrl === "string"
? router.query.callbackUrl
: "";
// If not absolute URL, make it absolute
if (!/^https?:\/\//.test(callbackUrl)) {
2023-01-16 14:25:55 +01:00
callbackUrl = `${NEXT_PUBLIC_WEBAPP_URL}/${callbackUrl}`;
2023-01-11 14:36:59 +01:00
}
const onSubmit = async (values: LoginValues) => {
setErrorMessage(null);
2023-01-13 19:01:58 +01:00
const res = await toast.promise(
signIn<"credentials">("credentials", {
...values,
callbackUrl,
redirect: false,
}),
{
2023-01-16 16:33:15 +01:00
loading: "Logging in...",
2023-01-13 19:01:58 +01:00
success: "Login successful.",
error: "Could not log in :/",
},
{
style: {
minWidth: "200px",
},
}
);
2023-01-12 15:59:21 +01:00
if (!res) {
2023-03-03 20:21:20 +01:00
setErrorMessage("Error");
2023-01-14 18:47:19 +01:00
toast.dismiss();
2023-01-12 15:59:21 +01:00
toast.error("Something went wrong.");
2023-03-09 13:14:11 +01:00
} else if (!res.error) {
2023-03-03 20:21:20 +01:00
// we're logged in, let's do a hard refresh to the original url
2023-01-13 19:01:58 +01:00
router.push(callbackUrl);
2023-03-09 13:14:11 +01:00
} else {
2023-01-14 18:47:19 +01:00
toast.dismiss();
2023-01-12 15:59:21 +01:00
if (res.status == 401) {
toast.error("Invalid email or password.");
} else {
toast.error("Could not login.");
}
}
2023-01-11 14:36:59 +01:00
};
2022-12-06 19:16:34 +01:00
return (
<>
<div className="flex min-h-full items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
<div className="w-full max-w-md space-y-8">
<div>
2023-01-11 15:27:09 +01:00
<Logo className="mx-auto h-10 w-auto"></Logo>
2022-12-06 19:16:34 +01:00
<h2 className="mt-6 text-center text-3xl font-bold tracking-tight text-gray-900">
Sign in to your account
</h2>
</div>
2023-01-11 14:36:59 +01:00
<FormProvider {...methods}>
<form
className="mt-8 space-y-6"
onSubmit={methods.handleSubmit(onSubmit)}
>
<input type="hidden" name="remember" defaultValue="true" />
<div className="-space-y-px rounded-md shadow-sm">
<div>
<label htmlFor="email-address" className="sr-only">
Email
</label>
<input
{...register("email")}
id="email-address"
name="email"
type="email"
autoComplete="email"
required
2023-01-11 19:51:22 +01:00
className="relative block w-full appearance-none rounded-none rounded-t-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-neon focus:outline-none focus:ring-neon sm:text-sm"
2023-01-13 19:01:58 +01:00
placeholder="Email"
2023-01-11 14:36:59 +01:00
/>
</div>
<div>
<label htmlFor="password" className="sr-only">
Password
</label>
<input
{...register("password")}
id="password"
name="password"
type="password"
autoComplete="current-password"
required
2023-01-11 19:51:22 +01:00
className="relative block w-full appearance-none rounded-none rounded-b-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-neon focus:outline-none focus:ring-neon sm:text-sm"
2023-01-11 14:36:59 +01:00
placeholder="Password"
/>
</div>
2022-12-06 19:16:34 +01:00
</div>
2023-01-11 14:36:59 +01:00
<div className="flex items-center justify-between">
<div className="text-sm">
<a href="#" className="font-medium text-neon hover:text-neon">
Forgot your password?
</a>
</div>
2022-12-06 19:16:34 +01:00
</div>
2023-01-11 14:36:59 +01:00
<div>
2023-02-02 14:46:24 +01:00
<Button
2023-02-15 17:57:51 +01:00
type="submit"
2023-01-11 14:36:59 +01:00
disabled={formState.isSubmitting}
2023-02-02 14:46:24 +01:00
className="group relative flex w-full"
2023-01-11 14:36:59 +01:00
>
<span className="absolute inset-y-0 left-0 flex items-center pl-3">
<LockClosedIcon
2023-02-02 14:46:24 +01:00
className="h-5 w-5 text-neon-dark group-hover:text-neon disabled:group-hover:bg-gray-600 disabled:disabled:bg-gray-600"
2023-01-11 14:36:59 +01:00
aria-hidden="true"
/>
</span>
Sign in
2023-02-02 14:46:24 +01:00
</Button>
2023-01-11 14:36:59 +01:00
</div>
2023-01-11 15:32:05 +01:00
<div>
<div className="relative">
<div
className="absolute inset-0 flex items-center"
aria-hidden="true"
>
<div className="w-full border-t border-gray-300" />
</div>
<div className="relative flex justify-center"></div>
</div>
</div>
2023-03-19 14:59:10 +01:00
{props.allowSignup ? (
<p className="mt-2 text-center text-sm text-gray-600">
Are you new here?{" "}
<Link
href="/signup"
className="font-medium text-neon hover:text-neon"
>
Create a new Account
</Link>
</p>
2023-03-20 15:45:57 +01:00
) : (
<p className="mt-2 text-center text-sm text-gray-600">
Like Documenso{" "}
<Link
href="https://documenso.com"
className="font-medium text-neon hover:text-neon"
>
Hosted Documenso will be availible soon
</Link>
</p>
)}
2023-01-11 14:36:59 +01:00
</form>
</FormProvider>
2022-12-06 19:16:34 +01:00
</div>
</div>
2023-01-13 19:01:58 +01:00
{/* <Toaster position="top-center" /> */}
2022-12-06 19:16:34 +01:00
</>
);
}