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 routing to dashboard and create a basic Auth page #2

Closed
wants to merge 7 commits into from
Closed
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
59 changes: 58 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"react-app-polyfill": "^3.0.0",
"react-dom": "^18.2.0",
"react-refresh": "^0.11.0",
"react-router-dom": "^6.3.0",
"resolve": "^1.20.0",
"resolve-url-loader": "^4.0.0",
"sass-loader": "^12.3.0",
Expand Down
50 changes: 50 additions & 0 deletions src/components/pages/Auth/Auth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (c) 2022, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

import React, { useCallback, useEffect, useState } from "react";
import { Navigate } from "react-router-dom";

import { clearValueInStorage, setValueToStorage } from "../../../storageService";
import { apiKeyLocalStorageKey } from "../../../constants";

const Auth: React.FC = () => {
const [isAuthenticated, setIsAuthenticated] = useState(false);

const promptForApiKey = useCallback(() => {
clearValueInStorage(apiKeyLocalStorageKey);

const apiKey = prompt("Please enter your API key");

if (apiKey !== null && apiKey.length !== 0) {
// TODO: verify the api key from backend before saving to localstorage
setValueToStorage(apiKeyLocalStorageKey, apiKey);
setIsAuthenticated(true);
} else {
promptForApiKey();
}
}, []);

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

if (isAuthenticated) {
return <Navigate to="/home" />
Copy link
Contributor

Choose a reason for hiding this comment

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

Just use window API please

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

}

return <></>
};

export default Auth;
9 changes: 9 additions & 0 deletions src/components/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

const Home = () => {
return (
<h1>Dashboard home</h1>
);
};

export default Home;
43 changes: 43 additions & 0 deletions src/components/utils/ProtectedComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Copyright (c) 2022, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

import React, { useEffect, useState } from "react";

import { Navigate } from "react-router-dom";

import { apiKeyLocalStorageKey } from "../../constants";
import { getValueFromStorage } from "../../storageService";

type PropTypes = React.PropsWithChildren;

const ProtectedComponent: React.FC<PropTypes> = (props) => {
const [isAuthenticated, setIsAuthenticated] = useState(true);

useEffect(() => {
const apiKeyInStorage = getValueFromStorage(apiKeyLocalStorageKey);

if (apiKeyInStorage === null || apiKeyInStorage.length === 0) {
setIsAuthenticated(false);
}
}, []);

if (!isAuthenticated) {
return <Navigate to="/" />
}

return <>{props.children}</>;
}

export default ProtectedComponent;
16 changes: 16 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Copyright (c) 2022, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

export const apiKeyLocalStorageKey = "dashboard-api-key";
25 changes: 22 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";

import Auth from "./components/pages/Auth/Auth";
import Home from './components/pages/Home/Home';
import { getDashboardAppPath } from './utils';
import ProtectedComponent from './components/utils/ProtectedComponent';

import './index.css';

const dashboardAppPath = getDashboardAppPath();

const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);

root.render(
<React.StrictMode>
<App />
<BrowserRouter basename={dashboardAppPath}>
<Routes>
<Route path="/" element={<Auth />} />
<Route path="/home" element={
<ProtectedComponent>
<Home />
</ProtectedComponent>
} />
<Route path="*" element={<Navigate to="/home" />} />
</Routes>
</BrowserRouter>
</React.StrictMode>
);

Expand Down
28 changes: 28 additions & 0 deletions src/storageService/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright (c) 2022, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

export const getValueFromStorage = (key: string): string | null => {
const value = localStorage.getItem(key);
return value;
}

export const setValueToStorage = (key: string, value: string): string => {
localStorage.setItem(key, value);
return value;
}

export const clearValueInStorage = (key: string) => {
localStorage.removeItem(key);
}
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ export function getStaticBasePath(): string {

export function getImageUrl(imageName: string): string {
return getStaticBasePath() + "/media/" + imageName;
}
}

export function getDashboardAppPath(): string {
return (window as any).dashboardAppPath;
}