Skip to content

Commit

Permalink
feat(app-auth): add PrivateRoute and initialization hook
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrieleMazzola committed Dec 26, 2020
1 parent a615b3c commit 6b864f3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
11 changes: 8 additions & 3 deletions packages/game-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import React from 'react';
import {Redirect, Route, Switch} from 'react-router-dom';
import {RoutingPath} from "@pipeline/routing";
import {PrivateRoute, RoutingPath} from "@pipeline/routing";
import { useBootstrapIsFinished } from './_shared';

function App() {

return (
const bootstrapIsFinished = useBootstrapIsFinished();

return bootstrapIsFinished ? (
<Switch>
<Route path={RoutingPath.Login} render={() => <div>Login</div>}/>
<Route path={RoutingPath.Signup} render={() => <div>Signup</div>}/>
<Route path={RoutingPath.Dashboard} render={() => <div>Dashboard</div>}/>
<PrivateRoute path={RoutingPath.Dashboard} render={() => <div>Dashboard</div>}/>
<Route path="*">
<Redirect to={RoutingPath.Signup}/>
</Route>
</Switch>
) : (
<div>LOADING ...</div>
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/game-app/src/_shared/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reducer, actions, name } from "./slice";
import { reducer, actions, name, selectors } from "./slice";
import saga from "./saga";

export { reducer, actions, name, saga };
export { reducer, actions, name, saga, selectors };
12 changes: 11 additions & 1 deletion packages/game-app/src/_shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import CONFIG from "@pipeline/app-config";
import firebase from "firebase/app";
import "firebase/analytics";
import { initializeI18n } from "./i18n";
import { actions as authActions } from "./auth";
import { actions as authActions, selectors as authSelectors } from "./auth";
import { useSelector } from "react-redux";

export function bootstrap() {
initializeI18n();
Expand All @@ -26,3 +27,12 @@ export function bootstrap() {

return store;
}

/**
* A hook that can be used to ensure that the asynchronous tasks of the initialization
* process are concluded.
*/
export function useBootstrapIsFinished() {
const isAuthInitialized = useSelector(authSelectors.isInitialized);
return isAuthInitialized;
}
24 changes: 24 additions & 0 deletions packages/game-app/src/_shared/routing/PrivateRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { selectors as authSelectors } from '@pipeline/auth';
import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { RoutingPath } from '@pipeline/routing';

type Props = React.ComponentProps<typeof Route>;

/**
* A Route which is aware of the authentication status of the current user.
* If the user is not authenticated, it redirects to the SignUp Route.
* If the user is authenticated, it behaves like a standard Route component.
*/
const PrivateRoute: React.FC<Props> = (props) => {

const currentUser = useSelector(authSelectors.getCurrentUser);

return currentUser ? <Route {...props}/> : <Redirect to={RoutingPath.Signup}/>
}


PrivateRoute.displayName = 'PrivateRoute';

export default PrivateRoute;
5 changes: 2 additions & 3 deletions packages/game-app/src/_shared/routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { RoutingPath } from "./routingPath";
import PrivateRoute from "./PrivateRoute";

export {
RoutingPath
}
export { RoutingPath, PrivateRoute };

0 comments on commit 6b864f3

Please sign in to comment.