-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogin.tsx
107 lines (103 loc) · 3.58 KB
/
login.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { Form, useLoaderData } from "@remix-run/react";
import type { LoaderFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import { auth } from "~/auth.server";
import { commitSession, getSession } from "~/sessions.server";
type LoaderData = {
error: { message: string } | null;
formError?: string;
fieldErrors?: {
email: string | undefined;
product: string | undefined;
};
fields?: {
email: string;
product: string;
};
};
export const loader: LoaderFunction = async ({ request }) => {
await auth.isAuthenticated(request, { successRedirect: "/private" });
const session = await getSession(request.headers.get("Cookie"));
const error = session.get(auth.sessionErrorKey) as LoaderData["error"];
const formErrors = session.get("login:form:error") as Omit<
LoaderData,
"error"
>;
return json<LoaderData>(
{ error, ...formErrors },
{ headers: { "Set-Cookie": await commitSession(session) } } // need these to clear the messages after reading flash keys above
);
};
export default function Login() {
const { error, fieldErrors, formError, fields } = useLoaderData<LoaderData>();
return (
<div className="mx-auto flex max-w-md flex-col py-20">
<h2 className="mt-5 text-center text-3xl">Log in to App</h2>
<p className="mt-4 text-center font-medium text-gray-500">
Click `Continue with SSO` and you will be redirected to your third-party
authentication provider to finish authenticating.
</p>
{error ? <div role="alert">{error.message}</div> : null}
{formError ? <div role="alert">{formError}</div> : null}
<div className="mx-auto mt-3 w-full max-w-sm">
<div className="rounded bg-white py-6 px-6">
<Form
method="post"
action="/auth/sso"
className="flex flex-col items-start space-y-4"
reloadDocument
>
<label htmlFor="email">Work Email</label>
<input
id="email"
type="email"
name="email"
readOnly
defaultValue={fields?.email || "[email protected]"}
className="input w-full"
placeholder="[email protected]"
required
/>
{fieldErrors?.email ? (
<p
className="form-validation-error"
role="alert"
id="email-error"
>
{fieldErrors.email}
</p>
) : null}
<input
type="text"
name="product"
hidden
defaultValue={fields?.product || "1eef7782-41d4-4a0a-b450-0857413b4f63"}
/>
{fieldErrors?.product ? (
<p
className="form-validation-error"
role="alert"
id="product-error"
>
{fieldErrors.product}
</p>
) : null}
<button
type="submit"
className="w-full rounded border border-transparent bg-indigo-600 px-4 py-2 text-sm font-medium text-white focus:outline-none"
>
Continue with SSO (Hosted Jackson)
</button>
<button
type="submit"
formAction="/auth/sso/embed"
className="w-full rounded border border-transparent bg-indigo-600 px-4 py-2 text-sm font-medium text-white focus:outline-none"
>
Continue with SSO (Embedded Jackson)
</button>
</Form>
</div>
</div>
</div>
);
}