{
  "id": "6b7d98ddbd3dcfe6040e6bae",
  "source": "solid:signals",
  "type": "github-document",
  "title": "auth",
  "content": "---\ntitle: Auth\nuse_cases: \u003e-\n  user authentication, protected routes, authorization checks, secure data\n  access, login systems\ntags:\n  - authentication\n  - authorization\n  - security\n  - protected\n  - login\n  - users\nversion: '1.0'\ndescription: \u003e-\n  Implement authentication and protected routes in SolidStart. Secure sensitive\n  resources and handle user authorization properly.\n---\n\nServer functions can be used to protect sensitive resources like user data.\n\n```tsx\n\"use server\"\n\nasync function getPrivatePosts() {\n\tconst user = await getUser()\n\tif(!user) {\n\t\treturn null  // or throw an error\n\t}\n\n\treturn db.getPosts({ userId: user.id, private: true })\n}\n```\n\nThe `getUser` function can be [implemented using sessions](/solid-start/advanced/session).\n\n## Protected Routes\n\nRoutes can be protected by checking the user or session object during data fetching.\nThis example uses [Solid Router](/solid-router).\n\n```tsx\nconst getPrivatePosts = query(async function() {\n\t\"use server\"\n\tconst user = await getUser()\n\tif(!user) {\n\t\tthrow redirect(\"/login\");\n\t}\n\n\treturn db.getPosts({ userId: user.id, private: true })\n})\n\nexport default function Page() {\n\tconst posts = createAsync(() =\u003e getPrivatePosts(), { deferStream: true });\n}\n```\n\nOnce the user hits this route, the router will attempt to fetch `getPrivatePosts` data.\nIf the user is not signed in, `getPrivatePosts` will throw and the router will redirect to the login page.\n\nTo prevent errors when opening the page directly, set `deferStream: true`.\nThis would ensure `getPrivatePosts` resolves before the page loads since server-side redirects cannot occur after streaming has started.",
  "url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/solid-start/advanced/auth.mdx",
  "metadata": {
    "path": "src/routes/solid-start/advanced/auth.mdx",
    "repo": "solidjs/solid-docs",
    "repo_url": "https://github.com/solidjs/solid-docs.git",
    "size": 1644,
    "source_type": "github"
  },
  "hash": "8fc49a96e28031bf13f061a3145e294b2181b7ee2a4237530cffe139af35a4cc",
  "timestamp": "2026-02-23T11:43:00.193534235+01:00"
}