Skip to content

Commit b9bb9fb

Browse files
Merge pull request #31 from metakgp/copilot/fix-2672d425-9df3-425c-a2b1-2d82bbcd69a4
Fix redirections to preserve user's intended destination after authentication
2 parents 0db0bcd + f96119f commit b9bb9fb

File tree

10 files changed

+50
-23
lines changed

10 files changed

+50
-23
lines changed

app/authenticate/AuthForm.jsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
import { useState, useEffect } from "react";
33
import { useRouter } from "next/navigation";
44

5-
export default function AuthForm({ institutes }) {
5+
export default function AuthForm({ institutes, redirectUrl }) {
66
const router = useRouter();
77

88
const check = () => {
99
if (localStorage.getItem("travelbuddy")) {
10-
router.push("/");
10+
// Redirect to original destination or homepage if no redirect URL
11+
router.push(redirectUrl ? decodeURIComponent(redirectUrl) : "/");
1112
return;
1213
}
1314

1415
if (institutes && institutes.length === 1) {
15-
router.push("/register?instituteCode=" + institutes[0].code);
16+
const redirectParam = redirectUrl ? `&redirect_url=${encodeURIComponent(redirectUrl)}` : '';
17+
router.push("/register?instituteCode=" + institutes[0].code + redirectParam);
1618
return;
1719
}
1820
};
@@ -33,7 +35,8 @@ export default function AuthForm({ institutes }) {
3335
alert("Please select an institute");
3436
return;
3537
}
36-
router.push("/register?instituteCode=" + institute);
38+
const redirectParam = redirectUrl ? `&redirect_url=${encodeURIComponent(redirectUrl)}` : '';
39+
router.push("/register?instituteCode=" + institute + redirectParam);
3740
return;
3841
};
3942

app/authenticate/page.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ export const metadata = {
88
title: "Select Institute",
99
};
1010

11-
const Page = async () => {
11+
const Page = async ({ searchParams }) => {
1212
await connectToDatabase();
1313

1414
// Return only names of Institues
1515
const institutes = await Institute.find({}, { _id: 0, name: 1, code: 1 });
1616

17-
return <AuthForm institutes={JSON.parse(JSON.stringify(institutes))} />;
17+
return <AuthForm institutes={JSON.parse(JSON.stringify(institutes))} redirectUrl={searchParams.redirect_url} />;
1818
};
1919

2020
export default Page;

app/register/RegForm.jsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Loading from "@/app/utils/Loading";
55
import { useRouter } from "next/navigation";
66
import { checkUser } from "@/app/utils/auth";
77

8-
export default function RegForm({ email, instituteCode }) {
8+
export default function RegForm({ email, instituteCode, redirectUrl }) {
99
const router = useRouter();
1010
const [loading, setLoading] = useState(true);
1111
const [formData, setFormData] = useState({
@@ -17,14 +17,16 @@ export default function RegForm({ email, instituteCode }) {
1717

1818
const check = async () => {
1919
if (localStorage.getItem("travelbuddy")) {
20-
router.push("/");
20+
// Redirect to original destination or homepage if no redirect URL
21+
router.push(redirectUrl ? decodeURIComponent(redirectUrl) : "/");
2122
return;
2223
}
2324

2425
const user = await checkUser({ email });
2526
if (user) {
2627
localStorage.setItem("travelbuddy", user);
27-
router.push("/");
28+
// Redirect to original destination or homepage if no redirect URL
29+
router.push(redirectUrl ? decodeURIComponent(redirectUrl) : "/");
2830
return;
2931
}
3032

@@ -76,7 +78,8 @@ export default function RegForm({ email, instituteCode }) {
7678
const json = await res.json();
7779
alert(json.message);
7880
localStorage.setItem("travelbuddy", json.user);
79-
router.push("/");
81+
// Redirect to original destination or homepage if no redirect URL
82+
router.push(redirectUrl ? decodeURIComponent(redirectUrl) : "/");
8083
} else {
8184
const json = await res.json();
8285
alert(json.message);

app/register/page.jsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const metadata = {
88
};
99

1010
const Page = async ({ searchParams }) => {
11-
const { instituteCode } = searchParams;
11+
const { instituteCode, redirect_url } = searchParams;
1212

1313
const institute = await instituteDetails({ instituteCode });
1414

@@ -17,22 +17,27 @@ const Page = async ({ searchParams }) => {
1717
const cookieStore = cookies();
1818
const cookie = cookieStore.get(authCookie);
1919

20+
// Build the redirect URL for Heimdall with proper fallback
21+
const finalRedirectUrl = redirect_url ?
22+
`https://travel.metakgp.org/register?instituteCode=${instituteCode}&redirect_url=${encodeURIComponent(redirect_url)}` :
23+
`https://travel.metakgp.org/register?instituteCode=${instituteCode}`;
24+
2025
if (!cookie) {
21-
redirect(authLink + "?redirect_url=https://travel.metakgp.org/");
26+
redirect(authLink + "?redirect_url=" + encodeURIComponent(finalRedirectUrl));
2227
}
2328

2429
const token = cookie.value;
2530
if (!token) {
26-
redirect(authLink + "?redirect_url=https://travel.metakgp.org/");
31+
redirect(authLink + "?redirect_url=" + encodeURIComponent(finalRedirectUrl));
2732
}
2833

2934
const email = JSON.parse(atob(token.split(".")[1])).email; // get the user email from jwt
3035

3136
if (!email) {
32-
redirect(authLink + "?redirect_url=https://travel.metakgp.org/");
37+
redirect(authLink + "?redirect_url=" + encodeURIComponent(finalRedirectUrl));
3338
}
3439

35-
return <RegForm email={email} instituteCode={instituteCode} />;
40+
return <RegForm email={email} instituteCode={instituteCode} redirectUrl={redirect_url} />;
3641
};
3742

3843
export default Page;

app/trains/create/TrainForm.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ const TrainForm = () => {
1515

1616
const check = async () => {
1717
if (!localStorage.getItem("travelbuddy")) {
18-
router.push("/authenticate");
18+
// Preserve current URL for redirect after authentication
19+
const currentUrl = encodeURIComponent(window.location.pathname + window.location.search);
20+
router.push(`/authenticate?redirect_url=${currentUrl}`);
1921
return;
2022
}
2123
const token = localStorage.getItem("travelbuddy");
2224
const email = await verifyUser({ token });
2325
if (!email) {
2426
localStorage.removeItem("travelbuddy");
25-
router.push("/authenticate");
27+
// Preserve current URL for redirect after authentication
28+
const currentUrl = encodeURIComponent(window.location.pathname + window.location.search);
29+
router.push(`/authenticate?redirect_url=${currentUrl}`);
2630
return;
2731
}
2832
setEmail(email);

app/trains/my-trains/MyTrains.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const MyTrains = () => {
1212

1313
const getDetails = async () => {
1414
if (!localStorage.getItem("travelbuddy")) {
15-
router.push("/authenticate");
15+
// Preserve current URL for redirect after authentication
16+
const currentUrl = encodeURIComponent(window.location.pathname + window.location.search);
17+
router.push(`/authenticate?redirect_url=${currentUrl}`);
1618
return;
1719
}
1820
const res = await fetch("/api/trains/find", {

app/trains/train/[trainID]/TrainDetails.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const TrainDetails = ({ trainID }) => {
1212

1313
const getDetails = async () => {
1414
if (!localStorage.getItem("travelbuddy")) {
15-
router.push("/authenticate");
15+
// Preserve current URL for redirect after authentication
16+
const currentUrl = encodeURIComponent(window.location.pathname + window.location.search);
17+
router.push(`/authenticate?redirect_url=${currentUrl}`);
1618
return;
1719
}
1820
const res = await fetch("/api/trains/find", {

app/trips/create/TripForm.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ const TripForm = () => {
1616

1717
const check = async () => {
1818
if (!localStorage.getItem("travelbuddy")) {
19-
router.push("/authenticate");
19+
// Preserve current URL for redirect after authentication
20+
const currentUrl = encodeURIComponent(window.location.pathname + window.location.search);
21+
router.push(`/authenticate?redirect_url=${currentUrl}`);
2022
return;
2123
}
2224
const token = localStorage.getItem("travelbuddy");
2325
const email = await verifyUser({ token });
2426
if (!email) {
2527
localStorage.removeItem("travelbuddy");
26-
router.push("/authenticate");
28+
// Preserve current URL for redirect after authentication
29+
const currentUrl = encodeURIComponent(window.location.pathname + window.location.search);
30+
router.push(`/authenticate?redirect_url=${currentUrl}`);
2731
return;
2832
}
2933
setEmail(email);

app/trips/my-trips/MyTrips.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ const MyTrips = () => {
1313

1414
const getDetails = async () => {
1515
if (!localStorage.getItem("travelbuddy")) {
16-
router.push("/authenticate");
16+
// Preserve current URL for redirect after authentication
17+
const currentUrl = encodeURIComponent(window.location.pathname + window.location.search);
18+
router.push(`/authenticate?redirect_url=${currentUrl}`);
1719
return;
1820
}
1921
const res = await fetch("/api/trips/find", {

app/trips/trip/[tripID]/TripDetails.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const TripDetails = ({ tripID }) => {
1212

1313
const getDetails = async () => {
1414
if (!localStorage.getItem("travelbuddy")) {
15-
router.push("/authenticate");
15+
// Preserve current URL for redirect after authentication
16+
const currentUrl = encodeURIComponent(window.location.pathname + window.location.search);
17+
router.push(`/authenticate?redirect_url=${currentUrl}`);
1618
return;
1719
}
1820
const res = await fetch("/api/trips/find", {

0 commit comments

Comments
 (0)