-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmagic.tsx
More file actions
56 lines (47 loc) · 1.59 KB
/
magic.tsx
File metadata and controls
56 lines (47 loc) · 1.59 KB
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
import type { LoaderFunctionArgs } from "@remix-run/server-runtime";
import { redirect } from "@remix-run/server-runtime";
import { prisma } from "~/db.server";
import { redirectWithErrorMessage } from "~/models/message.server";
import { authenticator } from "~/services/auth.server";
import { getRedirectTo } from "~/services/redirectTo.server";
import { commitSession, getSession } from "~/services/sessionStorage.server";
export async function loader({ request }: LoaderFunctionArgs) {
const redirectTo = await getRedirectTo(request);
const auth = await authenticator.authenticate("email-link", request, {
failureRedirect: "/login/magic", // If auth fails, the failureRedirect will be thrown as a Response
});
// manually get the session
const session = await getSession(request.headers.get("cookie"));
const userRecord = await prisma.user.findFirst({
where: {
id: auth.userId,
},
select: {
id: true,
mfaEnabledAt: true,
},
});
if (!userRecord) {
return redirectWithErrorMessage(
"/login/magic",
request,
"Could not find your account. Please contact support."
);
}
if (userRecord.mfaEnabledAt) {
session.set("pending-mfa-user-id", userRecord.id);
session.set("pending-mfa-redirect-to", redirectTo ?? "/");
return redirect("/login/mfa", {
headers: {
"Set-Cookie": await commitSession(session),
},
});
}
// and store the user data
session.set(authenticator.sessionKey, auth);
return redirect(redirectTo ?? "/", {
headers: {
"Set-Cookie": await commitSession(session),
},
});
}