-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathforgot-password.tsx
120 lines (109 loc) · 2.86 KB
/
forgot-password.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
108
109
110
111
112
113
114
115
116
117
118
119
120
import { useMemo } from 'react';
import { sendResetPasswordEmailForUser } from '~/api/supabase-auth.server';
import authenticated from '~/policies/authenticated.server';
import type {
ActionFunction,
LoaderFunction,
MetaFunction,
} from '@remix-run/node';
import {
json,
redirect,
} from '@remix-run/node';
import {
Form,
Link,
useActionData,
useSearchParams,
} from '@remix-run/react';
export const meta: MetaFunction = () => {
return { title: "Supabase x Remix | Forgot password" };
};
export const loader: LoaderFunction = async ({ request }) => {
return authenticated(
request,
() => {
return redirect("/profile");
},
() => {
return json({});
}
);
};
type ActionData = {
formError?: string;
fields?: { email: string };
message?: string;
};
export const action: ActionFunction = async ({ request }) => {
const form = await request.formData();
const email = form.get("email");
const redirectTo = form.get("redirectTo") || "/profile";
if (
!email ||
typeof redirectTo !== "string" ||
typeof email !== "string"
) {
return json<ActionData>(
{
formError: `Form not submitted correctly.`,
fields: {
email: String(email) ?? "",
},
},
403
);
}
try {
await sendResetPasswordEmailForUser({
email,
redirectTo: `${new URL(request.url).origin}/api/auth/callback`,
});
} catch (error) {
return json<ActionData>({
formError: `Failed to send reset password email. Please try again.`,
fields: {
email: String(email) ?? "",
},
})
}
return json<ActionData>(
{
message: `An email has been sent to ${email} with a password reset link. Make sure to check your spam folder.`,
},
);
};
export default function ForgotPassword() {
const actionData = useActionData<ActionData>();
const [searchParams] = useSearchParams();
const redirectTo = useMemo(
() => searchParams.get("redirectTo") ?? "/profile",
[searchParams]
);
return (
<div>
<h1>Reset Password</h1>
<Form method="post">
<fieldset>
<legend>Reset password</legend>
<p>Please enter your email address so we can send you the link to reset your password.</p>
<div style={{ margin: 5 }}>
<label>
Email <input type="email" name="email" required />
</label>
</div>
<div style={{ margin: 5 }}>
<button type="submit">Reset password</button>
<Link style={{ marginLeft: 10 }} to={`/login?redirectTo=${redirectTo}`}>Cancel</Link>
</div>
</fieldset>
</Form>
{actionData?.formError ? (
<p style={{ color: "red" }}>{actionData.formError}</p>
) : null}
{actionData?.message ? (
<p style={{ color: "green" }}>{actionData.message}</p>
) : null}
</div>
)
}