Skip to content

Commit 7471825

Browse files
committed
add honey pot
1 parent 7d1922f commit 7471825

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

apps/web/app/pages/_auth.register.tsx

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { registerSchema } from "@element/server-schemas"
22
import { createTemplates, hashPassword, sendAccountVerificationEmail, sendSlackMessage, stripe } from "@element/server-services"
33
import type { ActionFunctionArgs, MetaFunction } from "@remix-run/node"
4-
import { Link } from "@remix-run/react"
4+
import { Link, json, useLoaderData } from "@remix-run/react"
5+
import { HoneypotInputs, HoneypotProvider } from "remix-utils/honeypot/react"
6+
import { SpamError } from "remix-utils/honeypot/server"
57

68
import { Form, FormButton, FormError, FormField } from "~/components/ui/Form"
79
import { db } from "~/lib/db.server"
810
import { FORM_ACTION } from "~/lib/form"
911
import { formError, validateFormData } from "~/lib/form.server"
1012
import { createToken } from "~/lib/jwt.server"
1113
import { badRequest, redirect } from "~/lib/remix"
14+
import { honeypot } from "~/services/honeypot.server"
1215
import { getUserSession } from "~/services/session/session.server"
1316

1417
export const meta: MetaFunction = () => {
@@ -32,6 +35,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
3235
switch (action) {
3336
case RegisterActionMethods.Register:
3437
try {
38+
honeypot.check(formData)
3539
if (formData.get("passwordConfirmation")) return redirect("/")
3640

3741
const result = await validateFormData(request, registerSchema)
@@ -63,9 +67,12 @@ export const action = async ({ request }: ActionFunctionArgs) => {
6367
},
6468
headers,
6569
})
66-
} catch (e: unknown) {
67-
if (e instanceof Error) {
68-
return badRequest(e.message)
70+
} catch (error: unknown) {
71+
if (error instanceof SpamError) {
72+
return badRequest("Error")
73+
}
74+
if (error instanceof Error) {
75+
return badRequest(error.message)
6976
}
7077
return badRequest("Something went wrong")
7178
}
@@ -75,34 +82,42 @@ export const action = async ({ request }: ActionFunctionArgs) => {
7582
}
7683
}
7784

85+
export const loader = () => {
86+
return json({ honeypotInputProps: honeypot.getInputProps() })
87+
}
88+
7889
export default function Register() {
90+
const { honeypotInputProps } = useLoaderData<typeof loader>()
7991
return (
80-
<div>
81-
<Form method="post" replace>
82-
<div className="space-y-2">
83-
<h1 className="text-4xl font-bold">Register</h1>
84-
<FormField required label="Email address" name="email" placeholder="jim@gmail.com" />
85-
<FormField required label="Password" name="password" type="password" placeholder="********" />
86-
<input name="passwordConfirmation" className="hidden" />
87-
<FormField required label="First name" name="firstName" placeholder="Jim" />
88-
<FormField required label="Last name" name="lastName" placeholder="Bob" />
89-
<div>
90-
<FormButton name="_action" value={RegisterActionMethods.Register} className="w-full">
91-
Register
92-
</FormButton>
93-
<FormError />
94-
</div>
92+
<HoneypotProvider {...honeypotInputProps}>
93+
<div>
94+
<Form method="post" replace>
95+
<div className="space-y-2">
96+
<h1 className="text-4xl font-bold">Register</h1>
97+
<HoneypotInputs />
98+
<FormField required label="Email address" name="email" placeholder="jim@gmail.com" />
99+
<FormField required label="Password" name="password" type="password" placeholder="********" />
100+
<input name="passwordConfirmation" className="hidden" />
101+
<FormField required label="First name" name="firstName" placeholder="Jim" />
102+
<FormField required label="Last name" name="lastName" placeholder="Bob" />
103+
<div>
104+
<FormButton name="_action" value={RegisterActionMethods.Register} className="w-full">
105+
Register
106+
</FormButton>
107+
<FormError />
108+
</div>
95109

96-
<div className="flex justify-between">
97-
<Link to="/login" className="hover:opacity-70">
98-
Login
99-
</Link>
100-
<Link to="/forgot-password" className="hover:opacity-70">
101-
Forgot password?
102-
</Link>
110+
<div className="flex justify-between">
111+
<Link to="/login" className="hover:opacity-70">
112+
Login
113+
</Link>
114+
<Link to="/forgot-password" className="hover:opacity-70">
115+
Forgot password?
116+
</Link>
117+
</div>
103118
</div>
104-
</div>
105-
</Form>
106-
</div>
119+
</Form>
120+
</div>
121+
</HoneypotProvider>
107122
)
108123
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Honeypot } from "remix-utils/honeypot/server"
2+
3+
export const honeypot = new Honeypot({
4+
randomizeNameFieldName: false,
5+
encryptionSeed: undefined, // Ideally it should be unique even between processes
6+
})

0 commit comments

Comments
 (0)