@c-time/frelio-cms 1.3.7 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,122 @@
1
+ // functions/api/auth/callback.ts
2
+ function errorHtml(title, message) {
3
+ return `<!DOCTYPE html>
4
+ <html lang="ja">
5
+ <head>
6
+ <meta charset="UTF-8">
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
+ <title>${title} - Frelio</title>
9
+ <style>
10
+ * { margin: 0; padding: 0; box-sizing: border-box; }
11
+ body {
12
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
13
+ background: #f5f5f5;
14
+ display: flex;
15
+ justify-content: center;
16
+ align-items: center;
17
+ min-height: 100vh;
18
+ color: #333;
19
+ }
20
+ .card {
21
+ background: #fff;
22
+ border-radius: 12px;
23
+ box-shadow: 0 2px 12px rgba(0,0,0,0.1);
24
+ padding: 40px;
25
+ max-width: 480px;
26
+ width: 90%;
27
+ text-align: center;
28
+ }
29
+ .icon {
30
+ font-size: 48px;
31
+ margin-bottom: 16px;
32
+ }
33
+ h1 {
34
+ font-size: 20px;
35
+ font-weight: 600;
36
+ margin-bottom: 12px;
37
+ }
38
+ p {
39
+ font-size: 14px;
40
+ color: #666;
41
+ line-height: 1.6;
42
+ margin-bottom: 24px;
43
+ }
44
+ a {
45
+ display: inline-block;
46
+ background: #1976d2;
47
+ color: #fff;
48
+ text-decoration: none;
49
+ padding: 10px 24px;
50
+ border-radius: 6px;
51
+ font-size: 14px;
52
+ font-weight: 500;
53
+ transition: background 0.2s;
54
+ }
55
+ a:hover { background: #1565c0; }
56
+ </style>
57
+ </head>
58
+ <body>
59
+ <div class="card">
60
+ <div class="icon">\u26A0\uFE0F</div>
61
+ <h1>${title}</h1>
62
+ <p>${message}</p>
63
+ <a href="/">\u30C0\u30C3\u30B7\u30E5\u30DC\u30FC\u30C9\u306B\u623B\u308B</a>
64
+ </div>
65
+ </body>
66
+ </html>`;
67
+ }
68
+ var onRequestGet = async (context) => {
69
+ const url = new URL(context.request.url);
70
+ const code = url.searchParams.get("code");
71
+ if (!code) {
72
+ return new Response(
73
+ errorHtml("\u8A8D\u8A3C\u30A8\u30E9\u30FC", "\u8A8D\u8A3C\u30B3\u30FC\u30C9\u304C\u898B\u3064\u304B\u308A\u307E\u305B\u3093\u3067\u3057\u305F\u3002\u30C0\u30C3\u30B7\u30E5\u30DC\u30FC\u30C9\u306B\u623B\u3063\u3066\u518D\u5EA6\u30ED\u30B0\u30A4\u30F3\u3057\u3066\u304F\u3060\u3055\u3044\u3002"),
74
+ { status: 400, headers: { "Content-Type": "text/html; charset=utf-8" } }
75
+ );
76
+ }
77
+ const clientId = context.env.GITHUB_CLIENT_ID || context.env.VITE_GITHUB_CLIENT_ID;
78
+ if (!clientId) {
79
+ return new Response(
80
+ errorHtml("\u30B5\u30FC\u30D0\u30FC\u8A2D\u5B9A\u30A8\u30E9\u30FC", "\u30B5\u30FC\u30D0\u30FC\u306E\u8A2D\u5B9A\u306B\u554F\u984C\u304C\u3042\u308A\u307E\u3059\u3002\u7BA1\u7406\u8005\u306B\u304A\u554F\u3044\u5408\u308F\u305B\u304F\u3060\u3055\u3044\u3002"),
81
+ { status: 500, headers: { "Content-Type": "text/html; charset=utf-8" } }
82
+ );
83
+ }
84
+ try {
85
+ const tokenResponse = await fetch(
86
+ "https://github.com/login/oauth/access_token",
87
+ {
88
+ method: "POST",
89
+ headers: {
90
+ Accept: "application/json",
91
+ "Content-Type": "application/json"
92
+ },
93
+ body: JSON.stringify({
94
+ client_id: clientId,
95
+ client_secret: context.env.GITHUB_CLIENT_SECRET,
96
+ code
97
+ })
98
+ }
99
+ );
100
+ const tokenData = await tokenResponse.json();
101
+ if (tokenData.error) {
102
+ return new Response(
103
+ errorHtml(
104
+ "\u8A8D\u8A3C\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
105
+ "\u8A8D\u8A3C\u30B3\u30FC\u30C9\u304C\u7121\u52B9\u307E\u305F\u306F\u671F\u9650\u5207\u308C\u3067\u3059\u3002\u30C0\u30C3\u30B7\u30E5\u30DC\u30FC\u30C9\u306B\u623B\u3063\u3066\u30D6\u30E9\u30A6\u30B6\u3092\u30EA\u30ED\u30FC\u30C9\u3057\u3001\u518D\u5EA6\u30ED\u30B0\u30A4\u30F3\u3057\u3066\u304F\u3060\u3055\u3044\u3002"
106
+ ),
107
+ { status: 400, headers: { "Content-Type": "text/html; charset=utf-8" } }
108
+ );
109
+ }
110
+ const origin = url.origin;
111
+ const redirectUrl = `${origin}/auth/callback?token=${tokenData.access_token}`;
112
+ return Response.redirect(redirectUrl, 302);
113
+ } catch {
114
+ return new Response(
115
+ errorHtml("\u8A8D\u8A3C\u30A8\u30E9\u30FC", "\u8A8D\u8A3C\u51E6\u7406\u4E2D\u306B\u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F\u3002\u30C0\u30C3\u30B7\u30E5\u30DC\u30FC\u30C9\u306B\u623B\u3063\u3066\u518D\u5EA6\u30ED\u30B0\u30A4\u30F3\u3057\u3066\u304F\u3060\u3055\u3044\u3002"),
116
+ { status: 500, headers: { "Content-Type": "text/html; charset=utf-8" } }
117
+ );
118
+ }
119
+ };
120
+ export {
121
+ onRequestGet
122
+ };