Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
test: OliverSpeir/astro-decap-starter-ssr/
Browse files Browse the repository at this point in the history
  • Loading branch information
hetd54 committed Oct 16, 2024
1 parent cc272aa commit ed18095
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 119 deletions.
6 changes: 4 additions & 2 deletions public/admin/config.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
backend:
name: github
repo: brown-ccv/mmp
base_url: https://mmp-site-b1c9b.web.app/
auth_endpoint: api/auth
site_domain: mmp.research.brown.edu
base_url: https://mmp.research.brown.edu/
auth_endpoint: oauth
media_folder: public/images
public_folder: public/images
media_library:
max_file_size: 734003200
i18n:
Expand Down
16 changes: 0 additions & 16 deletions public/admin/index.html

This file was deleted.

30 changes: 0 additions & 30 deletions src/api/_lib/oauth2.ts

This file was deleted.

23 changes: 23 additions & 0 deletions src/pages/admin.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="noindex" />
<link href="/admin/config.yml" type="text/yaml" rel="cms-config-url" />
<title>Content Manager</title>
</head>
<body>
<!-- Dashboard script-->
<script src="https://unpkg.com/decap-cms@^3.1.0-beta.2/dist/decap-cms.js"></script>
<!-- CSS in preview panel -->
<script>
// @ts-expect-error CMS is defined globally
// eslint-disable-next-line
CMS.registerPreviewStyle("/global.css")
</script>

<!-- Only showing body in preview panel -->
<script src="@utils/preview.js"></script>
</body>
</html>
21 changes: 0 additions & 21 deletions src/pages/api/auth.astro

This file was deleted.

16 changes: 0 additions & 16 deletions src/pages/api/auth.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/pages/api/callback.astro

This file was deleted.

26 changes: 0 additions & 26 deletions src/pages/api/callback.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/pages/oauth/_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const clientId = process.env.OAUTH_GITHUB_CLIENT_ID || import.meta.env.OAUTH_GITHUB_CLIENT_ID
export const clientSecret =
process.env.OAUTH_GITHUB_CLIENT_SECRET || import.meta.env.OAUTH_GITHUB_CLIENT_SECRET

export const authUrl = `https://github.com/login/oauth/authorize?client_id=${clientId}&scope=repo,user`
export const tokenUrl = "https://github.com/login/oauth/access_token"
61 changes: 61 additions & 0 deletions src/pages/oauth/callback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export const prerender = "false"
import type { APIRoute } from "astro"
import { clientId, clientSecret, tokenUrl } from "./_config"

export const GET: APIRoute = async ({ url, redirect }) => {
const data = {
code: url.searchParams.get("code"),
client_id: clientId,
client_secret: clientSecret,
}

let script

try {
const response = await fetch(tokenUrl, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}

const body = await response.json()

const content = {
token: body.access_token,
provider: "github",
}

// This is what talks to the DecapCMS page.
// Using window.postMessage we give it the token details in a format it's expecting
script = `
<script>
const receiveMessage = (message) => {
window.opener.postMessage(
'authorization:${content.provider}:success:${JSON.stringify(content)}',
message.origin
);
window.removeEventListener("message", receiveMessage, false);
}
window.addEventListener("message", receiveMessage, false);
window.opener.postMessage("authorizing:${content.provider}", "*");
</script>
`

return new Response(script, {
headers: { "Content-Type": "text/html" },
})
} catch (err) {
// If we hit an error we'll handle that here
console.log(err)
return redirect("/?error=😡")
}
}
7 changes: 7 additions & 0 deletions src/pages/oauth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const prerender = "false"
import type { APIRoute } from "astro"
import { authUrl } from "./_config"

export const GET: APIRoute = ({ redirect }) => {
return redirect(authUrl)
}

0 comments on commit ed18095

Please sign in to comment.