Skip to content

Commit

Permalink
change cookie parsing in api routes example
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Dec 20, 2023
1 parent 1c2d9d7 commit 0fee972
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 41 deletions.
41 changes: 10 additions & 31 deletions docs/routes/core-concepts/api-routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,46 +82,24 @@ export async function GET({ params }: APIEvent) {

As HTTP is a stateless protocol, for awesome dynamic experiences, you want to know the state of the session on the client. For example, you want to know who the user is. The secure way of doing this is to use HTTP-only cookies.

You can store session data in them and they are persisted by the browser that your user is using. We expose the `Request` object which represents the user's request. The cookies can be accessed by parsing the `Cookie` header.
You can store session data in them and they are persisted by the browser that your user is using. We expose the `Request` object which represents the user's request. The cookies can be accessed by parsing the `Cookie` header. We re-export H3's helpers from `@solidjs/start/server` to make that a bit easier.

Let's look at an example of how to use the cookie to identify the user:

```tsx twoslash filename="routes/api/[house]/admin.ts"
// @filename: hogwarts.ts
export default {
getStudents(house: string, year: string) {
return [
{ name: "Harry Potter", house, year },
{ name: "Hermione Granger", house, year },
{ name: "Ron Weasley", house, year },
];
},
getHouseMaster(house: string) {
return {
name: "Severus Snape",
house,
id: "5"
};
},
};

// @filename: index.ts
const parseCookie = (t: string) => ({} as Record<string, any>)
// ---cut---
import { type APIEvent } from "@solidjs/start/server";
```tsx filename="routes/api/[house]/admin.ts"
import { getCookie, type APIEvent } from "@solidjs/start/server";
import hogwarts from "./hogwarts";

export async function GET({ request, params }: APIEvent) {
const cookie = parseCookie(request.headers.get("Cookie") ?? "");
const userId = cookie['userId'];
export async function GET(event: APIEvent) {
const userId = getCookie(event, "userId");
if (!userId) {
return new Response("Not logged in", { status: 401 });
}
const houseMaster = await hogwarts.getHouseMaster(params.house);
const houseMaster = await hogwarts.getHouseMaster(event.params.house);
if (houseMaster.id !== userId) {
return new Response("Not authorized", { status: 403 });
}
return await hogwarts.getStudents(params.house, params.year)
return await hogwarts.getStudents(event.params.house, event.params.year)
}
```

Expand Down Expand Up @@ -188,12 +166,13 @@ Let's see how to expose a [tRPC][trpc] server route. First you write your router

```tsx filename="lib/router.ts"
import { initTRPC } from '@trpc/server';
import { z } from 'zod';
import { wrap } from "@decs/typeschema";
import { string } from "valibot";

const t = initTRPC.create();

export const appRouter = t.router({
hello: t.procedure.input(z.string().nullish()).query(({ input }) => {
hello: t.procedure.input(wrap(string())).query(({ input }) => {
return `hello ${input ?? 'world'}`;
}),
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"bump": "node scripts/bump.js"
},
"devDependencies": {
"@decs/typeschema": "^0.12.1",
"@cloudflare/kv-asset-handler": "^0.2.0",
"@rollup/plugin-commonjs": "^24.1.0",
"@rollup/plugin-json": "^6.0.0",
Expand Down Expand Up @@ -53,7 +54,7 @@
"typescript": "4.7.4",
"vinxi": "0.0.52",
"vite": "^4.4.6",
"zod": "^3.21.4"
"valibot": "0.24.1"
},
"dependencies": {
"cross-env": "^7.0.3"
Expand Down
78 changes: 69 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0fee972

Please sign in to comment.