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
for time being my code looks something like this
I think you can store current url in a variable using router from next navigation then pass that as the callbackUrl
@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 ?
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
router.pathname does not exist in nextjs 13 app dir
use window.location.href
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
const router = useRouter();
const { previousUrl } = router.query
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
now it works
thanks @peterkyle01
then
callbackUrl:
${previousUrl || "/"}
,
that will use previous url if available or otherwise fallback to root parhrouter.query is no more in nextjs 13 app router
Owkay , glad to see it work 👌, thanks for the info ,i didnt notice it was removed actually
thanks @peterkyle01