sveltejs / sveltejs/kit

docs: Protected Routes: Best Practice?

Open
#7,267 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

It's a common scenario to have protected routes. It would be great if the docs would outline the best practice for doing this in sveltekit.

Describe the proposed solution

I would think that the hooks.server.js is the best place to check the current path, and act if a user is on a route and (for example) not authenticated.

Alternatives considered

Adding a server.js file per route, but this won't handle any subroutes.
Ex:
/api/server.js <- redirect request
/api/secrets <- still accessible

Importance

would make my life easier

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the protected-route guidance around hooks.server.js and the alternative per-route server.js approach described in the issue. Document the recommended SvelteKit pattern for checking authentication and covering subroutes, with an example that makes the expected redirect behavior clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
authentication, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.