2021-08-01 03:05:40 +00:00
|
|
|
import type { BlitzPage } from "blitz";
|
2021-09-25 12:58:28 +00:00
|
|
|
import { useRouter, Routes, AuthenticationError, Link, useMutation } from "blitz";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import BaseLayout from "../../core/layouts/base-layout";
|
2021-09-25 12:58:28 +00:00
|
|
|
import { AuthForm as Form, FORM_ERROR } from "../components/auth-form";
|
|
|
|
import { Login } from "../validations";
|
|
|
|
import { LabeledTextField } from "../components/labeled-text-field";
|
|
|
|
import login from "../mutations/login";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-01 03:05:40 +00:00
|
|
|
const SignIn: BlitzPage = () => {
|
2021-07-31 15:57:43 +00:00
|
|
|
const router = useRouter();
|
2021-09-25 12:58:28 +00:00
|
|
|
const [loginMutation] = useMutation(login);
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
return (
|
2021-09-25 12:58:28 +00:00
|
|
|
<Form
|
|
|
|
texts={{
|
|
|
|
title: "Welcome back!",
|
|
|
|
subtitle: (
|
|
|
|
<>
|
|
|
|
Need an account?
|
|
|
|
<Link href={Routes.SignUp()}>
|
|
|
|
<a className="font-medium text-primary-600 hover:text-primary-500 focus:outline-none focus:underline transition ease-in-out duration-150">
|
|
|
|
Create yours for free
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
submit: "Sign in",
|
|
|
|
}}
|
|
|
|
schema={Login}
|
|
|
|
initialValues={{ email: "", password: "" }}
|
|
|
|
onSubmit={async (values) => {
|
|
|
|
try {
|
|
|
|
await loginMutation(values);
|
2021-07-31 14:33:18 +00:00
|
|
|
const next = router.query.next
|
|
|
|
? decodeURIComponent(router.query.next as string)
|
2021-08-01 03:05:40 +00:00
|
|
|
: Routes.Messages();
|
2021-07-31 15:57:43 +00:00
|
|
|
router.push(next);
|
2021-09-25 12:58:28 +00:00
|
|
|
} catch (error: any) {
|
|
|
|
if (error instanceof AuthenticationError) {
|
|
|
|
return { [FORM_ERROR]: "Sorry, those credentials are invalid" };
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
[FORM_ERROR]: "Sorry, we had an unexpected error. Please try again. - " + error.toString(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2021-09-25 13:47:30 +00:00
|
|
|
<LabeledTextField name="email" label="Email" type="email" />
|
|
|
|
<LabeledTextField name="password" label="Password" type="password" showForgotPasswordLabel />
|
2021-09-25 12:58:28 +00:00
|
|
|
</Form>
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
|
|
|
};
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-01 03:05:40 +00:00
|
|
|
SignIn.redirectAuthenticatedTo = Routes.Messages();
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-10-26 21:34:21 +00:00
|
|
|
SignIn.getLayout = (page) => <BaseLayout title="Sign in">{page}</BaseLayout>;
|
2021-08-01 03:05:40 +00:00
|
|
|
|
|
|
|
export default SignIn;
|