You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The convenient function redirect is really useful to redirect users to another page. It can be used in a lot of different files files, except for the hooks file.
It is not that unlikely to have a logic inside the handle function that checks if the user is authorized and then redirect the user to the login page.
In order to do so someone can't throw a redirect as everything thrown inside handle will result in an 500 error. In order to redirect a proper Response object needs to be returned.
Writing it like this involves writing a bit of an overhead, but I would really love to use the redirect helper function like I do in all other code parts.
I'm currently using a wrapper to handle this use case where handleRequest is the actual implementation of my handle functionality:
exportconsthandle: Handle=async(input)=>handleRequest(input).catch(error=>{// can't use instanceof `Redirect` because class get's not exportedif(error.status&&error.location){returnnewResponse(null,{status: error.status,headers: {location: error.location}})}// can't use instanceof `HttpError` because class get's not exportedif(error.status&&error.body){returnnewResponse(JSON.stringify(error.body),{status: error.status})}returnnewResponse(null,{status: 500})})
Here I'm catching Redirects and HttpErrors and implementing and returning custom Responses. But this has a few weak points:
user code does not have access to Redirect, so I have to check it in another way
when SvelteKit changes the implementation of the Redirect class, my code will no longer work
possible inconsistencies with framework code (I did not take a look what exactly happens when SvelteKit handles `Redirect objects)
It would be great to have an official solution to this problem.
Describe the proposed solution
SvelteKit should catch errors from the handle function and redirect the user if a Redirect class gets thrown.
A similar thing could be done for the HttpError class thrown by the error() helper function. I guess that is related to this issue: #7272.
At least SvelteKit could set the correct status code when someone uses the error convenient function to throw inside handle.
Alternatives considered
Leave it to users to
discover that this functionality it is not supported
figure out a way to make redirects happen
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered:
Describe the problem
The convenient function
redirect
is really useful to redirect users to another page. It can be used in a lot of different files files, except for thehooks
file.It is not that unlikely to have a logic inside the
handle
function that checks if the user is authorized and then redirect the user to the login page.In order to do so someone can't throw a
redirect
as everything thrown insidehandle
will result in an 500 error. In order to redirect a properResponse
object needs to be returned.Writing it like this involves writing a bit of an overhead, but I would really love to use the
redirect
helper function like I do in all other code parts.I'm currently using a wrapper to handle this use case where
handleRequest
is the actual implementation of myhandle
functionality:Here I'm catching
Redirect
s andHttpError
s and implementing and returning customResponse
s. But this has a few weak points:Redirect
, so I have to check it in another wayRedirect
class, my code will no longer workResponse
objects inhandle
#7611)It would be great to have an official solution to this problem.
Describe the proposed solution
SvelteKit
should catch errors from thehandle
function and redirect the user if aRedirect
class gets thrown.A similar thing could be done for the
HttpError
class thrown by theerror()
helper function. I guess that is related to this issue: #7272.At least SvelteKit could set the correct status code when someone uses the
error
convenient function to throw insidehandle
.Alternatives considered
Leave it to users to
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: