The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
improved form, added more endpoints
Steven Tey3f8b08b6b369
diff --git a/app/app/(auth)/login/form.tsx b/app/app/(auth)/login/form.tsxindex 5e7ac4d3af..5881530d5b 100644--- a/app/app/(auth)/login/form.tsx+++ b/app/app/(auth)/login/form.tsx@@ -7,7 +7,8 @@ import { useSearchParams } from "next/navigation";import { toast } from "sonner";import Button from "#/ui/button";import { Google } from "@/components/shared/icons";-import { SSOWaitlist } from "#/ui/tooltip";+import { InfoTooltip, SSOWaitlist, SimpleTooltipContent } from "#/ui/tooltip";+import { HOME_DOMAIN } from "#/lib/constants";export default function LoginForm() {const searchParams = useSearchParams();@@ -116,9 +117,20 @@ export default function LoginForm() {onSubmit={async (e) => {e.preventDefault();setClickedSSO(true);- signIn("saml-jackson", undefined, {- tenant: "vercel",- product: "dub",+ fetch("/api/auth/sso/verify", {+ method: "POST",+ body: JSON.stringify({ slug: e.currentTarget.slug.value }),+ }).then(async (res) => {+ const { data, error } = await res.json();+ if (error) {+ toast.error(error);+ setClickedSSO(false);+ return;+ }+ await signIn("boxyhq-saml", undefined, {+ tenant: data.projectId,+ product: "dub",+ });});}}className="flex flex-col space-y-3"@@ -126,13 +138,27 @@ export default function LoginForm() {{showSSOOption && (<div><div className="mb-4 mt-1 border-t border-gray-300" />+ <div className="flex items-center space-x-2">+ <h2 className="text-sm font-medium text-gray-900">+ Project Slug+ </h2>+ <InfoTooltip+ content={+ <SimpleTooltipContent+ title={`This is your project's unique identifier on Dub. E.g. app.dub.sh/acme is "acme".`}+ cta="Learn more."+ href={`${HOME_DOMAIN}/help/article/sso`}+ />+ }+ />+ </div><input- id="email"- name="email"+ id="slug"+ name="slug"autoFocus- type="email"- placeholder="[email protected]"- autoComplete="email"+ type="text"+ placeholder="my-team"+ autoComplete="off"requiredvalue={email}onChange={(e) => {diff --git a/lib/jackson.ts b/lib/jackson.tsindex c872a96732..4175208f7a 100644--- a/lib/jackson.ts+++ b/lib/jackson.ts@@ -6,17 +6,20 @@ import type {ISPSAMLConfig,} from "@boxyhq/saml-jackson";-const samlAudience = "https://saml.boxyhq.com";+const samlAudience = "https://saml.dub.sh";const samlPath = "/api/auth/saml/acs";const opts: JacksonOption = {externalUrl: process.env.NEXTAUTH_URL as string,- samlAudience,samlPath,+ samlAudience,db: {engine: "sql",type: "mysql",url: process.env.DATABASE_URL as string,+ ssl: {+ rejectUnauthorized: false,+ },},idpEnabled: true,clientSecretVerifier: process.env.NEXTAUTH_SECRET as string,@@ -24,6 +27,7 @@ const opts: JacksonOption = {declare global {var connectionController: IConnectionAPIController | undefined;+ var apiController: IConnectionAPIController | undefined;var oauthController: IOAuthController | undefined;var samlSPConfig: ISPSAMLConfig | undefined;}@@ -31,17 +35,20 @@ declare global {export default async function init() {if (!globalThis.connectionController ||+ !globalThis.apiController ||!globalThis.oauthController ||!globalThis.samlSPConfig) {const ret = await jackson(opts);globalThis.connectionController = ret.connectionAPIController;+ globalThis.apiController = ret.connectionAPIController;globalThis.oauthController = ret.oauthController;globalThis.samlSPConfig = ret.spConfig;}return {connectionController: globalThis.connectionController,+ apiController: globalThis.apiController,oauthController: globalThis.oauthController,samlSPConfig: globalThis.samlSPConfig,};diff --git a/pages/api/auth/[...nextauth].tsx b/pages/api/auth/[...nextauth].tsxindex 7733264db6..ac48e262e9 100644--- a/pages/api/auth/[...nextauth].tsx+++ b/pages/api/auth/[...nextauth].tsx@@ -1,6 +1,6 @@import EmailProvider from "next-auth/providers/email";import GoogleProvider from "next-auth/providers/google";-import CredentialsProvider from "next-auth/providers/credentials";+import BoxyHQSAMLProvider from "next-auth/providers/boxyhq-saml";import { PrismaAdapter } from "@next-auth/prisma-adapter";import { sendEmail } from "emails";import LoginLink from "emails/login-link";@@ -32,87 +32,14 @@ export const authOptions: NextAuthOptions = {clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,allowDangerousEmailAccountLinking: true,}),- {- id: "saml",- name: "BoxyHQ",- type: "oauth",- checks: ["pkce", "state"],- authorization: {- url: `${process.env.NEXTAUTH_URL}/api/auth/saml/authorize`,- params: {- scope: "",- response_type: "code",- provider: "saml",- },- },- token: {- url: `${process.env.NEXTAUTH_URL}/api/auth/saml/token`,- params: { grant_type: "authorization_code" },- },- userinfo: `${process.env.NEXTAUTH_URL}/api/auth/saml/userinfo`,- profile: (profile) => {- return {- id: profile.id || "",- email: profile.email || "",- name: `${profile.firstName || ""} ${profile.lastName || ""}`.trim(),- email_verified: true,- };- },- options: {- clientId: "dummy",- clientSecret: process.env.NEXTAUTH_SECRET as string,- },+ BoxyHQSAMLProvider({+ authorization: { params: { scope: "" } },+ issuer: process.env.NEXTAUTH_URL,+ clientId: "dummy",+ clientSecret: "dummy",allowDangerousEmailAccountLinking: true,- },- CredentialsProvider({- id: "saml-idp",- name: "IdP Login",- credentials: {- code: {},- },- async authorize(credentials) {- if (!credentials) {- return null;- }-- const { code } = credentials;-- if (!code) {- return null;- }-- const { oauthController } = await (- await import("#/lib/jackson")- ).default();-- // Fetch access token- const { access_token } = await oauthController.token({- code,- grant_type: "authorization_code",- redirect_uri: process.env.NEXTAUTH_URL as string,- client_id: "dummy",- client_secret: process.env.NEXTAUTH_SECRET as string,- });-- if (!access_token) {- return null;- }-- // Fetch user info- const userInfo = await oauthController.userInfo(access_token);-- if (!userInfo) {- return null;- }-- const { id, firstName, lastName, email } = userInfo;-- return {- id,- email,- name: `${firstName} ${lastName}`.trim(),- email_verified: true,- };+ httpOptions: {+ timeout: 30000,},}),],diff --git a/pages/api/auth/saml/callback.ts b/pages/api/auth/saml/callback.tsnew file mode 100644index 0000000000..4d5d6e48d6--- /dev/null+++ b/pages/api/auth/saml/callback.ts@@ -0,0 +1,29 @@+import jackson from "#/lib/jackson";+import { NextApiRequest, NextApiResponse } from "next";++export default async function handler(+ req: NextApiRequest,+ res: NextApiResponse,+) {+ if (req.method == "POST") {+ const { oauthController } = await jackson();++ const { RelayState, SAMLResponse } = req.body;++ const { redirect_url } = await oauthController.samlResponse({+ RelayState,+ SAMLResponse,+ });++ if (!redirect_url) {+ throw new Error("No redirect URL found.");+ }++ return res.redirect(302, redirect_url);+ } else {+ res.setHeader("Allow", "POST");+ return res.status(405).json({+ error: { message: `Method ${req.method} Not Allowed` },+ });+ }+}diff --git a/pages/api/auth/sso/verify.tsx b/pages/api/auth/sso/verify.tsxnew file mode 100644index 0000000000..a5da578166--- /dev/null+++ b/pages/api/auth/sso/verify.tsx@@ -0,0 +1,51 @@+import { NextApiRequest, NextApiResponse } from "next";+import jackson from "#/lib/jackson";+import prisma from "#/lib/prisma";++export default async function handler(+ req: NextApiRequest,+ res: NextApiResponse,+) {+ if (req.method === "POST") {+ const { apiController } = await jackson();++ const { slug } = JSON.parse(req.body) as { slug: string };++ if (!slug) {+ return res.status(400).json({ error: "No project slug provided." });+ }++ const project = await prisma.project.findUnique({+ where: { slug },+ select: { id: true },+ });++ if (!project) {+ return res.status(404).json({ error: "Project not found." });+ }++ const connections = await apiController.getConnections({+ tenant: project.id,+ product: "dub",+ });++ if (!connections || connections.length === 0) {+ return res+ .status(404)+ .json({ error: "No SSO connections found for this project." });+ }++ console.log({ connections });++ const data = {+ projectId: project.id,+ };++ return res.status(200).json({ data });+ } else {+ res.setHeader("Allow", "POST");+ return res.status(405).json({+ error: { message: `Method ${req.method} Not Allowed` },+ });+ }+}