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

Add Auth2 token generation and new fetching with tokens for the orgchart #3

Open
wants to merge 3 commits into
base: choreo
Choose a base branch
from
Open
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
123 changes: 93 additions & 30 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, {useState} from 'react';
import {Route, Routes} from "react-router-dom";
import React, { useState, useEffect } from 'react';
import { Route, Routes } from "react-router-dom";
import './index.css';
import Header from "./shared/header/Header";
import TreeView from "./tree/Tree";
import {createTheme, ThemeProvider} from '@mui/material/styles';
import {generateSearchQuery} from "./tree/functions/generateSearchQuery";
import {ApiRoutes, getServerUrl} from "./server";
import { createTheme, ThemeProvider } from '@mui/material/styles';
import { generateSearchQuery } from "./tree/functions/generateSearchQuery";
import { ApiRoutes, getServerUrl } from "./server";

const appTheme = createTheme({
palette: {
Expand All @@ -14,11 +14,12 @@ const appTheme = createTheme({
});

function App() {

const [searchKey, setSearchKey] = useState("");
const [searchResults, setSearchResults] = useState(null);
const [loadedEntity, setLoadedEntity] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [accessToken, setAccessToken] = useState(null);

const app_props = {
searchKey, setSearchKey,
searchResults, setSearchResults,
Expand All @@ -27,41 +28,103 @@ function App() {
getSearchResults, getEntity
};

useEffect(() => {
getAccessToken();
}, []);

async function getAccessToken() {
// Token request URL, defaults to "/". Used to obtain the access token for OAuth 2.0.
const tokenEndpoint = window?.configs?.tokenEndpoint ? window.configs.tokenEndpoint : "/";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments for readability. Explain what these configs are for

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.
// OAuth 2.0 client ID, defaults to "id". Identifies the application.
const clientId = window?.configs?.clientId ? window.configs.clientId : "id";

// OAuth 2.0 client secret, defaults to "secret". Authenticates the application.
const clientSecret = window?.configs?.clientSecret ? window.configs.clientSecret : "secret";

try {
const response = await fetch(tokenEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
}),
});

if (!response.ok) {
throw new Error('Failed to get access token');
}

const data = await response.json();
setAccessToken(data.access_token);
} catch (error) {
console.error('Error getting access token:', error);
}
}

function getSearchResults(searchInput) {
async function getSearchResults(searchInput) {
setIsLoading(true);
if (searchInput.length > 1) {
if (searchInput.length > 1 && accessToken) {
let searchUrl = generateSearchQuery(searchInput);
searchUrl += '&limit=0';

fetch(searchUrl, {
method: 'GET'
}).then(results => {
return results.json();
}).then(data => {
try {
const response = await fetch(searchUrl, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
});

if (!response.ok) {
throw new Error('Search request failed');
}

const data = await response.json();
setSearchResults(data);
}).then(
setIsLoading(false)
);
} catch (error) {
console.error('Error in getSearchResults:', error);
} finally {
setIsLoading(false);
}
} else {
setIsLoading(false);
}

}

function getEntity(title, callback) {
async function getEntity(title, callback) {
setIsLoading(true);
fetch(getServerUrl(ApiRoutes.entity) + title, {
method: 'GET'
}).then(results => {
if (results.status === 200) {
return results.json();
if (accessToken) {
try {
const response = await fetch(getServerUrl(ApiRoutes.entity) + title, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
});

if (response.status === 200) {
const data = await response.json();
setLoadedEntity(data);
callback();
} else {
setLoadedEntity(null);
}
} catch (error) {
console.error('Error in getEntity:', error);
setLoadedEntity(null);
} finally {
setIsLoading(false);
}
return null
}).then(data => {
setLoadedEntity(data);
callback();
}).then(
setIsLoading(false)
)
} else {
setIsLoading(false);
}
}

return (
Expand Down