How to get `post_login_redirect_url` to work
I’d like to redirect my user back where they were after they log in. For that i found the (undocumented) property
post_login_redirect_url
which is only mentioned in the Nextjs SDK docs.
Unfortunately it did not add the search param to the kinde login URL 😦
Then we’ve got the other thing: how to get that URL back in my /callback
endpoint?
I call this await kindeClient.handleRedirectToApp(manager, url)
but what about then?
9 Replies
I ended up making a work around storing it in cookies, but wonder if i could make use of this after all?
Hey, i'm curious which SDK are you using please ? Please add its version too
"@kinde-oss/kinde-typescript-sdk": "^2.9.1",
Excuse me. So if i understand your issue correctly it's when passing a value to post_login_redirect_url the params aren't added ? Like if you wanted
post_login_redirect_url: domain.com?redirect=shop
The part ?redirect=shop isn't added ?
I am not familiar with the typescript sdk but i've had this use case and what i've done was to store the page the user was on before signing in (I'm using custom auth with a modal)
Then after the login all users are redirected to /callback where I check for the cookie value to redirect him
i did the same thing, but I was wondering two things:
1. if that parameter doesn’t work why does it exist — it does not add to the URL search params
2. how do I get that redirect URL back inside my callback
I’d rather use the API itself than my own solution, but for now im doing exactly that; storing the URL in a cookie and then restoring it
If the param isn’t working then it might be a bug
It’s mostly a bug yeah. You should open an issue on the GitHub repo. Ive passed it to the team but the issue would be even better
Hi @freddie ,
Thank you for the details. I will check on the issue with the post_login_redirect_url parameter and get back to you shortly.
Hi @freddie you can implement a custom solution by storing the intended URL before authentication and retrieving it after the user logs in. Here's how you can achieve this:
Before initiating the login process, capture the URL the user intends to visit and store it. For client-side applications, you can use localStorage, and for server-side applications, cookies are appropriate.
Client-Side Example:
// Store the intended URL in localStorage
localStorage.setItem('postLoginRedirect', window.location.href);
Server-Side Example (using cookies):
// Set a cookie with the intended URL
res.cookie('postLoginRedirect', req.originalUrl, { httpOnly: true, secure: true });
Initiate the Login Process:
Proceed with the login process using the Kinde SDK.
// Redirect the user to the Kinde login URL
const loginURL = await client.login(locals.sessionManager);
return redirect(loginURL.toString());
Handle the Callback After Login:
After the user authenticates, they will be redirected to your application's callback URL. In this handler, retrieve the stored URL and redirect the user accordingly.
Client-Side Example:
// Retrieve the intended URL from localStorage
const postLoginRedirect = localStorage.getItem('postLoginRedirect') || '/';
// Redirect the user
window.location.replace(postLoginRedirect);
Server-Side Example (using cookies):
// Retrieve the intended URL from the cookie
const postLoginRedirect = req.cookies.postLoginRedirect || '/';
// Clear the cookie
res.clearCookie('postLoginRedirect');
// Redirect the user
res.redirect(postLoginRedirect);
For more detailed information on handling redirects after authentication, refer to Kinde's documentation on Redirecting Users https://docs.kinde.com/authenticate/custom-configurations/redirect-users/
Please try implementing this and let me know how it goes? If you encounter any issues or have further questions, feel free to ask—I'm here to helpHi @freddie ,
I just wanted to check in and see if you've had a chance to try the solution I shared regarding handling redirects after login. Have you encountered any issues or do you have any questions? Otherwise we'll go ahead and close this ticket.
Feel free to reach out if you need further assistance.