api authentiation roughed in (#18)

This commit is contained in:
Michael Quigley 2022-08-02 14:24:01 -04:00
parent 1f4c697a2a
commit 56243956da
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 47 additions and 8 deletions

View File

@ -1,19 +1,15 @@
import Login from './Login';
import Logout from './Logout';
import Version from './Version';
import * as gateway from "./api/gateway";
import {useEffect, useState} from "react";
gateway.init({
url: '/api/v1'
});
import Identities from "./Identities";
const App = () => {
const [user, setUser] = useState();
useEffect(() => {
const localUser = localStorage.getItem("user")
if(localUser) {
if (localUser) {
setUser(JSON.parse(localUser))
console.log('reloaded user', localUser)
}
@ -32,10 +28,14 @@ const App = () => {
<header className="zrok-header">
<h1>zrok</h1>
<Version/>
<Logout user={user} logout={() => { setUser(null); }}/>
<Identities user={user}/>
<Logout user={user} logout={() => {
setUser(null);
}}/>
</header>
</div>
);
}
export default App;

16
ui/src/Identities.js Normal file
View File

@ -0,0 +1,16 @@
import {useEffect} from "react";
import * as metadata from './api/metadata';
const Identities = (props) => {
useEffect(() => {
metadata.listIdentities().then((resp) => { console.log(resp); })
}, [])
return (
<div>
<h3>Identities</h3>
</div>
)
};
export default Identities;

View File

@ -2,10 +2,33 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import * as gateway from "./api/gateway";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
);
gateway.init({
url: '/api/v1',
getAuthorization
});
function getAuthorization(security) {
switch(security.id) {
case 'key': return getApiKey();
default: console.log('default');
}
}
function getApiKey() {
const localUser = JSON.parse(localStorage.getItem("user"))
if(localUser) {
console.log('getApiKey', localUser.token)
return Promise.resolve({ apiKey: localUser.token });
} else {
throw new Error("token not available");
}
}