Skip to content

Commit f6a4b0b

Browse files
committed
[add] API proxy & OAuth middleware of GitHub
1 parent 7d08fc7 commit f6a4b0b

File tree

6 files changed

+40
-10
lines changed

6 files changed

+40
-10
lines changed

components/Layout/MainNavigator.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { BrandLogo, GithubIcon } from './Svg';
1111

1212
export const mainNavLinks = () => [
1313
{ title: t('latest_projects'), href: '/project' },
14-
{ title: 'GitHub-reward', href: '/project/reward/issue' },
14+
{ title: 'GitHub-reward', href: '/project/reward/issue', target: '_top' },
1515
{ title: t('member'), href: '/member' },
1616
{ title: t('open_source_project'), href: '/open-source' },
1717
];
@@ -27,8 +27,8 @@ export class MainNavigator extends Component {
2727
};
2828

2929
renderLinks = () =>
30-
mainNavLinks().map(({ title, href }) => (
31-
<Link key={title} className="py-1" href={href}>
30+
mainNavLinks().map(({ title, href, target }) => (
31+
<Link key={title} className="py-1" href={href} target={target}>
3232
{title}
3333
</Link>
3434
));

models/Base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if (!isServer()) githubClient.baseURI = `${API_Host}/api/GitHub/`;
1010
githubClient.use(({ request }, next) => {
1111
if (GithubToken)
1212
request.headers = {
13-
authorization: `Bearer ${GithubToken}`,
13+
Authorization: `Bearer ${GithubToken}`,
1414
...request.headers,
1515
};
1616
return next();

models/configuration.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { parseCookie } from 'mobx-i18n';
2-
31
export const Name = process.env.NEXT_PUBLIC_SITE_NAME,
42
Summary = process.env.NEXT_PUBLIC_SITE_SUMMARY,
53
DefaultImage = process.env.NEXT_PUBLIC_LOGO || '/og.png';
64

75
export const isServer = () => typeof window === 'undefined';
86

9-
export const VercelHost = process.env.VERCEL_URL;
7+
export const { VERCEL } = process.env,
8+
VercelHost = process.env.VERCEL_URL;
109

1110
export const API_Host = isServer()
1211
? VercelHost
@@ -18,8 +17,9 @@ export const CACHE_HOST = process.env.NEXT_PUBLIC_CACHE_HOST!;
1817

1918
export const { CRAWLER_TOKEN, JWT_SECRET } = process.env;
2019

21-
export const GithubToken =
22-
parseCookie(globalThis.document?.cookie || '').token || process.env.GITHUB_TOKEN;
20+
export const ProxyBaseURL = `https://idea2.app/proxy`;
21+
22+
export const GithubToken = process.env.GITHUB_TOKEN;
2323

2424
export const LARK_API_HOST = `${API_Host}/api/Lark/`;
2525

next.config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { withSentryConfig } from '@sentry/nextjs';
2+
import { NextConfig } from 'next';
23
import setPWA from 'next-pwa';
34
import webpack from 'webpack';
45

@@ -12,6 +13,20 @@ const withPWA = setPWA({
1213
disable: isDev,
1314
});
1415

16+
const rewrites: NextConfig['rewrites'] = async () => ({
17+
beforeFiles: [
18+
{
19+
source: '/proxy/github.com/:path*',
20+
destination: 'https://github.com/:path*',
21+
},
22+
{
23+
source: '/proxy/raw.githubusercontent.com/:path*',
24+
destination: 'https://raw.githubusercontent.com/:path*',
25+
},
26+
],
27+
afterFiles: [],
28+
});
29+
1530
const nextConfig = withPWA({
1631
output: CI ? 'standalone' : undefined,
1732
compiler: {
@@ -30,6 +45,7 @@ const nextConfig = withPWA({
3045
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
3146
return config;
3247
},
48+
rewrites,
3349
});
3450

3551
export default isDev || !SENTRY_AUTH_TOKEN

pages/api/GitHub/core.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Context, Middleware } from 'koa';
22
import { githubClient } from 'mobx-github';
3+
import { githubOAuth2 } from 'next-ssr-middleware';
4+
5+
import { ProxyBaseURL, VERCEL } from '../../../models/configuration';
36

47
export const proxyGithub = async <T>({
58
method,
@@ -20,3 +23,13 @@ export const proxyGitHubAll: Middleware = async context => {
2023
context.status = status;
2124
context.body = body;
2225
};
26+
27+
const client_id = process.env.GITHUB_OAUTH_CLIENT_ID!,
28+
client_secret = process.env.GITHUB_OAUTH_CLIENT_SECRET!;
29+
30+
export const githubOAuth = githubOAuth2({
31+
rootBaseURL: VERCEL ? undefined : `${ProxyBaseURL}/github.com/`,
32+
client_id,
33+
client_secret,
34+
scopes: ['user', 'repo'],
35+
});

pages/project/reward/issue.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import { PageHead } from '../../../components/PageHead';
99
import { ScrollList } from '../../../components/ScrollList';
1010
import issueStore, { IssueFilter, IssueModel } from '../../../models/Issue';
1111
import { i18n } from '../../../models/Translation';
12+
import { githubOAuth } from '../../api/GitHub/core';
1213

1314
const issueFilter: IssueFilter = {
1415
repository_url: 'https://github.com/idea2app',
1516
state: 'open',
1617
title: 'reward',
1718
};
1819

19-
export const getServerSideProps = compose(cache(), errorLogger, translator(i18n), async () => {
20+
export const getServerSideProps = compose(githubOAuth, errorLogger, translator(i18n), async () => {
2021
const list = await new IssueModel().getList(issueFilter);
2122

2223
return { props: JSON.parse(JSON.stringify({ list })) };

0 commit comments

Comments
 (0)