authorizer/app/src/Root.tsx

130 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-12-28 22:46:31 +00:00
import React, { useEffect, lazy, Suspense } from 'react';
import { Switch, Route } from 'react-router-dom';
import { useAuthorizer } from '@authorizerdev/authorizer-react';
import styled, { ThemeProvider } from 'styled-components';
import SetupPassword from './pages/setup-password';
import { hasWindow, createRandomString } from './utils/common';
import { theme } from './theme';
2021-12-28 22:46:31 +00:00
const ResetPassword = lazy(() => import('./pages/rest-password'));
const Login = lazy(() => import('./pages/login'));
const Dashboard = lazy(() => import('./pages/dashboard'));
const SignUp = lazy(() => import('./pages/signup'));
const Wrapper = styled.div`
font-family: ${(props) => props.theme.fonts.fontStack};
color: ${(props) => props.theme.colors.textColor};
font-size: ${(props) => props.theme.fonts.mediumText};
box-sizing: border-box;
*,
*:before,
*:after {
box-sizing: inherit;
}
`;
2022-03-09 04:40:39 +00:00
export default function Root({
globalState,
}: {
globalState: Record<string, string>;
}) {
2021-08-06 13:47:52 +00:00
const { token, loading, config } = useAuthorizer();
const searchParams = new URLSearchParams(
2022-10-02 17:09:47 +00:00
hasWindow() ? window.location.search : ``,
);
const state = searchParams.get('state') || createRandomString();
const scope = searchParams.get('scope')
? searchParams.get('scope')?.toString().split(' ')
: ['openid', 'profile', 'email'];
2022-11-12 18:24:37 +00:00
const code = searchParams.get('code') || '';
const nonce = searchParams.get('nonce') || '';
const urlProps: Record<string, any> = {
state,
scope,
};
const redirectURL =
searchParams.get('redirect_uri') || searchParams.get('redirectURL');
if (redirectURL) {
urlProps.redirectURL = redirectURL;
} else {
urlProps.redirectURL = hasWindow() ? window.location.origin : redirectURL;
}
urlProps.redirect_uri = urlProps.redirectURL;
2021-08-06 13:47:52 +00:00
useEffect(() => {
2021-08-09 03:16:07 +00:00
if (token) {
let redirectURL = config.redirectURL || '/app';
2022-10-19 17:47:13 +00:00
let params = `access_token=${token.access_token}&id_token=${token.id_token}&expires_in=${token.expires_in}&state=${globalState.state}`;
2022-10-19 13:34:15 +00:00
if (code !== '') {
2022-11-12 18:24:37 +00:00
params += `&code=${code}`;
2022-10-19 13:34:15 +00:00
}
2022-10-19 17:47:13 +00:00
2022-10-19 18:44:06 +00:00
if (nonce !== '') {
2022-11-12 18:24:37 +00:00
params += `&nonce=${nonce}`;
2022-10-19 18:44:06 +00:00
}
2022-03-09 04:40:39 +00:00
if (token.refresh_token) {
params += `&refresh_token=${token.refresh_token}`;
}
2022-10-19 13:34:15 +00:00
const url = new URL(redirectURL);
if (redirectURL.includes('?')) {
redirectURL = `${redirectURL}&${params}`;
} else {
redirectURL = `${redirectURL}?${params}`;
}
2021-08-09 03:16:07 +00:00
if (url.origin !== window.location.origin) {
2022-03-07 18:14:19 +00:00
sessionStorage.removeItem('authorizer_state');
window.location.replace(redirectURL);
2021-08-09 03:16:07 +00:00
}
2021-08-06 13:47:52 +00:00
}
return () => {};
2022-11-12 18:24:37 +00:00
}, [token, config]);
2021-08-06 13:47:52 +00:00
if (loading) {
return <h1>Loading...</h1>;
}
2021-08-06 13:47:52 +00:00
if (token) {
return (
2021-12-28 22:46:31 +00:00
<Suspense fallback={<></>}>
<Switch>
<Route path="/app" exact>
<Dashboard />
</Route>
</Switch>
</Suspense>
2021-08-06 13:47:52 +00:00
);
}
return (
2021-12-28 22:46:31 +00:00
<Suspense fallback={<></>}>
<ThemeProvider theme={theme}>
<Wrapper>
<Switch>
<Route path="/app" exact>
<Login urlProps={urlProps} />
</Route>
<Route path="/app/signup">
<SignUp urlProps={urlProps} />
</Route>
<Route path="/app/reset-password">
<ResetPassword />
</Route>
<Route path="/app/setup-password">
<SetupPassword />
</Route>
</Switch>
</Wrapper>
</ThemeProvider>
2021-12-28 22:46:31 +00:00
</Suspense>
2021-08-06 13:47:52 +00:00
);
}