Skip to content

Commit

Permalink
dev env bypass authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
NishantBorthakur committed Jun 6, 2023
1 parent 794440c commit 1cc67c6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 29 deletions.
2 changes: 1 addition & 1 deletion gui/pages/Content/Agents/AgentCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function AgentCreate({sendAgentData, selectedProjectId, fetchAgen
const [goals, setGoals] = useState(['agent goal 1']);

const models = ['gpt-4', 'gpt-3.5-turbo']
const [model, setModel] = useState(models[0]);
const [model, setModel] = useState(models[1]);
const modelRef = useRef(null);
const [modelDropdown, setModelDropdown] = useState(false);

Expand Down
10 changes: 6 additions & 4 deletions gui/pages/Content/Agents/ResourceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {ToastContainer, toast} from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import {getResources, uploadFile} from "@/pages/api/DashboardService";
import {formatBytes, downloadFile} from "@/utils/utils";
import axios from 'axios';
import api, {baseUrl} from "@/pages/api/apiConfig";

export default function ResourceManager({agentId}) {
const [output, setOutput] = useState([]);
Expand All @@ -24,7 +26,7 @@ export default function ResourceManager({agentId}) {
"size": files[0].size,
"type": files[0].type,
};
uploadResource(fileData);
uploadFile(fileData);
}
};

Expand Down Expand Up @@ -56,22 +58,22 @@ export default function ResourceManager({agentId}) {
"size": files[0].size,
"type": files[0].type,
};
uploadResource(fileData);
uploadFile(fileData);
}
};

useEffect(() => {
fetchResources();
}, [agentId]);

function uploadResource(fileData) {
function uploadFile(fileData) {
const formData = new FormData();
formData.append('file', fileData.file);
formData.append('name', fileData.name);
formData.append('size', fileData.size);
formData.append('type', fileData.type);

uploadFile(agentId, formData)
axios.post(`${baseUrl()}/resources/add/${agentId}`, formData)
.then((response) => {
fetchResources();
toast.success('Resource added successfully', { autoClose: 1800 });
Expand Down
4 changes: 2 additions & 2 deletions gui/pages/Dashboard/TopBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {ToastContainer, toast} from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import {refreshUrl} from "@/utils/utils";

export default function TopBar({selectedProject, userName}) {
export default function TopBar({selectedProject, userName, env}) {
const [dropdown, setDropdown] = useState(false);
const [settingsModal, setSettingsModal] = useState(false);
const router = useRouter();
Expand Down Expand Up @@ -71,7 +71,7 @@ export default function TopBar({selectedProject, userName}) {
<div className={styles.top_right_icon} onClick={() => setDropdown(!dropdown)}>
<Image width={20} height={20} src="/images/profile_pic.png" alt="dropdown-icon"/>
</div>
{dropdown && <div style={{marginTop:'13vh',marginRight:'-45px'}} onMouseEnter={() => setDropdown(true)} onMouseLeave={() => setDropdown(false)}>
{dropdown && env !== 'DEV' && <div style={{marginTop:'13vh',marginRight:'-45px'}} onMouseEnter={() => setDropdown(true)} onMouseLeave={() => setDropdown(false)}>
<ul className="dropdown_container" style={{width:'fit-content'}}>
<li className="dropdown_item" onClick={() => setDropdown(false)}>{userName}</li>
<li className="dropdown_item" onClick={logoutUser}>Logout</li>
Expand Down
72 changes: 51 additions & 21 deletions gui/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'bootstrap/dist/css/bootstrap.css';
import './_app.css'
import Head from 'next/head';
import Image from "next/image";
import { getOrganisation, getProject, validateAccessToken } from "@/pages/api/DashboardService";
import { getOrganisation, getProject, validateAccessToken, checkEnvironment, addUser } from "@/pages/api/DashboardService";
import { githubClientId } from "@/pages/api/apiConfig";
import { useRouter } from 'next/router';
import querystring from 'querystring';
Expand All @@ -18,32 +18,62 @@ export default function App() {
const [selectedProject, setSelectedProject] = useState(null);
const [userName, setUserName] = useState('');
const [organisationId, setOrganisationId] = useState(null);
const [env, setEnv] = useState('DEV');
const router = useRouter();

function fetchOrganisation(userId) {
getOrganisation(userId)
.then((response) => {
setOrganisationId(response.data.id);
})
.catch((error) => {
console.error('Error fetching project:', error);
});
}

useEffect(() => {
const queryParams = router.asPath.split('?')[1];
const parsedParams = querystring.parse(queryParams);
let access_token = parsedParams.access_token || null;
checkEnvironment()
.then((response) => {
setEnv(response.data.env);
if (response.data.env === 'DEV') {
const userData = {
"name" : "SuperAGI User",
"email" : "super6@agi.com",
"password" : "pass@123",
}

if(typeof window !== 'undefined' && access_token) {
localStorage.setItem('accessToken', access_token);
refreshUrl();
}
addUser(userData)
.then((response) => {
setUserName(response.data.name);
isTokenAuthenticated(true);
fetchOrganisation(response.data.id);
})
.catch((error) => {
console.error('Error adding user:', error);
});
} else {
const queryParams = router.asPath.split('?')[1];
const parsedParams = querystring.parse(queryParams);
let access_token = parsedParams.access_token || null;

validateAccessToken()
.then((response) => {
setUserName(response.data.name || '');
isTokenAuthenticated(true);
getOrganisation(response.data.id)
.then((response) => {
setOrganisationId(response.data.id);
})
.catch((error) => {
console.error('Error fetching project:', error);
});
if(typeof window !== 'undefined' && access_token) {
localStorage.setItem('accessToken', access_token);
refreshUrl();
}

validateAccessToken()
.then((response) => {
setUserName(response.data.name || '');
isTokenAuthenticated(true);
fetchOrganisation(response.data.id);
})
.catch((error) => {
console.error('Error validating access token:', error);
});
}
})
.catch((error) => {
console.error('Error validating access token:', error);
console.error('Error fetching project:', error);
});
}, []);

Expand Down Expand Up @@ -78,7 +108,7 @@ export default function App() {
</div>
<div className="workSpaceStyle">
<div className="topBarStyle">
<TopBar selectedProject={selectedProject} userName={userName}/>
<TopBar selectedProject={selectedProject} userName={userName} env={env}/>
</div>
<div className="contentStyle">
<Content selectedView={selectedView} selectedProjectId={selectedProject?.id || ''}/>
Expand Down
8 changes: 8 additions & 0 deletions gui/pages/api/DashboardService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export const getOrganisation = (userId) => {
return api.get(`/organisations/get/user/${userId}`);
};

export const addUser = (userData) => {
return api.post(`/users/add`, userData);
};

export const getProject = (organisationId) => {
return api.get(`/projects/get/organisation/${organisationId}`);
};
Expand Down Expand Up @@ -58,4 +62,8 @@ export const uploadFile = (agentId, formData) => {

export const validateAccessToken = () => {
return api.get(`/validate-access-token`);
}

export const checkEnvironment = () => {
return api.get(`/configs/get/env`);
}
2 changes: 1 addition & 1 deletion gui/pages/api/apiConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios';

const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID || 'eaaf029abe1165e23c1e';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8001';
// const API_BASE_URL = 'http://192.168.1.200:8001';
// const API_BASE_URL = 'http://192.168.255.48:8001';

export const baseUrl = () => {
return API_BASE_URL;
Expand Down

0 comments on commit 1cc67c6

Please sign in to comment.