Server side redirect
Unanswered
Florida White posted this in #help-forum
Florida WhiteOP
Hey guys. I have small application working as sub-app. Let's say that main url is https://my-website.com (which is .NET app with whole authorization etc, main point of bigger system). My app is a module of this system and it works under https://my-website.com/next. In my application I need to be sure that user is logged in parent app. I have a cookie which is fine and I'm using it to get some data from parent app - everything fine. Now I need to secure it that after some time when cookie expiry I will be redirected to parent app (to log-in again). Using middleware I'm able to do it on entering the views - seems to be working fine. But what about server actions and fetches on client? For server actions I was trying to create it with next-safe-action and own middleware with small request to parent app to re-validate the cookie, if it fails then redirect/permamentRedirect to https://my-website.com. Problem is that page remains the same, I see request with redirect to https://my-website.com but it's still displaying nextjs application. Can you help me with that case?
// edit
For route handlers and fetching(I'm using it to have call to parent app and it's used with SWR axios) I wanted to do the same or something similar so please give me some tip for it also!
// edit
For route handlers and fetching(I'm using it to have call to parent app and it's used with SWR axios) I wanted to do the same or something similar so please give me some tip for it also!
16 Replies
Florida WhiteOP
bump
Florida WhiteOP
bump
Florida WhiteOP
bump
Trigg Hound
Are you using the
redirect(full url)
function to redirect for your server actions and on client router.push(full url) from next/navigation
Florida WhiteOP
on client it's pretty simple and yes - router.push. On server I'm using permamentRedirect or redirect - both not working. Thanks for answer!
Trigg Hound
it would be helpful if you can provide the video or reproduction of the issues or at least some code to work with
I can only help with so much with so little information
Florida WhiteOP
I don't have currently access to my code but I'm trying to use https://nextjs.org/docs/app/building-your-application/routing/redirecting#redirect-function
and I'm doing something like fetch to external api (parent app like I described) and for example my cookie is expired, so it should return 401
and then I would like to redirect
talkin about server action or route handler of course
for route handler (mostly used to have SWR hook with axios) to get data
and seems to working when I return 401 and in axios interceptor I have config that if I receive 401 then window.location.replace
Or maybe you can propose some other way of doing it?
Trigg Hound
so you mentioned that this is kinda like a micro front-end for a bigger auth solution. Are you using the
redirect()
or permanentRedirect()
to redirect to the auth pageFlorida WhiteOP
tried both