1

I'm trying to protect routes, or redirect to the login when auth_token isn't in local storage.

In my src/routes/+layout.server.ts I have:

import { redirect } from "@sveltejs/kit"
import type { LayoutServerLoad } from "./$types"

export const load: LayoutServerLoad = async ({ locals }) => {
    if (localStorage.getItem('auth_token') === null) {
        throw redirect(303, '/')
    }
}

Although I'm getting:

ReferenceError: localStorage is not defined?

Paolo
  • 20,112
  • 21
  • 72
  • 113
  • Well, `localStorage` is a browser API and your code is covered in the word **server** so ‍♂️ – Quentin Mar 23 '23 at 10:15
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Quentin Mar 23 '23 at 10:15

0 Answers0