Skip to content

Commit

Permalink
feat: moved CMS configuration into JS
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Kozlov committed Mar 29, 2023
1 parent d41ede6 commit ad77c44
Show file tree
Hide file tree
Showing 27 changed files with 5,756 additions and 204 deletions.
8 changes: 8 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Public
BASE_URL=http://localhost:3000
GITHUB_REPO=lidofinance/cms
GITHUB_BRANCH=main

# Secret
OAUTH_CLIENT_ID =
OAUTH_CLIENT_SECRET =
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.next
22 changes: 0 additions & 22 deletions admin/config.yml

This file was deleted.

66 changes: 0 additions & 66 deletions admin/directives/table.mjs

This file was deleted.

14 changes: 0 additions & 14 deletions admin/index.html

This file was deleted.

2 changes: 0 additions & 2 deletions admin/libs/js-yaml.min.js

This file was deleted.

25 changes: 0 additions & 25 deletions admin/utils/customDirectiveUtils.mjs

This file was deleted.

5 changes: 5 additions & 0 deletions globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { CMS } from 'netlify-cms-core'

declare global {
var CMS: CMS
}
19 changes: 3 additions & 16 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
/** @type {import('next').NextConfig} */

const rawOrigin = process.env.ORIGIN;
if (rawOrigin == null || rawOrigin == "") {
console.error("You need to specify ORIGIN environment variable");
process.exit();
}
const origins = rawOrigin.split(",").map((origin) => new URL(origin).host);

const nextConfig = {
trailingSlash: true,
reactStrictMode: true,
images: {
unoptimized: true,
},
publicRuntimeConfig: {
origins,
redirectUri: process.env.REDIRECT_URL,
loginAuthTarget: process.env.AUTH_TARGET || "_self",
oauthProvider: process.env.OAUTH_PROVIDER || "github",
scopes: process.env.SCOPES || "repo",
// Supply GIT_HOSTNAME for enterprise github installs.
tokenHost: process.env.GIT_HOSTNAME || "https://github.com",
tokenPath: process.env.OAUTH_TOKEN_PATH || "/login/oauth/access_token",
authorizePath: process.env.OAUTH_AUTHORIZE_PATH || "/login/oauth/authorize",
baseUrl: process.env.BASE_URL || "http://localhost:3000",
githubRepo: process.env.GITHUB_REPO || "lidofinance/cms",
githubBranch: process.env.GITHUB_BRANCH || "main",
},
serverRuntimeConfig: {
oauthClientId: process.env.OAUTH_CLIENT_ID,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
"@types/react-dom": "18.0.10",
"eslint": "8.31.0",
"eslint-config-next": "13.1.1",
"gray-matter": "^4.0.3",
"js-yaml": "^4.1.0",
"nanoid": "^4.0.0",
"netlify-cms-app": "^2.15.72",
"next": "13.1.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"remark": "^14.0.2",
"remark-html": "^15.0.1",
"simple-oauth2": "1",
"typescript": "4.9.4"
},
"devDependencies": {
"@types/js-yaml": "^4.0.5",
"prettier": "^2.8.2"
}
}
5 changes: 5 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { AppProps } from "next/app";

export default function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
}
13 changes: 13 additions & 0 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Html, Head, Main, NextScript } from "next/document";

export default function Document() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
16 changes: 16 additions & 0 deletions pages/admin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useEffect } from "react";
import { initializeCMS, registerTable } from "widgets/cms";

export default function Tmp() {
useEffect(() => {
(async () => {
const CMS = (await import('netlify-cms-app')).default
globalThis.CMS = CMS

initializeCMS()
registerTable()
})()
}, []);

return <></>;
}
21 changes: 21 additions & 0 deletions pages/auth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { oauth2 } from "widgets/auth";
import { nanoid } from "nanoid";
import getConfig from "next/config";

const { publicRuntimeConfig } = getConfig();

export default function Auth() {
return <>Redirecting...</>;
}

export const getServerSideProps = () => {
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: publicRuntimeConfig.baseUrl + '/callback',
state: nanoid(32),
});
return {
redirect: {
destination: authorizationUri,
},
};
};
35 changes: 10 additions & 25 deletions pages/callback.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect, FC } from "react";
import { GetServerSideProps } from "next";
import getConfig from "next/config";
import { oauth2 } from "shared/authentication/api/oauth2";
import { oauth2 } from "widgets/auth";

const { serverRuntimeConfig, publicRuntimeConfig } = getConfig();
const { publicRuntimeConfig } = getConfig();

interface CallbackProps {
message: string;
Expand All @@ -12,12 +12,12 @@ interface CallbackProps {

const Callback: FC<CallbackProps> = ({ message, content }) => {
useEffect(() => {
const oauthProvider = publicRuntimeConfig.oauthProvider;
const origins = publicRuntimeConfig.origins;
const oauthProvider = 'github';
function recieveMessage(e: MessageEvent) {
console.log("recieveMessage %o", e);
const origin = new URL(e.origin).host;
if (!origins.includes(origin)) {
const originHost = new URL(e.origin).host;
const baseUrlHost = new URL(publicRuntimeConfig.baseUrl).host
if (originHost !== baseUrlHost) {
console.log("Invalid origin: %s", e.origin);
return;
}
Expand All @@ -38,21 +38,6 @@ const Callback: FC<CallbackProps> = ({ message, content }) => {

export default Callback;

const getOptions = (code: string) => {
let options: Record<string, string> = {
code,
};

if (publicRuntimeConfig.oauthProvider === "gitlab") {
options.client_id = serverRuntimeConfig.oauthClientId;
options.client_secret = serverRuntimeConfig.oauthClientServer;
options.grant_type = "authorization_code";
options.redirect_uri = publicRuntimeConfig.redirectUri;
}

return options;
};

const getAccessToken = (options: Record<string, string>) =>
new Promise<{ message: string; content: any }>((resolve) => {
oauth2.authorizationCode.getToken(options, (error: Error, result: any) => {
Expand All @@ -64,12 +49,11 @@ const getAccessToken = (options: Record<string, string>) =>
});
} else {
const token = oauth2.accessToken.create(result);
console.log(token);
resolve({
message: "success",
content: {
token: token.token.access_token,
provider: publicRuntimeConfig.oauthProvider,
provider: 'github'
},
});
}
Expand All @@ -80,8 +64,9 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
if (context.query.code == null || Array.isArray(context.query.code)) {
throw new Error("code wasn't specified");
}
const options = await getOptions(context.query.code);
const { message, content } = await getAccessToken(options);
const { message, content } = await getAccessToken({
code: context.query.code
});
return {
props: {
message,
Expand Down
17 changes: 17 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { GetStaticProps } from "next";
import { FC } from "react";

const Index: FC = () => {
return <>Redirecting...</>;
};

export default Index;

export const getStaticProps: GetStaticProps = async () => {
return {
redirect: {
permanent: false,
destination: "/admin",
},
};
};
Loading

0 comments on commit ad77c44

Please sign in to comment.