Redirect next-auth signIn to history page

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

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Trying to start dev of julius acme-corp repoI added env vars, ran pnpm install, ran npm i in every package and I still get this error: How do I Does anyone know of a performant slider/carousel library for react?We are currently using Keen Slider but it does not perform well. I'm hoping to find a performant sliHow to implement redirect based on user roleHi, I would love to implement redirections based on the user role. I want to implement it so that oRegex cutting off anything after space in the urlHeres the current regex : const regex = /(https?:\/\/\S+)\s*\n*\s*(Canonical|\d*)/g; it gives backdynamic routes in middleware with next-authso i want to do dynamic routes using next-auth with the config varibe that middleware offers This Do we need a fetcher like SWR with the t3 stackIt isn't mention in the documentation so I was wondering, do we need a fetcher with tRPC ? It's my UploadThing with PythonI am using UploadThing on my main app where a user uploads a word document. I save it's UT URL in myDeployment error with working environment variableI'm trying to deploy on vercel and i got this error below. I can call the OpenAI API (via server siBoth my dev environment and my deployed code are the exact same, but my text is aligned differentlyAnyone know why this is the case?Prisma not connecting to DB in amplifyHey guys so i got the following schema ```ts model Images { id Int @id @default(autoincrExplicitly specifying an object in Prisma with its foreign relationshipsI've set up my schema as an approximation of a directory structure: directories (`ADir`s) and documeError when running `drizzle-kit push:mysql` to a Planetscale databaseso after reading through the Doc's it looks like i need an SSL cert to run the push however i'm not Headless UI TransitionsHeya, I'm trying to create a simple little animation where a dropdown menu slides down when revealedhow can I invoke a trpc call inside a nested function?I know it isn't directly possible. But is there a workaround?Counting rows in Drizzle ORMHi, I was wondering what the best way to count rows in drizzle is. Thanks for the help!Does App Router have getStaticPaths? Does it even need that?Hello everybody! 👋 I hope you are well! So I'm looking into App Router. I just recently followed Pull template repositoryI would like to know if it is possible to use a template repository and after the template repo receTypeError: client[procedureType] is not a function weirdnessI'm trying to make an adapter for auth.js and want to call my backend via the adapter with tRPC proxInvalid env variables NextjsHi guys ! It's certainly very dumb but I don't figure it out. I have set everything correctly from wWeird type errors with next.js and drizzle```ts //when I use export const dynamicParams = false; //or export const dynamicParams = true; ``` N