|
1 | 1 | import React from "react"
|
2 | 2 | import "./App.css"
|
3 |
| -import { IdentityModal, useNetlifyIdentity, IdentityContextProvider } from "react-netlify-identity-widget" |
| 3 | +import { useIdentityContext, IdentityContextProvider } from "react-netlify-identity-widget" |
4 | 4 | import "react-netlify-identity-widget/styles.css"
|
5 | 5 |
|
| 6 | +function App() { |
| 7 | + const url = "https://react-netlify-identity-widget.netlify.com" |
| 8 | + return ( |
| 9 | + <IdentityContextProvider url={url}> |
| 10 | + <AuthStatusView /> |
| 11 | + </IdentityContextProvider> |
| 12 | + ) |
| 13 | +} |
6 | 14 |
|
| 15 | +// code split the modal til you need it! |
| 16 | +const IdentityModal = React.lazy(() => import("react-netlify-identity-widget")) |
7 | 17 |
|
8 |
| -function App() { |
| 18 | +function AuthStatusView() { |
| 19 | + const identity = useIdentityContext() |
9 | 20 | const [dialog, setDialog] = React.useState(false)
|
10 |
| - const identity = useNetlifyIdentity("https://react-netlify-identity-widget.netlify.com") |
11 | 21 | const name =
|
12 | 22 | (identity && identity.user && identity.user.user_metadata && identity.user.user_metadata.full_name) || "NoName"
|
13 | 23 | const avatar_url = identity && identity.user && identity.user.user_metadata && identity.user.user_metadata.avatar_url
|
14 | 24 | return (
|
15 |
| - <IdentityContextProvider value={identity}> |
16 |
| - <div className="App"> |
17 |
| - <header className="App-header"> |
18 |
| - {identity && identity.isLoggedIn ? ( |
19 |
| - <> |
20 |
| - <h1> hello {name}!</h1> |
21 |
| - {avatar_url && <img src={avatar_url} style={{ height: 100, borderRadius: "50%" }} />} |
22 |
| - <button |
23 |
| - className="btn" |
24 |
| - style={{ maxWidth: 400, background: "orangered" }} |
25 |
| - onClick={() => setDialog(true)} |
26 |
| - > |
27 |
| - LOG OUT |
28 |
| - </button> |
29 |
| - </> |
30 |
| - ) : ( |
31 |
| - <> |
32 |
| - <h1> hello! try logging in! </h1> |
33 |
| - <button |
34 |
| - className="btn" |
35 |
| - style={{ maxWidth: 400, background: "darkgreen" }} |
36 |
| - onClick={() => setDialog(true)} |
37 |
| - > |
38 |
| - LOG IN |
39 |
| - </button> |
40 |
| - </> |
41 |
| - )} |
| 25 | + <div className="App"> |
| 26 | + <header className="App-header"> |
| 27 | + {identity && identity.isLoggedIn ? ( |
| 28 | + <> |
| 29 | + <h1> hello {name}!</h1> |
| 30 | + {avatar_url && <img alt="user name" src={avatar_url} style={{ height: 100, borderRadius: "50%" }} />} |
| 31 | + <button className="btn" style={{ maxWidth: 400, background: "orangered" }} onClick={() => setDialog(true)}> |
| 32 | + LOG OUT |
| 33 | + </button> |
| 34 | + </> |
| 35 | + ) : ( |
| 36 | + <> |
| 37 | + <h1> hello! try logging in! </h1> |
| 38 | + <button className="btn" style={{ maxWidth: 400, background: "darkgreen" }} onClick={() => setDialog(true)}> |
| 39 | + LOG IN |
| 40 | + </button> |
| 41 | + </> |
| 42 | + )} |
| 43 | + |
| 44 | + <React.Suspense fallback="loading..."> |
42 | 45 | <IdentityModal showDialog={dialog} onCloseDialog={() => setDialog(false)} />
|
43 |
| - <h3> |
44 |
| - Or{" "} |
45 |
| - <a |
46 |
| - href="https://github.com/sw-yx/react-netlify-identity-widget" |
47 |
| - target="_blank" |
48 |
| - style={{ color: "powderblue" }} |
49 |
| - > |
50 |
| - view the source |
51 |
| - </a> |
52 |
| - </h3> |
53 |
| - </header> |
54 |
| - </div> |
55 |
| - </IdentityContextProvider> |
| 46 | + </React.Suspense> |
| 47 | + <h3> |
| 48 | + Or{" "} |
| 49 | + <a |
| 50 | + href="https://github.com/sw-yx/react-netlify-identity-widget" |
| 51 | + target="_blank" |
| 52 | + rel="noopener noreferrer" |
| 53 | + style={{ color: "powderblue" }} |
| 54 | + > |
| 55 | + view the source |
| 56 | + </a> |
| 57 | + </h3> |
| 58 | + </header> |
| 59 | + </div> |
56 | 60 | )
|
57 | 61 | }
|
58 |
| - |
59 | 62 | export default App
|
0 commit comments