Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

move auth endpoints to App Router

Steven Tey8e160dbe5c5f
diff --git a/apps/web/app/admin.dub.co/actions.ts b/apps/web/app/admin.dub.co/actions.ts
index 64752a9b06..088023c06a 100644
--- a/apps/web/app/admin.dub.co/actions.ts
+++ b/apps/web/app/admin.dub.co/actions.ts
@@ -1,16 +1,14 @@
"use server";
import { deleteUserLinks } from "@/lib/api/links";
import { deleteProject } from "@/lib/api/project";
-import { hashToken } from "@/lib/auth";
+import { getSession, hashToken } from "@/lib/auth";
import prisma from "@/lib/prisma";
-import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { DUB_PROJECT_ID, getDomainWithoutWWW } from "@dub/utils";
import { get } from "@vercel/edge-config";
import { randomBytes } from "crypto";
-import { getServerSession } from "next-auth";
async function isAdmin() {
- const session = await getServerSession(authOptions);
+ const session = await getSession();
if (!session?.user) return false;
const response = await prisma.projectUsers.findUnique({
where: {
diff --git a/apps/web/app/api/auth/[...nextauth]/route.tsx b/apps/web/app/api/auth/[...nextauth]/route.tsx
new file mode 100644
index 0000000000..fa3a658258
--- /dev/null
+++ b/apps/web/app/api/auth/[...nextauth]/route.tsx
@@ -0,0 +1,6 @@
+import NextAuth from "next-auth";
+import { authOptions } from "@/lib/auth/options";
+
+const handler = NextAuth(authOptions);
+
+export { handler as GET, handler as POST };
diff --git a/apps/web/app/api/auth/account-exists/route.ts b/apps/web/app/api/auth/account-exists/route.ts
new file mode 100644
index 0000000000..367e29e905
--- /dev/null
+++ b/apps/web/app/api/auth/account-exists/route.ts
@@ -0,0 +1,39 @@
+import { isWhitelistedEmail } from "@/lib/edge-config";
+import { conn } from "@/lib/planetscale";
+import { ratelimit } from "@/lib/upstash";
+import { LOCALHOST_IP } from "@dub/utils";
+import { ipAddress } from "@vercel/edge";
+import { NextRequest, NextResponse } from "next/server";
+
+export const runtime = "edge";
+
+export async function POST(req: NextRequest) {
+ const ip = ipAddress(req) || LOCALHOST_IP;
+ const { success } = await ratelimit(5, "1 m").limit(ip);
+ if (!success) {
+ return new Response("Don't DDoS me pls 🥺", { status: 429 });
+ }
+
+ const { email } = (await req.json()) as { email: string };
+
+ if (!conn) {
+ return new Response("Database connection not established", {
+ status: 500,
+ });
+ }
+
+ const user = await conn
+ .execute("SELECT email FROM User WHERE email = ?", [email])
+ .then((res) => res.rows[0]);
+
+ if (user) {
+ return NextResponse.json({ exists: true });
+ }
+
+ const whitelisted = await isWhitelistedEmail(email);
+ if (whitelisted) {
+ return NextResponse.json({ exists: true });
+ }
+
+ return NextResponse.json({ exists: false });
+}
diff --git a/apps/web/app/api/auth/saml/authorize/route.ts b/apps/web/app/api/auth/saml/authorize/route.ts
new file mode 100644
index 0000000000..524190b05e
--- /dev/null
+++ b/apps/web/app/api/auth/saml/authorize/route.ts
@@ -0,0 +1,26 @@
+import jackson from "@/lib/jackson";
+import { getSearchParams } from "@/lib/auth";
+import { redirect } from "next/navigation";
+
+const handler = async (req: Request) => {
+ const { oauthController } = await jackson();
+
+ const requestParams =
+ req.method === "GET" ? getSearchParams(req.url) : await req.json();
+
+ const { redirect_url, authorize_form } = await oauthController.authorize(
+ requestParams,
+ );
+
+ if (redirect_url) {
+ redirect(redirect_url);
+ } else {
+ return new Response(authorize_form, {
+ headers: {
+ "Content-Type": "text/html; charset=utf-8",
+ },
+ });
+ }
+};
+
+export { handler as GET, handler as POST };
diff --git a/apps/web/app/api/auth/saml/callback/route.ts b/apps/web/app/api/auth/saml/callback/route.ts
new file mode 100644
index 0000000000..87457a6f25
--- /dev/null
+++ b/apps/web/app/api/auth/saml/callback/route.ts
@@ -0,0 +1,21 @@
+import jackson from "@/lib/jackson";
+import { redirect } from "next/navigation";
+
+export async function POST(req: Request) {
+ const { oauthController } = await jackson();
+
+ const body = await req.json();
+
+ const { RelayState, SAMLResponse } = body;
+
+ const { redirect_url } = await oauthController.samlResponse({
+ RelayState,
+ SAMLResponse,
+ });
+
+ if (!redirect_url) {
+ throw new Error("No redirect URL found.");
+ }
+
+ redirect(redirect_url);
+}
diff --git a/apps/web/app/api/auth/saml/token/route.ts b/apps/web/app/api/auth/saml/token/route.ts
new file mode 100644
index 0000000000..0c59f07743
--- /dev/null
+++ b/apps/web/app/api/auth/saml/token/route.ts
@@ -0,0 +1,12 @@
+import jackson from "@/lib/jackson";
+import { NextResponse } from "next/server";
+
+export async function POST(req: Request) {
+ const { oauthController } = await jackson();
+
+ const body = await req.json();
+
+ const response = await oauthController.token(body);
+
+ return NextResponse.json(response);
+}
diff --git a/apps/web/app/api/auth/saml/userinfo/index.ts b/apps/web/app/api/auth/saml/userinfo/index.ts
new file mode 100644
index 0000000000..5c20c08f74
--- /dev/null
+++ b/apps/web/app/api/auth/saml/userinfo/index.ts
@@ -0,0 +1,37 @@
+import jackson from "@/lib/jackson";
+import { NextResponse } from "next/server";
+
+// export default async function handler(
+// req: NextApiRequest,
+// res: NextApiResponse,
+// ) {
+// const { oauthController } = await jackson();
+
+// const authHeader = req.headers["authorization"];
+
+// if (!authHeader) {
+// throw new Error("Unauthorized");
+// }
+
+// const token = authHeader.split(" ")[1];
+
+// const user = await oauthController.userInfo(token);
+
+// return res.json(user);
+// }
+
+export async function GET(req: Request) {
+ const { oauthController } = await jackson();
+
+ const authHeader = req.headers.get("Authorization");
+
+ if (!authHeader) {
+ throw new Error("Unauthorized");
+ }
+
+ const token = authHeader.split(" ")[1];
+
+ const user = await oauthController.userInfo(token);
+
+ return NextResponse.json(user);
+}
diff --git a/apps/web/app/api/auth/saml/verify/route.tsx b/apps/web/app/api/auth/saml/verify/route.tsx
new file mode 100644
index 0000000000..19016227b1
--- /dev/null
+++ b/apps/web/app/api/auth/saml/verify/route.tsx
@@ -0,0 +1,43 @@
+import jackson from "@/lib/jackson";
+import prisma from "@/lib/prisma";
+import { NextResponse } from "next/server";
+
+export async function POST(req: Request) {
+ const { apiController } = await jackson();
+
+ const { slug } = await req.json();
+
+ if (!slug) {
+ return NextResponse.json(
+ { error: "No project slug provided." },
+ { status: 400 },
+ );
+ }
+
+ const project = await prisma.project.findUnique({
+ where: { slug },
+ select: { id: true },
+ });
+
+ if (!project) {
+ return NextResponse.json({ error: "Project not found." }, { status: 404 });
+ }
+
+ const connections = await apiController.getConnections({
+ tenant: project.id,
+ product: "Dub",
+ });
+
+ if (!connections || connections.length === 0) {
+ return NextResponse.json(
+ { error: "No SSO connections found for this project." },
+ { status: 404 },
+ );
+ }
+
+ const data = {
+ projectId: project.id,
+ };
+
+ return NextResponse.json({ data });
+}
diff --git a/apps/web/lib/auth.ts b/apps/web/lib/auth/index.ts
similarity index 98%
rename from apps/web/lib/auth.ts
rename to apps/web/lib/auth/index.ts
index 6b06dec55d..f845c8ab08 100644
--- a/apps/web/lib/auth.ts
+++ b/apps/web/lib/auth/index.ts
@@ -1,11 +1,11 @@
+import { authOptions } from "./options";
import prisma from "@/lib/prisma";
-import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { Link as LinkProps } from "@prisma/client";
-import { PlanProps, ProjectProps } from "./types";
+import { PlanProps, ProjectProps } from "../types";
import { getServerSession } from "next-auth/next";
import { createHash } from "crypto";
import { API_DOMAIN } from "@dub/utils";
-import { ratelimit } from "./upstash";
+import { ratelimit } from "../upstash";
export interface Session {
user: {
diff --git a/apps/web/lib/auth-links.ts b/apps/web/lib/auth/links.ts
similarity index 97%
rename from apps/web/lib/auth-links.ts
rename to apps/web/lib/auth/links.ts
index 1fe94e3606..562b70aff4 100644
--- a/apps/web/lib/auth-links.ts
+++ b/apps/web/lib/auth/links.ts
@@ -1,12 +1,12 @@
+import { authOptions } from "./options";
import { NextApiRequest, NextApiResponse } from "next";
import { API_DOMAIN } from "@dub/utils";
-import { ratelimit } from "./upstash";
-import { Session, hashToken } from "./auth";
+import { ratelimit } from "../upstash";
+import { Session, hashToken } from ".";
import { Link as LinkProps } from "@prisma/client";
-import { PlanProps, ProjectProps } from "./types";
+import { PlanProps, ProjectProps } from "../types";
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
-import { authOptions } from "@/pages/api/auth/[...nextauth]";
interface WithLinksAuthNextApiHandler {
(
diff --git a/apps/web/pages/api/auth/[...nextauth].tsx b/apps/web/lib/auth/options.ts
similarity index 98%
rename from apps/web/pages/api/auth/[...nextauth].tsx
rename to apps/web/lib/auth/options.ts
index 4637d3fe72..85265b60ae 100644
--- a/apps/web/pages/api/auth/[...nextauth].tsx
+++ b/apps/web/lib/auth/options.ts
@@ -5,7 +5,7 @@ import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { sendEmail } from "emails";
import LoginLink from "emails/login-link";
import WelcomeEmail from "emails/welcome-email";
-import NextAuth, { type NextAuthOptions } from "next-auth";
+import { type NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import EmailProvider from "next-auth/providers/email";
import GoogleProvider from "next-auth/providers/google";
@@ -312,5 +312,3 @@ export const authOptions: NextAuthOptions = {
},
},
};
-
-export default NextAuth(authOptions);
diff --git a/apps/web/pages/api/auth/account-exists.ts b/apps/web/pages/api/auth/account-exists.ts
deleted file mode 100644
index dcb6a5aac1..0000000000
--- a/apps/web/pages/api/auth/account-exists.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-import { isWhitelistedEmail } from "@/lib/edge-config";
-import { conn } from "@/lib/planetscale";
-import { ratelimit } from "@/lib/upstash";
-import { LOCALHOST_IP } from "@dub/utils";
-import { ipAddress } from "@vercel/edge";
-import { NextRequest } from "next/server";
-
-export const config = {
- runtime: "edge",
-};
-
-export default async function handler(req: NextRequest) {
- if (req.method === "POST") {
- const ip = ipAddress(req) || LOCALHOST_IP;
- const { success } = await ratelimit(5, "1 m").limit(ip);
- if (!success) {
- return new Response("Don't DDoS me pls 🥺", { status: 429 });
- }
-
- const { email } = (await req.json()) as { email: string };
-
- if (!conn) {
- return new Response("Database connection not established", {
- status: 500,
- });
- }
-
- const user = await conn
- .execute("SELECT email FROM User WHERE email = ?", [email])
- .then((res) => res.rows[0]);
-
- if (user) {
- return new Response(JSON.stringify({ exists: true }));
- }
-
- const whitelisted = await isWhitelistedEmail(email);
- if (whitelisted) {
- return new Response(JSON.stringify({ exists: true }));
- }
-
- return new Response(JSON.stringify({ exists: false }));
- } else {
- return new Response("Method not allowed", {
- status: 405,
- statusText: "Method not allowed",
- });
- }
-}
diff --git a/apps/web/pages/api/auth/saml/authorize.ts b/apps/web/pages/api/auth/saml/authorize.ts
deleted file mode 100644
index b875aab236..0000000000
--- a/apps/web/pages/api/auth/saml/authorize.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import jackson from "@/lib/jackson";
-import { NextApiRequest, NextApiResponse } from "next";
-
-export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse,
-) {
- if (req.method === "GET" || req.method === "POST") {
- const { oauthController } = await jackson();
-
- const requestParams = req.method === "GET" ? req.query : req.body;
-
- const { redirect_url, authorize_form } = await oauthController.authorize(
- requestParams,
- );
-
- if (redirect_url) {
- res.redirect(302, redirect_url);
- } else {
- res.setHeader("Content-Type", "text/html; charset=utf-8");
- res.send(authorize_form);
- }
- } else {
- res.setHeader("Allow", "GET, POST");
- res.status(405).json({
- error: { message: `Method ${req.method} Not Allowed` },
- });
- }
-}
diff --git a/apps/web/pages/api/auth/saml/callback.ts b/apps/web/pages/api/auth/saml/callback.ts
deleted file mode 100644
index b0aea6a1b2..0000000000
--- a/apps/web/pages/api/auth/saml/callback.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-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/apps/web/pages/api/auth/saml/token.ts b/apps/web/pages/api/auth/saml/token.ts
deleted file mode 100644
index 34049f4c14..0000000000
--- a/apps/web/pages/api/auth/saml/token.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import type { NextApiRequest, NextApiResponse } from "next";
-
-import jackson from "@/lib/jackson";
-
-export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse,
-) {
- const { oauthController } = await jackson();
-
- const response = await oauthController.token(req.body);
-
- return res.json(response);
-}
diff --git a/apps/web/pages/api/auth/saml/userinfo.ts b/apps/web/pages/api/auth/saml/userinfo.ts
deleted file mode 100644
index 636cf937e5..0000000000
--- a/apps/web/pages/api/auth/saml/userinfo.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-import type { NextApiRequest, NextApiResponse } from "next";
-
-import jackson from "@/lib/jackson";
-
-export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse,
-) {
- const { oauthController } = await jackson();
-
- const authHeader = req.headers["authorization"];
-
- if (!authHeader) {
- throw new Error("Unauthorized");
- }
-
- const token = authHeader.split(" ")[1];
-
- const user = await oauthController.userInfo(token);
-
- return res.json(user);
-}
diff --git a/apps/web/pages/api/auth/saml/verify.tsx b/apps/web/pages/api/auth/saml/verify.tsx
deleted file mode 100644
index 7c77b5229b..0000000000
--- a/apps/web/pages/api/auth/saml/verify.tsx
+++ /dev/null
@@ -1,49 +0,0 @@
-import jackson from "@/lib/jackson";
-import prisma from "@/lib/prisma";
-import { NextApiRequest, NextApiResponse } from "next";
-
-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." });
- }
-
- 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` },
- });
- }
-}
diff --git a/apps/web/pages/api/links/[key]/index.ts b/apps/web/pages/api/links/[key]/index.ts
index 7cfba274f2..ea2ef2ea7a 100644
--- a/apps/web/pages/api/links/[key]/index.ts
+++ b/apps/web/pages/api/links/[key]/index.ts
@@ -1,5 +1,5 @@
import { deleteLink, editLink, processKey } from "@/lib/api/links";
-import { withLinksAuth } from "@/lib/auth-links";
+import { withLinksAuth } from "@/lib/auth/links";
import { isBlacklistedDomain, isBlacklistedKey } from "@/lib/edge-config";
import { GOOGLE_FAVICON_URL, getApexDomain, log } from "@dub/utils";
diff --git a/apps/web/pages/api/links/index.ts b/apps/web/pages/api/links/index.ts
index f161ebda6f..7b5e77b6f4 100644
--- a/apps/web/pages/api/links/index.ts
+++ b/apps/web/pages/api/links/index.ts
@@ -4,7 +4,7 @@ import {
getRandomKey,
processKey,
} from "@/lib/api/links";
-import { withLinksAuth } from "@/lib/auth-links";
+import { withLinksAuth } from "@/lib/auth/links";
import { isBlacklistedDomain, isBlacklistedKey } from "@/lib/edge-config";
import {
DUB_PROJECT_ID,