forked from denoland/deno_kv_oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.ts
79 lines (72 loc) · 1.98 KB
/
demo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { STATUS_CODE } from "@std/http";
import {
createGitHubOAuthConfig,
getSessionId,
handleCallback,
signIn,
signOut,
} from "./mod.ts";
/**
* Modify the OAuth configuration creation function when testing for providers.
*
* @example
* ```ts
* import { createNotionOAuthConfig } from "./mod.ts";
*
* const oauthConfig = createNotionOAuthConfig();
* ```
*/
const oauthConfig = createGitHubOAuthConfig();
async function indexHandler(request: Request) {
const sessionId = await getSessionId(request);
const hasSessionIdCookie = sessionId !== undefined;
const body = `
<p>Authorization endpoint URI: ${oauthConfig.authorizationEndpointUri}</p>
<p>Token URI: ${oauthConfig.tokenUri}</p>
<p>Scope: ${oauthConfig.defaults?.scope}</p>
<p>Signed in: ${hasSessionIdCookie}</p>
<p>
<a href="/signin">Sign in</a>
</p>
<p>
<a href="/signout">Sign out</a>
</p>
<p>
<a href="https://github.com/denoland/deno_kv_oauth/blob/main/demo.ts">Source code</a>
</p>
`;
return new Response(body, {
headers: { "content-type": "text/html; charset=utf-8" },
});
}
export async function handler(request: Request): Promise<Response> {
if (request.method !== "GET") {
return new Response(null, { status: STATUS_CODE.NotFound });
}
switch (new URL(request.url).pathname) {
case "/": {
return await indexHandler(request);
}
case "/signin": {
return await signIn(request, oauthConfig);
}
case "/callback": {
try {
const { response } = await handleCallback(request, oauthConfig);
return response;
} catch {
return new Response(null, { status: STATUS_CODE.InternalServerError });
}
}
case "/signout": {
return await signOut(request);
}
default: {
return new Response(null, { status: STATUS_CODE.NotFound });
}
}
}
if (import.meta.main) {
Deno.serve(handler);
}