Skip to content

Commit 1202779

Browse files
committed
Implement email whitelist check before sending magic link emails
1 parent 43a5eed commit 1202779

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

apps/webapp/app/routes/login.magic/route.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { InboxArrowDownIcon } from "@heroicons/react/24/solid";
22
import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node";
33
import { redirect } from "@remix-run/node";
4-
import { Form, useNavigation } from "@remix-run/react";
4+
import { Form, useActionData, useNavigation } from "@remix-run/react";
55
import { getMatchesData, metaV1 } from "@remix-run/v1-meta";
66
import {
77
TypedMetaFunction,
@@ -74,6 +74,18 @@ export async function action({ request }: ActionFunctionArgs) {
7474
.parse(payload);
7575

7676
if (action === "send") {
77+
const { email } = z
78+
.object({
79+
email: z.string().email(),
80+
})
81+
.parse(payload);
82+
83+
if (process.env.WHITELISTED_EMAILS && !new RegExp(process.env.WHITELISTED_EMAILS).test(email)) {
84+
return {
85+
error: "This email is unauthorized",
86+
};
87+
}
88+
7789
return authenticator.authenticate("email-link", request, {
7890
successRedirect: "/login/magic",
7991
failureRedirect: "/login/magic",
@@ -92,6 +104,7 @@ export async function action({ request }: ActionFunctionArgs) {
92104

93105
export default function LoginMagicLinkPage() {
94106
const { magicLinkSent, magicLinkError } = useTypedLoaderData<typeof loader>();
107+
const actionData = useActionData<typeof action>();
95108
const navigate = useNavigation();
96109

97110
const isLoading =
@@ -176,6 +189,7 @@ export default function LoginMagicLinkPage() {
176189
/>
177190
{isLoading ? "Sending…" : "Send a magic link"}
178191
</Button>
192+
{actionData?.error && <FormError>{actionData?.error}</FormError>}
179193
{magicLinkError && <FormError>{magicLinkError}</FormError>}
180194
</Fieldset>
181195
<Paragraph variant="extra-small" className="mb-4 mt-6 text-center">

0 commit comments

Comments
 (0)