-
Notifications
You must be signed in to change notification settings - Fork 0
fix: live admin page authenticates with github #87
base: main
Are you sure you want to change the base?
Changes from 5 commits
940a8b5
e3f4574
43f81e2
77ca364
aaf2feb
ef8e69b
ae18038
cc272aa
ed18095
7c1acbe
8aca097
9dfcf36
24c2881
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as simpleOauthModule from "simple-oauth2" | ||
|
||
export const create = () => | ||
simpleOauthModule.create({ | ||
client: { | ||
id: process.env.OAUTH_CLIENT_ID || "", | ||
secret: process.env.OAUTH_CLIENT_SECRET || "", | ||
}, | ||
auth: { | ||
tokenHost: `https://github.com`, | ||
tokenPath: `/login/oauth/access_token`, | ||
authorizePath: `/login/oauth/authorize`, | ||
}, | ||
}) | ||
|
||
export const renderBody = (status: string, content: object) => ` | ||
<script> | ||
const receiveMessage = (message) => { | ||
window.opener.postMessage( | ||
'authorization:github:${status}:${JSON.stringify(content)}', | ||
message.origin | ||
); | ||
|
||
window.removeEventListener("message", receiveMessage, false); | ||
} | ||
window.addEventListener("message", receiveMessage, false); | ||
|
||
window.opener.postMessage("authorizing:github", "*"); | ||
</script> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs to go in the |
||
` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,16 @@ import { defineConfig } from "astro/config"; | |
import mdx from "@astrojs/mdx"; | ||
import sitemap from "@astrojs/sitemap"; | ||
import tailwind from "@astrojs/tailwind"; | ||
|
||
import react from "@astrojs/react"; | ||
|
||
import node from "@astrojs/node"; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
site: "https://example.com", | ||
integrations: [mdx(), sitemap(), tailwind(), react()] | ||
output: "hybrid", | ||
integrations: [mdx(), sitemap(), tailwind(), react()], | ||
adapter: node({ | ||
mode: "standalone" | ||
}) | ||
Comment on lines
+12
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably why firebase is upset |
||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"dependencies": { | ||
"@astrojs/check": "^0.5.3", | ||
"@astrojs/mdx": "^2.1.1", | ||
"@astrojs/node": "^8.3.3", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What did you need node for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Astro requires an ssr adapter in order to enable server side rendering : https://docs.astro.build/en/guides/server-side-rendering |
||
"@astrojs/react": "^3.1.0", | ||
"@astrojs/rss": "^4.0.5", | ||
"@astrojs/sitemap": "^3.0.5", | ||
|
@@ -36,11 +37,13 @@ | |
"react-dom": "^18.2.0", | ||
"react-hook-form": "^7.51.3", | ||
"react-select": "^5.8.0", | ||
"simple-oauth2": "2.5.2", | ||
"tailwindcss": "^3.4.1", | ||
"typescript": "^5.3.3" | ||
}, | ||
"devDependencies": { | ||
"@brown-ccv/eslint-config": "^0.3.0", | ||
"@types/simple-oauth2": "^2.5.2", | ||
"eslint": "^8.56.0", | ||
"eslint-plugin-astro": "^0.31.4", | ||
"prettier": "^3.2.5", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
import Layout from "../../layouts/Layout.astro" | ||
import { GET } from "./auth" | ||
|
||
const auth = await GET(Astro.url.origin) | ||
--- | ||
|
||
<Layout title="Authenticate" description="" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need this if the thing is calling |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import crypto from "crypto" | ||
import { create } from "../../../api/_lib/oauth2.ts" | ||
|
||
export const prerender = false | ||
|
||
export const GET = (host: string) => { | ||
const randomString = () => crypto.randomBytes(4).toString(`hex`) | ||
const oauth2 = create() | ||
|
||
const url = oauth2.authorizationCode.authorizeURL({ | ||
redirect_uri: `${host}/api/callback`, | ||
scope: `repo,user`, | ||
state: randomString(), | ||
}) | ||
return Response.redirect(url, 301) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
import Layout from "../../layouts/Layout.astro" | ||
import { GetCallback } from "./callback" | ||
|
||
const callback = GetCallback(Astro.request, Astro.url.origin) | ||
--- | ||
|
||
<Layout title="Callback" description="" /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { create, renderBody } from "../../../api/_lib/oauth2.ts" | ||
|
||
export const prerender = false | ||
|
||
export const GetCallback = async (req: Request, host: string) => { | ||
const params = new URLSearchParams(req.url) | ||
const code = params.get("code") || "" | ||
const oauth2 = create() | ||
|
||
try { | ||
const accessToken = await oauth2.authorizationCode.getToken({ | ||
code, | ||
redirect_uri: `${host}/api/callback`, | ||
}) | ||
const { token } = oauth2.accessToken.create(accessToken) | ||
return new Response( | ||
renderBody("success", { | ||
token: token.access_token, | ||
provider: "github", | ||
}), | ||
{ status: 200 } | ||
) | ||
} catch (e: any) { | ||
return new Response(renderBody("error", e.message), { status: 200 }) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably move this code into the
src
directory. I think this folder was left over from the next template