Skip to content

Commit

Permalink
frontend: implement logged out redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
anuejn committed Oct 8, 2023
1 parent 8493de1 commit 3c947c4
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion frontend/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useAuthData } from './utils/auth';
import { LoadingPage } from './pages/loading';
import { PagePage } from './pages/page';
import { AboutPage } from './pages/about';
import { useGetConfig } from './api/config';

registerCopyHandler();

Expand Down Expand Up @@ -47,6 +48,26 @@ export function LoggedInRoute<T extends DefaultParams = DefaultParams>({
return <Route {...props} />;
}

export function LoggedInRedirectRoute<T extends DefaultParams = DefaultParams>({
...props
}: RouteProps<T>) {
const [_, navigate] = useLocation();
const { isLoggedIn, isLoading } = useAuthData();
const { data: config, isLoading: configLoading } = useGetConfig({});
if (isLoading) {
return <Route component={LoadingPage} />;
}
if (!isLoggedIn && !configLoading) {
if (config.logged_out_redirect_url) {
window.location.replace(config.logged_out_redirect_url);
} else {
navigate('/login');
}
return null;
}
return <Route {...props} />;
}

export function App() {
const routerBase = trimTrailingSlash(import.meta.env.BASE_URL);

Expand All @@ -59,7 +80,7 @@ export function App() {
<Route path="/page/:pageId" component={PagePage} />
<Route path="/about" component={AboutPage} />

<LoggedInRoute path="/" component={UserHomePage} />
<LoggedInRedirectRoute path="/" component={UserHomePage} />
<LoggedInRoute path="/new" component={NewDocumentPage} />

<AuthenticatedRoute path="/document/:documentId" component={DocumentPage} />
Expand Down

0 comments on commit 3c947c4

Please sign in to comment.