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.

added `next` query param to redirect folks to requested path after sign in

Steven Teya9448a9da714
diff --git a/components/layout/app/user-dropdown.tsx b/components/layout/app/user-dropdown.tsx
index c65413df66..d66a228b9d 100644
--- a/components/layout/app/user-dropdown.tsx
+++ b/components/layout/app/user-dropdown.tsx
@@ -70,7 +70,9 @@ export default function UserDropdown() {
<button
className="w-full rounded-md p-2 text-sm transition-all duration-75 hover:bg-gray-100 active:bg-gray-200"
onClick={() => {
- signOut();
+ signOut({
+ callbackUrl: "/login",
+ });
// track logout event
va.track("Logout");
}}
diff --git a/lib/middleware/app.ts b/lib/middleware/app.ts
index b83e598e95..8194e5ab1b 100644
--- a/lib/middleware/app.ts
+++ b/lib/middleware/app.ts
@@ -14,18 +14,26 @@ export default async function AppMiddleware(req: NextRequest) {
};
// 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));
+ return NextResponse.redirect(
+ new URL(
+ `/login?next=${encodeURIComponent(new URL(req.url).pathname)}`,
+ req.url,
+ ),
+ );
- // if there's a session and the path is /login or /register, redirect to /
- } else if (session?.email && (path === "/login" || path === "/register")) {
- // only redirect if the user was created in the last 10 seconds
+ // if there's a session
+ } else if (session?.email) {
+ // if the user was created in the last 10 seconds, redirect to "/welcome"
// (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
+ new Date(session?.user?.createdAt).getTime() > Date.now() - 10000 &&
+ path !== "/welcome"
) {
return NextResponse.redirect(new URL("/welcome", req.url));
- } else {
+
+ // if the path is /login or /register, redirect to "/"
+ } else if (path === "/login" || path === "/register") {
return NextResponse.redirect(new URL("/", req.url));
}
}
diff --git a/pages/app/login.tsx b/pages/app/login.tsx
index 0a11baf548..7328f6e6ef 100644
--- a/pages/app/login.tsx
+++ b/pages/app/login.tsx
@@ -8,8 +8,11 @@ import { toast } from "sonner";
import Button from "#/ui/button";
import { Google } from "@/components/shared/icons";
import { SSOWaitlist } from "#/ui/tooltip";
+import { useRouter } from "next/router";
export default function Login() {
+ const router = useRouter();
+ const { next } = router.query as { next?: string };
const [showEmailOption, setShowEmailOption] = useState(false);
const [noSuchAccount, setNoSuchAccount] = useState(false);
const [email, setEmail] = useState("");
@@ -47,7 +50,9 @@ export default function Login() {
text="Continue with Google"
onClick={() => {
setClickedGoogle(true);
- signIn("google");
+ signIn("google", {
+ ...(next && next.length > 0 ? { callbackUrl: next } : {}),
+ });
}}
loading={clickedGoogle}
disabled={clickedEmail}
@@ -68,6 +73,7 @@ export default function Login() {
signIn("email", {
email,
redirect: false,
+ ...(next ? { callbackUrl: next } : {}),
}).then((res) => {
setClickedEmail(false);
if (res?.ok && !res?.error) {
diff --git a/pages/app/register.tsx b/pages/app/register.tsx
index daa261f723..cbc0cca520 100644
--- a/pages/app/register.tsx
+++ b/pages/app/register.tsx
@@ -7,8 +7,11 @@ import { Google } from "@/components/shared/icons";
import Button from "#/ui/button";
import { SSOWaitlist } from "#/ui/tooltip";
import Link from "next/link";
+import { useRouter } from "next/router";
export default function Register() {
+ const router = useRouter();
+ const { next } = router.query as { next?: string };
const [clickedGoogle, setClickedGoogle] = useState(false);
return (
@@ -43,7 +46,9 @@ export default function Register() {
text="Continue with Google"
onClick={() => {
setClickedGoogle(true);
- signIn("google");
+ signIn("google", {
+ ...(next && next.length > 0 ? { callbackUrl: next } : {}),
+ });
}}
loading={clickedGoogle}
icon={<Google className="h-4 w-4" />}