Redirect next-auth signIn to history page

Hi guys i am facing a issue i have create a custom signin page and i am using credientials provider how to achive the redirect after signIn to the page it was opened before log in. like if i dont have a sigin page the default signpage of next-auth redirect to the perticulate page i have been redirected to the signin page how can i achive auth redirect after for more context i am using nextjs app dir
13 Replies
ayush_uidev
ayush_uidevOP•2y ago
for time being my code looks something like this
function onSubmit(data: Inputs) {
startTransition(async () => {
try {
await signIn("credentials", {
redirect: true,
email: data.email,
password: data.password,
callbackUrl: "/",
});
} catch (error) {
console.log(error);
toast.error("Something Went Wrong");
}
});
}
function onSubmit(data: Inputs) {
startTransition(async () => {
try {
await signIn("credentials", {
redirect: true,
email: data.email,
password: data.password,
callbackUrl: "/",
});
} catch (error) {
console.log(error);
toast.error("Something Went Wrong");
}
});
}
peterkyle01
peterkyle01•2y ago
I think you can store current url in a variable using router from next navigation then pass that as the callbackUrl
ayush_uidev
ayush_uidevOP•2y ago
@peterkyle01 how is that possible ? you got any example or can you tell me where to put it I want the behaviour like the next-auth custom signIn pages have callback searchparams which redirects to the page which was open before the redirect to signIn page ?
peterkyle01
peterkyle01•2y ago
before startTransition const router = useRouter() const currentURL = router. pathname then on the callbackUrl: callbackUrl:{currentUrl} if that doesnt work you can instead of using a callback url just do a : router.push(currentUrl) after the await signin method which will auto navigate to previous url
ayush_uidev
ayush_uidevOP•2y ago
router.pathname does not exist in nextjs 13 app dir
peterkyle01
peterkyle01•2y ago
use window.location.href
ayush_uidev
ayush_uidevOP•2y ago
but this is giving me current url i want the url before i was redirected to signIn page as i was unAuthenticated actually no worries i have figured it out thanks for help
peterkyle01
peterkyle01•2y ago
const router = useRouter(); const { previousUrl } = router.query
ayush_uidev
ayush_uidevOP•2y ago
this was my mistake next-auth does not add callback searchParams when it is redirected from the "/" url it only add searchParams when it is redirected from the different page for example "/about" then i updated the code something like this
const searchParams = useSearchParams();
const callbackUrl = searchParams.get("callbackUrl");

function onSubmit(data: Inputs) {
startTransition(async () => {
try {
await signIn("credentials", {
redirect: true,
email: data.email,
password: data.password,
callbackUrl: callbackUrl || "/",
});
} catch (error) {
console.log(error);
toast.error("Something Went Wrong");
}
});
}
const searchParams = useSearchParams();
const callbackUrl = searchParams.get("callbackUrl");

function onSubmit(data: Inputs) {
startTransition(async () => {
try {
await signIn("credentials", {
redirect: true,
email: data.email,
password: data.password,
callbackUrl: callbackUrl || "/",
});
} catch (error) {
console.log(error);
toast.error("Something Went Wrong");
}
});
}
now it works thanks @peterkyle01
peterkyle01
peterkyle01•2y ago
then callbackUrl: ${previousUrl || "/"}, that will use previous url if available or otherwise fallback to root parh
ayush_uidev
ayush_uidevOP•2y ago
router.query is no more in nextjs 13 app router
peterkyle01
peterkyle01•2y ago
Owkay , glad to see it work 👌, thanks for the info ,i didnt notice it was removed actually
ayush_uidev
ayush_uidevOP•2y ago
thanks @peterkyle01
Want results from more Discord servers?
Add your server