Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate OAuth to backend #129

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,3 @@ NEXT_PUBLIC_SENTRY_DSN=

GOOGLE_ANALYTICS=G-K7333MX9FZ
API_URL=http://localhost:8080

NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=this_is_auth_secret

GITHUB_ID=
GITHUB_SECRET=

GITEE_ID=
GITEE_SECRET=

#http_proxy=
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"html2canvas": "^1.4.1",
"http-proxy": "^1.18.1",
"i18next": "^22.0.6",
"js-cookie": "^3.0.5",
"lodash": "^4.17.21",
"next": "12.2.5",
"next-auth": "^4.20.1",
"next-i18next": "^13.0.0",
"nextjs-progressbar": "^0.0.14",
"proxy-agent": "^5.0.0",
Expand Down
30 changes: 0 additions & 30 deletions patches/next-auth+4.20.1.patch

This file was deleted.

12 changes: 3 additions & 9 deletions src/common/components/Header/ChangeLanguage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react';
import { useRouter } from 'next/router';
import Cookies from 'js-cookie';
import { MdLanguage } from 'react-icons/md';
import { AiFillCaretDown } from 'react-icons/ai';
import { getDomain } from '@common/utils/getDomain';
import getLocale, { USER_LOCALE_KEY } from '@common/utils/getLocale';
import getLocale from '@common/utils/getLocale';
import { setCookieLocale } from '@common/utils/cookie';
import { NoSsr } from '@mui/base';

const languages = [
Expand All @@ -23,7 +22,6 @@ const languages = [
const ChangeLanguage = () => {
const { reload } = useRouter();
const local = getLocale();
console.log('local', local);
const language = languages.find((i) => i.id === local);

return (
Expand All @@ -41,11 +39,7 @@ const ChangeLanguage = () => {
key={item.id}
className="flex cursor-pointer border-b border-white/20 py-4 pl-6 text-center last:border-b-0 hover:bg-[#333333]"
onClick={() => {
Cookies.set(USER_LOCALE_KEY, item.id, {
expires: 365,
path: '/',
domain: getDomain(),
});
setCookieLocale(item.id);
reload();
}}
>
Expand Down
11 changes: 11 additions & 0 deletions src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ export const isDev = process.env.NODE_ENV === 'development';
export const isProd = process.env.NODE_ENV === 'production';

export type CommunityRepoType = 'governance' | 'software-artifact';

export const oauthProvider = {
github: {
id: 'github',
name: 'GitHub',
},
gitee: {
id: 'gitee',
name: 'Gitee',
},
};
64 changes: 0 additions & 64 deletions src/common/lib/authProviders/gitee.ts

This file was deleted.

102 changes: 0 additions & 102 deletions src/common/lib/authProviders/github.ts

This file was deleted.

45 changes: 45 additions & 0 deletions src/common/utils/cookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import jsCookie from 'js-cookie';
import { getDomain } from './getDomain';

export const cookieKeys = {
USER_LOCALE_KEY: 'locale',
AUTH_CALLBACK_URL: 'auth.callback-url',
AUTH_PROVIDER: 'auth.provider',
};

// ------------------------------auth callback url-----------------------------------
const inMinutes = 1 / (24 * 60);

export const setCallbackUrl = (path: string) => {
jsCookie.set(cookieKeys.AUTH_CALLBACK_URL, path, {
expires: 5 * inMinutes,
path: '/',
domain: getDomain(),
});
};

// -----------------------------auth provider------------------------------------
export const setAuthProvider = (val: string) => {
jsCookie.set(cookieKeys.AUTH_PROVIDER, val, {
expires: 365,
path: '/',
domain: getDomain(),
});
};

export const getAuthProvider = () => {
return jsCookie.get(cookieKeys.AUTH_PROVIDER);
};

// -----------------------------locale------------------------------------
export const setCookieLocale = (local: string) => {
jsCookie.set(cookieKeys.USER_LOCALE_KEY, local, {
expires: 365,
path: '/',
domain: getDomain(),
});
};

export const getCookieLocale = () => {
return jsCookie.get(cookieKeys.USER_LOCALE_KEY);
};
19 changes: 9 additions & 10 deletions src/common/utils/getLocale.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { NextApiRequestCookies } from 'next/dist/server/api-utils';
import Cookies from 'js-cookie';
import { navigatorLangLookup } from '@common/utils/languageDetector';
import { getDomain } from '@common/utils/getDomain';
import {
cookieKeys,
setCookieLocale,
getCookieLocale,
} from '@common/utils/cookie';
const { USER_LOCALE_KEY } = cookieKeys;

type TypeLang = 'zh' | 'en';

const locales = ['zh', 'en'];
export const USER_LOCALE_KEY = 'locale';

function getLocale(): TypeLang;

Expand All @@ -17,7 +20,7 @@ function getLocale(reqCookies?: NextApiRequestCookies | undefined): TypeLang {
const language = reqCookies[USER_LOCALE_KEY] as TypeLang;
return locales.includes(language) ? language : 'en';
} else if (typeof reqCookies === 'undefined') {
return (Cookies.get(USER_LOCALE_KEY) as TypeLang) || 'en';
return (getCookieLocale() as TypeLang) || 'en';
}
return 'en';
}
Expand All @@ -33,13 +36,9 @@ export function getLang(found: string[]) {
export function browserLanguageDetectorAndReload() {
const found = navigatorLangLookup();

if (found && !Cookies.get(USER_LOCALE_KEY)) {
if (found && !getCookieLocale()) {
const lang = getLang(found);
Cookies.set(USER_LOCALE_KEY, lang, {
expires: 365,
path: '/',
domain: getDomain(),
});
setCookieLocale(lang);
window.location.reload();
}
}
Loading