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.

final changes, used middleware to handle newUser

Steven Teybd36f76ffe91
diff --git a/app/ui/home/nav.tsx b/app/ui/home/nav.tsx
index 6c9f13078f..56a99344e3 100644
--- a/app/ui/home/nav.tsx
+++ b/app/ui/home/nav.tsx
@@ -69,7 +69,7 @@ export default function Nav() {
? "https://app.dub.sh/register"
: "http://app.localhost:3000/register"
}
- className="rounded-full border border-black bg-black px-4 py-1 text-sm text-white transition-all hover:bg-white hover:text-black"
+ className="rounded-full border border-black bg-black px-4 py-1.5 text-sm text-white transition-all hover:bg-white hover:text-black"
>
Sign Up
</Link>
diff --git a/components/app/modals/add-edit-link-modal/index.tsx b/components/app/modals/add-edit-link-modal/index.tsx
index ab0735ee02..b09c2f2d88 100644
--- a/components/app/modals/add-edit-link-modal/index.tsx
+++ b/components/app/modals/add-edit-link-modal/index.tsx
@@ -312,7 +312,7 @@ function AddEditLinkModal({
{ revalidate: true },
);
// for welcome page, redirect to links page after adding a link
- if (router.asPath === "/welcome") {
+ if (router.pathname === "/app/welcome") {
router.push("/links").then(() => {
setShowAddEditLinkModal(false);
});
diff --git a/lib/api/links.ts b/lib/api/links.ts
index 3885ccc0ca..7fb066bad9 100644
--- a/lib/api/links.ts
+++ b/lib/api/links.ts
@@ -383,21 +383,27 @@ export async function deleteUserLinks(userId: string) {
links.forEach(({ key }) => {
pipeline.del(`dub.sh:${key}`);
});
- return await Promise.allSettled([
- pipeline.exec(), // delete all links from redis
- // remove all images from cloudinary
- ...links.map(({ key, proxy }) =>
- proxy
- ? cloudinary.v2.uploader.destroy(`dub.sh/${key}`, {
- invalidate: true,
- })
- : Promise.resolve(),
- ),
- prisma.link.deleteMany({
- where: {
- userId,
- domain: "dub.sh",
- },
- }),
- ]);
+ const [deleteRedis, deleteCloudinary, deletePrisma] =
+ await Promise.allSettled([
+ pipeline.exec(), // delete all links from redis
+ // remove all images from cloudinary
+ ...links.map(({ key, proxy }) =>
+ proxy
+ ? cloudinary.v2.uploader.destroy(`dub.sh/${key}`, {
+ invalidate: true,
+ })
+ : Promise.resolve(),
+ ),
+ prisma.link.deleteMany({
+ where: {
+ userId,
+ domain: "dub.sh",
+ },
+ }),
+ ]);
+ return {
+ deleteRedis,
+ deleteCloudinary,
+ deletePrisma,
+ };
}
diff --git a/lib/cron/domains.tsx b/lib/cron/domains.tsx
index 3d77bf46d7..568df64b28 100644
--- a/lib/cron/domains.tsx
+++ b/lib/cron/domains.tsx
@@ -98,7 +98,7 @@ export const handleDomainUpdates = async ({
}),
]);
} else {
- console.log(
+ await log(
`Domain *${domain}* has been invalid for > 30 days but has links, not deleting.`,
"cron",
true,
diff --git a/lib/middleware/app.ts b/lib/middleware/app.ts
index 776bb97c86..b83e598e95 100644
--- a/lib/middleware/app.ts
+++ b/lib/middleware/app.ts
@@ -1,14 +1,35 @@
import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
import { parse } from "@/lib/middleware/utils";
+import { UserProps } from "../types";
export default async function AppMiddleware(req: NextRequest) {
const { path } = parse(req);
- const session = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
+ const session = (await getToken({
+ req,
+ secret: process.env.NEXTAUTH_SECRET,
+ })) as {
+ email?: string;
+ user?: UserProps;
+ };
+ // if there's no session and the path isn't /login or /register, redirect to /login
if (!session?.email && path !== "/login" && path !== "/register") {
return NextResponse.redirect(new URL("/login", req.url));
+
+ // if there's a session and the path is /login or /register, redirect to /
} else if (session?.email && (path === "/login" || path === "/register")) {
- return NextResponse.redirect(new URL("/", req.url));
+ // only redirect if the user was created in the last 10 seconds
+ // (this is a workaround because the `isNewUser` flag is triggered when a user does `dangerousEmailAccountLinking`)
+ if (
+ session?.user?.createdAt &&
+ new Date(session?.user?.createdAt).getTime() > Date.now() - 10000
+ ) {
+ return NextResponse.redirect(new URL("/welcome", req.url));
+ } else {
+ return NextResponse.redirect(new URL("/", req.url));
+ }
}
+
+ // otherwise, rewrite the path to /app
return NextResponse.rewrite(new URL(`/app${path}`, req.url));
}
diff --git a/lib/types.ts b/lib/types.ts
index ab4c8d12f9..c7d44c9eed 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -76,10 +76,7 @@ export interface UserProps {
name: string;
email: string;
image?: string;
-
- // TO DELETE
- stripeId: string;
- usageLimit: number;
+ createdAt: Date;
joinedAt?: Date;
projects?: { projectId: string }[];
diff --git a/pages/api/auth/[...nextauth].tsx b/pages/api/auth/[...nextauth].tsx
index f60de6edea..013fd1b2e1 100644
--- a/pages/api/auth/[...nextauth].tsx
+++ b/pages/api/auth/[...nextauth].tsx
@@ -104,11 +104,11 @@ export const authOptions: NextAuthOptions = {
createdAt: true,
},
});
- // only send the welcome email if the user was created in the last 1 minute
+ // only send the welcome email if the user was created in the last 10 seconds
// (this is a workaround because the `isNewUser` flag is triggered when a user does `dangerousEmailAccountLinking`)
if (
user?.createdAt &&
- new Date(user.createdAt).getTime() > Date.now() - 60000
+ new Date(user.createdAt).getTime() > Date.now() - 10000
) {
sendMarketingMail({
subject: "Welcome to Dub.sh!",
diff --git a/pages/api/user/index.ts b/pages/api/user/index.ts
index 1ae562fa3f..10471708f1 100644
--- a/pages/api/user/index.ts
+++ b/pages/api/user/index.ts
@@ -44,10 +44,6 @@ export default withUserAuth(async (req, res, session) => {
id: session.user.id,
},
});
- console.log({
- deleteLinks: JSON.stringify(deleteLinks),
- response,
- });
return res.status(200).json({ deleteLinks, response });
}
} else {
diff --git a/pages/app/register.tsx b/pages/app/register.tsx
index 497d3ba176..3ea3a986af 100644
--- a/pages/app/register.tsx
+++ b/pages/app/register.tsx
@@ -35,9 +35,7 @@ export default function Register() {
text="Continue with Google"
onClick={() => {
setClickedGoogle(true);
- signIn("google", {
- callbackUrl: "/welcome",
- });
+ signIn("google");
}}
loading={clickedGoogle}
icon={<Google className="h-4 w-4" />}