add Authenticator component

This commit is contained in:
Michael Fatemi 2021-04-10 14:26:24 -04:00
parent d5bc178e67
commit 3b62ba29eb
4 changed files with 50 additions and 2 deletions

View File

@ -16,6 +16,7 @@ import Main from './components/Main';
import 'bootstrap/dist/css/bootstrap.min.css'; import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap/dist/js/bootstrap.bundle.min'; import 'bootstrap/dist/js/bootstrap.bundle.min';
import './App.css'; import './App.css';
import Authenticator from './components/Authenticator';
function App() { function App() {
return ( return (
@ -24,6 +25,7 @@ function App() {
<Nav /> <Nav />
<Switch> <Switch>
<Route component={Main} path="/about" /> <Route component={Main} path="/about" />
<Route component={Authenticator} path="/auth/:provider/callback" />
<Route component={CreatePool} path="/create_pool" /> <Route component={CreatePool} path="/create_pool" />
<Route component={CreateGroup} path="/create_group" /> <Route component={CreateGroup} path="/create_group" />
<Route component={Groups} path="/groups" /> <Route component={Groups} path="/groups" />

View File

@ -2,4 +2,4 @@
const dev = process.env.NODE_ENV === 'development'; const dev = process.env.NODE_ENV === 'development';
export const API_ENDPOINT = 'http://localhost/api'; export const API_ENDPOINT = 'http://localhost/api';
export const ION_AUTHORIZATION_ENDPOINT = export const ION_AUTHORIZATION_ENDPOINT =
'https://ion.tjhsst.edu/oauth/authorize?response_type=code&client_id=ScL9QdZ9m3iGmHG11uznwABg4ZSkabQJan05ZYsk&scope=read&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fauth%2Fion%2Fsuccess;'; 'https://ion.tjhsst.edu/oauth/authorize?response_type=code&client_id=rNa6n9YSg8ftINdyVPpUsaMuxNbHLo9dh1OsOktR&scope=read&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fion%2Fcallback';

View File

@ -0,0 +1,44 @@
import { useEffect, useState } from 'react';
import { Redirect, useLocation, useParams } from 'react-router-dom';
import { API_ENDPOINT } from '../api';
export default function Authenticator() {
const { provider } = useParams<{ provider: string }>();
const query = new URLSearchParams(useLocation().search);
const code = query.get('code');
const [status, setStatus] = useState<'pending' | 'errored' | 'authenticated'>(
'pending'
);
useEffect(() => {
fetch(`${API_ENDPOINT}/create_session`, {
method: 'post',
body: JSON.stringify({
code,
provider,
}),
})
.then((response) => {
response.json().then((json) => {
if (json.status === 'success') {
localStorage.setItem('session_token', json.token);
setStatus('authenticated');
} else {
setStatus('errored');
}
});
})
.catch(() => {
setStatus('errored');
});
}, [code, provider]);
switch (status) {
case 'authenticated':
return <Redirect to="/" />;
case 'errored':
return <h1>Sign In Error</h1>;
case 'pending':
return <h1>Signing In</h1>;
}
}

View File

@ -1,3 +1,5 @@
import { ION_AUTHORIZATION_ENDPOINT } from '../api';
export default function Home() { export default function Home() {
return ( return (
<div <div
@ -9,7 +11,7 @@ export default function Home() {
> >
<h1>Home</h1> <h1>Home</h1>
<div style={{ display: 'flex', flexDirection: 'row' }}> <div style={{ display: 'flex', flexDirection: 'row' }}>
<a href="/">Log In with Ion</a> <a href={ION_AUTHORIZATION_ENDPOINT}>Log In with Ion</a>
</div> </div>
</div> </div>
); );