Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for throwing redirect in handle function #7609

Closed
ivanhofer opened this issue Nov 11, 2022 · 1 comment · Fixed by #7612
Closed

support for throwing redirect in handle function #7609

ivanhofer opened this issue Nov 11, 2022 · 1 comment · Fixed by #7612

Comments

@ivanhofer
Copy link
Contributor

ivanhofer commented Nov 11, 2022

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 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.

if (!authorized) {
	return new Response(null, { status: 302, headers: { Location: '/login' } })
}

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:

export const handle: Handle = async (input) => handleRequest(input)
	.catch(error => {
		// can't use instanceof `Redirect` because class get's not exported
		if (error.status && error.location) {
			return new Response(null, {
				status: error.status,
				headers: { location: error.location }
			})
		}

		// can't use instanceof `HttpError` because class get's not exported
		if (error.status && error.body) {
			return new Response(JSON.stringify(error.body), { status: error.status })
		}

		return new Response(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)
  • does not handle setting cookies (related to cookies are not being set when returning custom Response objects in handle #7611)

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

  1. discover that this functionality it is not supported
  2. figure out a way to make redirects happen

Importance

would make my life easier

Additional Information

No response

@ivanhofer
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant