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_uidev•12mo 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•12mo 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_uidev•12mo 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•12mo 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_uidev•12mo ago
router.pathname does not exist in nextjs 13 app dir
peterkyle01
peterkyle01•12mo ago
use window.location.href
ayush_uidev
ayush_uidev•12mo 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•12mo ago
const router = useRouter(); const { previousUrl } = router.query
ayush_uidev
ayush_uidev•12mo 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•12mo ago
then callbackUrl: ${previousUrl || "/"}, that will use previous url if available or otherwise fallback to root parh
ayush_uidev
ayush_uidev•12mo ago
router.query is no more in nextjs 13 app router
peterkyle01
peterkyle01•12mo ago
Owkay , glad to see it work 👌, thanks for the info ,i didnt notice it was removed actually
ayush_uidev
ayush_uidev•12mo ago
thanks @peterkyle01