-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathApp.tsx
46 lines (40 loc) · 1.42 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React from 'react';
import { BrowserRouter, Redirect, Route, Switch } from 'react-router-dom';
import {
IdentityContextProvider,
useIdentityContext,
} from 'react-netlify-identity';
import { GlobalStyles } from './components';
import { CreateAccount, Home, LogIn, Welcome } from './views';
interface Props {
component: React.FunctionComponent;
exact?: boolean;
path: string;
}
const PublicRoute: React.FunctionComponent<Props> = (props: Props) => {
const { isLoggedIn } = useIdentityContext();
return isLoggedIn ? <Redirect to="/home" /> : <Route {...props} />;
};
const PrivateRoute: React.FunctionComponent<Props> = (props: Props) => {
const { isLoggedIn } = useIdentityContext();
return isLoggedIn ? <Route {...props} /> : <Redirect to="/welcome" />;
};
export const App: React.FunctionComponent = () => {
const url = 'https://react-netlify-identity-example.netlify.com';
return (
<>
<GlobalStyles />
<IdentityContextProvider url={url}>
<BrowserRouter>
<Switch>
<PublicRoute exact path="/" component={Welcome} />
<PublicRoute path="/welcome" component={Welcome} />
<PublicRoute path="/createaccount" component={CreateAccount} />
<PublicRoute path="/login" component={LogIn} />
<PrivateRoute path="/home" component={Home} />
</Switch>
</BrowserRouter>
</IdentityContextProvider>
</>
);
};