Skip to content

Commit

Permalink
Add a live demo (#19)
Browse files Browse the repository at this point in the history
* fix typo

* add a live demo

* add change set
  • Loading branch information
toyamarinyon authored Jun 30, 2022
1 parent a79308e commit d788375
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/late-mangos-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"webcrypt-session-example": patch
"webcrypt-session": patch
---

Add a live demo
78 changes: 66 additions & 12 deletions examples/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ const sessionScheme = z.object({
const signInParamScheme = z.object({
username: z.string(),
});

const welComePage = `<!DOCTYPE html>
<body>
<h1>Hello WebCrypt-Session</h1>
<p>Please sign in first with <a href="/signIn">sign-in</a>.</p>
</body>
</html>`;

const welComeUserPage = `<!DOCTYPE html>
<body>
<h1>Hello WebCrypt-Session</h1>
<p>Welcome, <%= username %>!</p>
<p>You can sign out with clicking following button.</p>
<form action="/signOut" method="POST">
<button type="submit">Sign Out</button>
</form>
</body>
</html>`;

const signInPage = `<!DOCTYPE html>
<body>
<h1>Sign-In to WebCrypt-Session</h1>
<form action="signIn" method="POST">
<label>Username: <input type="text" name="username" required /></label>
<button type="submit">Sign in</button>
</form>
</body>
</html>`;

export default {
async fetch(
request: Request,
Expand All @@ -40,40 +69,65 @@ export default {
}
);
const url = new URL(request.url);
const baseUrl = `${url.protocol}//${url.host}`;
if (url.pathname === "/signIn") {
if (request.method !== "POST") {
if (request.method === "GET") {
return new Response(signInPage, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
});
} else if (request.method !== "POST") {
return new Response(null, { status: 405 });
}
try {
const signInParam = signInParamScheme.parse(await request.json());
const formData = await request.formData();
const formObject = Object.fromEntries(formData.entries());
const signInParam = signInParamScheme.parse(formObject);
await webCryptSession.save({
username: signInParam.username,
});
const session = webCryptSession.toHeaderValue();
if (session == null) {
throw new Error();
}
return new Response(`Hello ${signInParam.username}`, {
return new Response(null, {
status: 303,
headers: {
location: baseUrl,
"Set-Cookie": session,
},
});
} catch {
} catch (_) {
return new Response(null, {
status: 400,
});
}
} else if (url.pathname === "/signOut") {
return new Response(null, {
status: 303,
headers: {
location: baseUrl,
"Set-Cookie": "session=delete; expires=Thu, 01 Jan 1970 00:00:00 GMT",
},
});
}
const session = webCryptSession.username;
if (session == null) {
return new Response(
"Please sign in first with http://localhost:8787/signIn"
);
return new Response(welComePage, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
});
}
return new Response(`Hello ${webCryptSession.username}`, {
headers: {
"Set-Cookie": session,
},
});
return new Response(
welComeUserPage.replace("<%= username %>", webCryptSession.username),
{
headers: {
"Set-Cookie": webCryptSession.toHeaderValue() ?? "",
"content-type": "text/html;charset=UTF-8",
},
}
);
},
};
2 changes: 1 addition & 1 deletion examples/cloudflare/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "cloudflare"
name = "webcrypt-session"
main = "src/index.ts"
compatibility_date = "2022-06-14"
tsconfig = "./tsconfig.json"
9 changes: 7 additions & 2 deletions packages/webcrypt-session/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# WebCrypt Session

demo: https://webcrypt-session.sat0shi.workers.dev/

_🛠 Stateless session utility using signed and encrypted cookies to store data. Works with WebCrypt API on Cloudflare Workers._

```ts
Expand All @@ -16,10 +18,13 @@ export default {
request,
{
password: "IF4B#t69!WlX$uS22blaxDvzJJ%$vEh%",
cookie: "session",
}
);
return webCryptSession.response(`Hello ${webCryptSession.session.username}`)
return new Response(`Hello ${webCryptSession.username}`, {
headers: {
"Set-Cookie": webCryptSession.toHeaderValue(),
},
});
},
};
```
Expand Down
2 changes: 1 addition & 1 deletion packages/webcrypt-session/src/adapters/trpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function getWebCryptSession<T extends z.AnyZodObject>(

// Block calls to WebCryptSession's internal functions
// allowing access only to the scheme.
return (webCryptSession as unknown) as Exclude<
return (webCryptSession as unknown) as Omit<
WebCryptSession<T>,
"toHeaderValue"
>;
Expand Down

0 comments on commit d788375

Please sign in to comment.