-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.sso.embed.tsx
45 lines (40 loc) · 1.5 KB
/
auth.sso.embed.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
import type { ActionFunction } from "@remix-run/node";
import { redirect } from "@remix-run/node";
import { auth } from "~/auth.server";
import { commitSession, getSession } from "~/sessions.server";
import { validateEmail, validateProduct } from "~/utils.server";
export const action: ActionFunction = async ({ request }) => {
const session = await getSession(request.headers.get("Cookie"));
const formData = await request.formData();
const email = formData.get("email");
const product = formData.get("product");
if (typeof email !== "string" || typeof product !== "string") {
session.flash("login:form:error", {
formError: "Form not submitted correctly",
});
return redirect("/login", {
headers: { "Set-Cookie": await commitSession(session) },
});
}
const fieldErrors = {
email: validateEmail(email),
product: validateProduct(product),
};
const fields = { email, product };
if (fieldErrors.email || fieldErrors.product) {
session.flash("login:form:error", { fieldErrors, fields });
return redirect("/login", {
headers: { "Set-Cookie": await commitSession(session) },
});
}
// extracting the tenant from email is one way to set it
const tenant = email.split("@")[1];
return await auth.authenticate("boxyhq-sso-embed", request, {
successRedirect: "/private",
failureRedirect: "/login",
context: {
clientID: `tenant=${tenant}&product=${product}`,
clientSecret: process.env.CLIENT_SECRET_VERIFIER || "dummy",
},
});
};