Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

1036 redirect to original page after login (#1042)

Oreoluwa Bimbo-Salamic09bb0c95778
diff --git a/apps/web/src/app/[locale]/login/page.tsx b/apps/web/src/app/[locale]/login/page.tsx
index a3b1718d..1abf4525 100644
--- a/apps/web/src/app/[locale]/login/page.tsx
+++ b/apps/web/src/app/[locale]/login/page.tsx
@@ -1,8 +1,13 @@
import Link from 'next/link';
import { LoginButton } from './_components/LoginButton';
-// @TODO: add a redirect param to send users back to previous page
-export default async function Index() {
+export default async function Index({
+ searchParams,
+}: {
+ searchParams: {
+ redirectTo: string | null;
+ };
+}) {
return (
<>
<div className="container -mt-[56px] flex h-screen flex-col items-center justify-center">
@@ -16,7 +21,7 @@ export default async function Index() {
Start your typescript journey by logging in below.
</p>
</div>
- <LoginButton redirectTo="/explore" />
+ <LoginButton redirectTo={searchParams.redirectTo ?? '/explore'} />
<p className="text-muted-foreground mx-auto px-8 text-sm sm:w-[350px]">
By clicking Login, you agree to our <br />
<Link href="/tos" className="hover:text-primary underline underline-offset-4">
diff --git a/apps/web/src/components/navigation.tsx b/apps/web/src/components/navigation.tsx
index 358270f4..bb1b56d0 100644
--- a/apps/web/src/components/navigation.tsx
+++ b/apps/web/src/components/navigation.tsx
@@ -136,9 +136,12 @@ function ThemeButton() {
);
}
+const BLACKLISTED_LOGIN_REDIRECT_PATHS = ['/', '/login'];
+
function LoginButton() {
const { data: session, status } = useSession();
const router = useRouter();
+ const pathname = usePathname();
const isAdminOrMod = isAdminOrModerator(session);
@@ -147,6 +150,10 @@ function LoginButton() {
router.refresh();
};
+ const isBlacklistedPath = BLACKLISTED_LOGIN_REDIRECT_PATHS.some((blacklistedPath) => {
+ return blacklistedPath === pathname;
+ });
+
return session ? (
<DropdownMenu>
<DropdownMenuTrigger asChild>
@@ -202,7 +209,12 @@ function LoginButton() {
) : (
<Link
className="focus:bg-accent w-20 rounded-lg bg-transparent p-2 text-black duration-300 hover:bg-gray-200 focus:outline-none dark:text-white hover:dark:bg-gray-800"
- href="/login"
+ href={{
+ pathname: '/login',
+ query: {
+ ...(!isBlacklistedPath && { redirectTo: pathname }),
+ },
+ }}
>
{status === 'loading' ? (
<Loader2 className="h-5 w-5 animate-spin" />