docs: 🐛 remove oauth param from .env + code refacto

Signed-off-by: slumbering <slumbering.pierrot@gmail.com>
This commit is contained in:
slumbering 2021-06-18 16:04:40 +02:00
parent 8c2ef129cf
commit 5171b1666c
3 changed files with 16 additions and 22 deletions

View File

@ -4,8 +4,10 @@
"private": true, "private": true,
"scripts": { "scripts": {
"docusaurus": "docusaurus", "docusaurus": "docusaurus",
"start": "docusaurus start", "start": "OAUTH_ENABLE=false docusaurus start",
"build": "docusaurus build", "start:withAuth": "OAUTH_ENABLE=true docusaurus start",
"build": "OAUTH_ENABLE=true docusaurus build",
"build:withoutAuth": "OAUTH_ENABLE=false docusaurus build",
"swizzle": "docusaurus swizzle", "swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy", "deploy": "docusaurus deploy",
"clear": "docusaurus clear", "clear": "docusaurus clear",

View File

@ -21,7 +21,7 @@ function bindApiCall({ url, config, errorMessage }) {
async function getAccessToken(code) { async function getAccessToken(code) {
const accessToken = await bindApiCall({ const accessToken = await bindApiCall({
url: `${process.env.REACT_APP_API_PROXY_ENABLE ? '/github-proxy' : 'https://github.com' }/login/oauth/access_token`, url: `${process.env.REACT_APP_API_PROXY_ENABLE == 'true' ? '/github-proxy' : 'https://github.com'}/login/oauth/access_token`,
config: { config: {
params: { params: {
code, code,
@ -37,7 +37,7 @@ async function getAccessToken(code) {
export async function getUser(access_token) { export async function getUser(access_token) {
const user = await bindApiCall({ const user = await bindApiCall({
url: `${process.env.REACT_APP_API_PROXY_ENABLE ? '/github-api-proxy' : 'https://api.github.com'}/user`, url: `${process.env.REACT_APP_API_PROXY_ENABLE == 'true' ? '/github-api-proxy' : 'https://api.github.com'}/user`,
config: { config: {
headers: { Authorization: `token ${access_token}` }, headers: { Authorization: `token ${access_token}` },
}, },
@ -49,7 +49,6 @@ export async function getUser(access_token) {
error: user.data?.error_description, error: user.data?.error_description,
status: user.status status: user.status
} }
} }
export async function checkUserCollaboratorStatus(code) { export async function checkUserCollaboratorStatus(code) {
@ -57,11 +56,11 @@ export async function checkUserCollaboratorStatus(code) {
const { login } = await getUser(access_token) const { login } = await getUser(access_token)
const isUserCollaborator = await bindApiCall({ const isUserCollaborator = await bindApiCall({
url: `${process.env.REACT_APP_API_PROXY_ENABLE ? '/docs-access' : 'https://j20f3pfq11.execute-api.us-east-1.amazonaws.com/Prod/u'}/${login}`, url: `${process.env.REACT_APP_API_PROXY_ENABLE == 'true' ? '/docs-access' : 'https://j20f3pfq11.execute-api.us-east-1.amazonaws.com/Prod/u'}/${login}`,
errorMessage: 'error checkUserCollaboratorStatus' errorMessage: 'error checkUserCollaboratorStatus'
}) })
return { return {
isAllowed: isUserCollaborator.data userPermission: isUserCollaborator.data
} }
} }

View File

@ -139,8 +139,7 @@ function DocPage(props) {
); );
// CUSTOM DOCPAGE // CUSTOM DOCPAGE
if (process.env.REACT_APP_OAUTH_ENABLE == 'true') { if (process.env.OAUTH_ENABLE == 'true') {
const [isUserAuthorized, setIsUserAuthorized] = useState()
const [isLoading, setIsLoading] = useState(true) const [isLoading, setIsLoading] = useState(true)
const [redirectState, setRedirectState] = useState() const [redirectState, setRedirectState] = useState()
const authQuery = qs.parse(location.search); const authQuery = qs.parse(location.search);
@ -149,17 +148,11 @@ function DocPage(props) {
})()) })())
useEffect(async () => { useEffect(async () => {
if (userAccessStatus) { if (!isEmpty(authQuery) && userAccessStatus === null) { //callback after successful auth with github
setIsUserAuthorized(userAccessStatus) const isUserCollaborator = await checkUserCollaboratorStatus(authQuery.code);
} else { setUserAccessStatus(isUserCollaborator?.userPermission)
if (!isEmpty(authQuery)) { //callback after successful auth with github if (isUserCollaborator?.userPermission) {
const isUserCollaborator = await checkUserCollaboratorStatus(authQuery.code); if (typeof window !== "undefined") window.localStorage.setItem('user-github-isAllowed', isUserCollaborator?.userPermission);
if (isUserCollaborator?.isAllowed) {
setUserAccessStatus(isUserCollaborator?.isAllowed)
if (typeof window !== "undefined") window.localStorage.setItem('user-github-isAllowed', isUserCollaborator?.isAllowed);
}
setIsUserAuthorized(isUserCollaborator?.isAllowed)
} }
} }
setIsLoading(false) setIsLoading(false)
@ -168,11 +161,11 @@ function DocPage(props) {
if (isLoading) return <Spinner /> if (isLoading) return <Spinner />
if (isUserAuthorized === false) { if (userAccessStatus === false) {
return <DocPageRedirect /> return <DocPageRedirect />
} }
if (typeof isUserAuthorized == 'undefined' || isUserAuthorized?.status === 401) { if (userAccessStatus === null) {
return ( return (
<DocPageAuthentication /> <DocPageAuthentication />
) )