Skip to content

Commit 986dc1e

Browse files
author
sw-yx
committed
update api after r-n-i api change
1 parent ef0e19b commit 986dc1e

File tree

10 files changed

+767
-112
lines changed

10 files changed

+767
-112
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const IdentityModal = React.lazy(() => import("react-netlify-identity-widget"))
2929

3030
function App() {
3131
const [dialog, setDialog] = React.useState(false)
32-
const identity = useNetlifyIdentity("https://netlify-gotrue-in-react.netlify.com")
32+
const url = process.env.REACT_APP_NETLIFY_URL // supply the url of your Netlify site instance. VERY IMPORTANT
3333
const name =
3434
(identity && identity.user && identity.user.user_metadata && identity.user.user_metadata.name) || "NoName"
3535
return (

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"react-dom": "^15.0.0 || ^16.0.0"
3838
},
3939
"dependencies": {
40-
"react-netlify-identity": "^0.0.16"
40+
"react-netlify-identity": "^0.1.2"
4141
},
4242
"config": {
4343
"commitizen": {

src/app.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react"
22
import { Login } from "./components/login"
33
import { Logout } from "./components/logout"
44
import { Signup } from "./components/signup"
5-
import { useIdentityContext } from "./context"
5+
import { useIdentityCtx } from "react-netlify-identity"
66
import { Tabs, TabList, Tab, TabPanels, TabPanel } from "@reach/tabs"
77

88
import { Providers } from "./components/providers"
@@ -33,8 +33,8 @@ function LoggedInScreen() {
3333
return <Logout />
3434
}
3535

36-
function Gate({ }: { onCloseDialog: Function }) {
37-
const identity = useIdentityContext()
36+
function Gate({ }: { onCloseDialog: Function }) {
37+
const identity = useIdentityCtx()
3838
const isLoggedIn = Boolean(identity && identity.user)
3939
return isLoggedIn ? <LoggedInScreen /> : <LoggedOutScreen />
4040
}

src/components/login.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from "react"
2-
import { useIdentityContext } from "../context"
2+
import { useIdentityCtx } from "react-netlify-identity"
33
import useLoading from "../useLoading"
44
import VisuallyHidden from "@reach/visually-hidden"
55

66
export function Login() {
7-
const { loginUser } = useIdentityContext()
7+
const { loginUser } = useIdentityCtx()
88
const formRef = React.useRef<HTMLFormElement>(null)
99
const [msg, setMsg] = React.useState("")
1010
const [isLoading, load] = useLoading()
@@ -30,11 +30,11 @@ export function Login() {
3030
const email = target.email.value
3131
const password = target.password.value
3232
load(loginUser(email, password, true))
33-
.then(user => {
33+
.then((user) => {
3434
console.log("Success! Logged in", user)
3535
// navigate("/dashboard")
3636
})
37-
.catch(err => void console.error(err) || setMsg("Error: " + err.message))
37+
.catch((err) => void console.error(err) || setMsg("Error: " + err.message))
3838
}}
3939
>
4040
<div className="formGroup" key="email">

src/components/logout.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from "react"
2-
import { useIdentityContext } from "../context"
2+
import { useIdentityCtx } from "react-netlify-identity"
33
import useLoading from "../useLoading"
44

55
export function Logout() {
6-
const identity = useIdentityContext()
6+
const identity = useIdentityCtx()
77
const name =
88
(identity && identity.user && identity.user.user_metadata && identity.user.user_metadata.full_name) || "NoName"
9-
9+
1010
const [isLoading, load] = useLoading()
1111
const logout = () => load(identity.logoutUser())
1212
return (

src/components/providers.tsx

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import React from "react"
2-
import { useIdentityContext, SettingContext } from "../context"
3-
import { Settings } from "react-netlify-identity"
2+
import { Settings, useIdentityCtx } from "react-netlify-identity"
43

54
export function Providers() {
6-
const setting = React.useContext(SettingContext)
7-
const hasProviders =
8-
setting &&
9-
setting.external &&
10-
Object.entries(setting.external).some(([k, v]) => ["github", "gitlab", "bitbucket", "google"].includes(k) && v)
5+
const { settings } = useIdentityCtx()
6+
const hasProviders = Object.entries(settings.external).some(
7+
([k, v]) => ["github", "gitlab", "bitbucket", "google"].includes(k) && v
8+
)
119
if (!hasProviders) return null
1210
let isLocalhost = false
1311
if (typeof window !== "undefined") {
@@ -25,22 +23,21 @@ export function Providers() {
2523
<pre>⚠️Testing providers on localhost won't work because OAuth redirects to your production site</pre>
2624
)}
2725
<hr className="hr" />
28-
<ProviderButton setting={setting} provider="Google" />
29-
<ProviderButton setting={setting} provider="GitHub" />
30-
<ProviderButton setting={setting} provider="GitLab" />
31-
<ProviderButton setting={setting} provider="Bitbucket" />
26+
<ProviderButton settings={settings} provider="Google" />
27+
<ProviderButton settings={settings} provider="GitHub" />
28+
<ProviderButton settings={settings} provider="GitLab" />
29+
<ProviderButton settings={settings} provider="Bitbucket" />
3230
</div>
3331
)
3432
}
3533

3634
interface Dict<T> {
3735
[id: string]: T
3836
}
39-
function ProviderButton({ setting, provider }: { setting: Settings | null; provider: string }) {
40-
if (!setting) return null
41-
const ext = setting.external as Dict<{}>
37+
function ProviderButton({ settings, provider }: { settings: Settings; provider: string }) {
38+
const ext = settings.external as Dict<{}>
4239
if (!ext[provider.toLowerCase()]) return null
43-
const { loginProvider } = useIdentityContext()
40+
const { loginProvider } = useIdentityCtx()
4441
const click = () => loginProvider(provider.toLowerCase() as "github" | "gitlab" | "bitbucket" | "google")
4542
return (
4643
<button onClick={click} className={`provider${provider} btn btnProvider`}>

src/components/signup.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from "react"
2-
import { useIdentityContext } from "../context"
2+
import { useIdentityCtx } from "react-netlify-identity"
33
import useLoading from "../useLoading"
44
import VisuallyHidden from "@reach/visually-hidden"
55

66
export function Signup() {
7-
const { signupUser } = useIdentityContext()
7+
const { signupUser } = useIdentityCtx()
88
const formRef = React.useRef<HTMLFormElement>(null)
99
const [msg, setMsg] = React.useState("")
1010
const [isLoading, load] = useLoading()
@@ -15,11 +15,11 @@ export function Signup() {
1515
const password = formRef.current.password.value
1616
const data = { signupSource: "react-netlify-identity-widget", full_name }
1717
load(signupUser(email, password, data))
18-
.then(user => {
18+
.then((user) => {
1919
console.log("Success! Signed up", user)
2020
// navigate("/dashboard")
2121
})
22-
.catch(err => void console.error(err) || setMsg("Error: " + err.message))
22+
.catch((err) => void console.error(err) || setMsg("Error: " + err.message))
2323
}
2424
return (
2525
<form

src/context.tsx

-33
This file was deleted.

src/index.tsx

+26-40
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import VisuallyHidden from "@reach/visually-hidden"
77

88
import { Widget } from "./app"
99

10-
import { FormStateContextProvider, SettingContext } from "./context"
1110
import { ErrorBoundary } from "./errorBoundary"
1211

13-
import { Settings, useNetlifyIdentity as _useNetlifyIdentity } from "react-netlify-identity"
12+
import {
13+
IdentityContextProvider as _IdentityContextProvider,
14+
useIdentityCtx as _useIdentityCtx
15+
} from "react-netlify-identity"
16+
export { User, Settings, ReactNetlifyIdentityAPI, useNetlifyIdentity } from "react-netlify-identity"
1417

1518
/** URL of your Netlify Instance with Identity enabled e.g. https://netlify-gotrue-in-react.netlify.com */
1619

@@ -21,46 +24,29 @@ type ModalProps = {
2124
onCloseDialog: () => void
2225
}
2326

24-
import {
25-
IdentityContextProvider as _IdentityContextProvider,
26-
useIdentityContext as _useIdentityContext,
27-
IdentityContext as _IdentityContext
28-
} from "./context"
29-
3027
export const IdentityContextProvider = _IdentityContextProvider
31-
export const useNetlifyIdentity = _useNetlifyIdentity
32-
export const IdentityContext = _IdentityContext
33-
export const useIdentityContext = _useIdentityContext
28+
export const useIdentityContext = _useIdentityCtx
3429
export function IdentityModal({ showDialog, onCloseDialog }: ModalProps) {
35-
const { settings } = useIdentityContext()
36-
const [setting, setSetting] = React.useState<Settings | null>(null)
37-
React.useEffect(() => {
38-
settings().then(x => setSetting(x))
39-
}, [settings])
4030
return (
41-
<SettingContext.Provider value={setting}>
42-
<Dialog
43-
isOpen={showDialog}
44-
onDismiss={() => void console.log("hi") || onCloseDialog()}
45-
style={{
46-
border: "solid 5px hsla(0, 0%, 0%, 0.5)",
47-
borderRadius: "10px",
48-
position: "relative",
49-
maxWidth: 400
50-
}}
51-
>
52-
<button className="btn btnClose" onClick={onCloseDialog}>
53-
<VisuallyHidden>Close</VisuallyHidden>
54-
</button>
55-
<FormStateContextProvider>
56-
<ErrorBoundary>
57-
<React.Suspense fallback={<div>Loading...</div>}>
58-
<Widget onCloseDialog={onCloseDialog} />
59-
</React.Suspense>
60-
</ErrorBoundary>
61-
</FormStateContextProvider>
62-
</Dialog>
63-
</SettingContext.Provider>
31+
<Dialog
32+
isOpen={showDialog}
33+
onDismiss={onCloseDialog}
34+
style={{
35+
border: "solid 5px hsla(0, 0%, 0%, 0.5)",
36+
borderRadius: "10px",
37+
position: "relative",
38+
maxWidth: 400
39+
}}
40+
>
41+
<button className="btn btnClose" onClick={onCloseDialog}>
42+
<VisuallyHidden>Close</VisuallyHidden>
43+
</button>
44+
<ErrorBoundary>
45+
<React.Suspense fallback={<div>Loading...</div>}>
46+
<Widget onCloseDialog={onCloseDialog} />
47+
</React.Suspense>
48+
</ErrorBoundary>
49+
</Dialog>
6450
)
6551
}
66-
export default IdentityModal
52+
export default IdentityModal

0 commit comments

Comments
 (0)