Skip to content

Commit 013e1fd

Browse files
author
sw-yx
committed
update version
1 parent 1b83d00 commit 013e1fd

File tree

3 files changed

+64
-32
lines changed

3 files changed

+64
-32
lines changed

README.md

+5-15
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,25 @@ function Login() {
8181
const email = formRef.current.email.value
8282
const password = formRef.current.password.value
8383
signupUser(email, password)
84-
.then(user => {
84+
.then((user) => {
8585
console.log("Success! Signed up", user)
8686
navigate("/dashboard")
8787
})
88-
.catch(err => console.error(err) || setMsg("Error: " + err.message))
88+
.catch((err) => console.error(err) || setMsg("Error: " + err.message))
8989
}
9090
return (
9191
<form
9292
ref={formRef}
93-
onSubmit={e => {
93+
onSubmit={(e) => {
9494
e.preventDefault()
9595
const email = e.target.email.value
9696
const password = e.target.password.value
9797
load(loginUser(email, password, true))
98-
.then(user => {
98+
.then((user) => {
9999
console.log("Success! Logged in", user)
100100
navigate("/dashboard")
101101
})
102-
.catch(err => console.error(err) || setMsg("Error: " + err.message))
102+
.catch((err) => console.error(err) || setMsg("Error: " + err.message))
103103
}}
104104
>
105105
<div>
@@ -174,16 +174,6 @@ function Dashboard() {
174174

175175
</details>
176176

177-
**As a render prop**: This is also exported as a render prop component, `NetlifyIdentity`, but we're not quite sure if its that useful if you can already use hooks in your app:
178-
179-
```tsx
180-
<NetlifyIdentity domain="https://mydomain.netlify.com">
181-
{({ loginUser, signupUser }) => {
182-
// use it
183-
}}
184-
</NetlifyIdentity>
185-
```
186-
187177
## License
188178

189179
MIT © [sw-yx](https://github.com/sw-yx)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-netlify-identity",
3-
"version": "0.0.16",
3+
"version": "0.1.0-beta",
44
"description": "use netlify identity with react",
55
"author": "sw-yx",
66
"license": "MIT",

src/index.tsx

+58-16
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,68 @@ import { runRoutes } from "./runRoutes"
55

66
type authChangeParam = (user?: User) => string | void
77

8-
interface NIProps {
9-
children: any
10-
domain: string
11-
onAuthChange?: authChangeParam
12-
}
13-
148
export type Settings = Settings
159
export type User = User
16-
export default function NetlifyIdentity({ children, domain, onAuthChange }: NIProps) {
17-
return children(useNetlifyIdentity(domain, onAuthChange))
10+
11+
export type ReactNetlifyIdentityAPI = {
12+
user: User | undefined
13+
/** not meant for normal use! you should mostly use one of the other exported methods to update the user instance */
14+
setUser: React.Dispatch<React.SetStateAction<User | undefined>>
15+
isConfirmedUser: boolean
16+
isLoggedIn: boolean
17+
signupUser: (email: string, password: string, data: Object) => Promise<User | undefined>
18+
loginUser: (email: string, password: string, remember?: boolean) => Promise<User | undefined>
19+
logoutUser: () => Promise<User | undefined>
20+
requestPasswordRecovery: (email: string) => Promise<void>
21+
recoverAccount: (token: string, remember?: boolean | undefined) => Promise<User>
22+
updateUser: (fields: Object) => Promise<User | undefined>
23+
getFreshJWT: () => Promise<string>
24+
authedFetch: {
25+
get: (endpoint: string, obj?: {}) => Promise<any>
26+
post: (endpoint: string, obj?: {}) => Promise<any>
27+
put: (endpoint: string, obj?: {}) => Promise<any>
28+
delete: (endpoint: string, obj?: {}) => Promise<any>
29+
}
30+
_goTrueInstance: GoTrue
31+
_url: string
32+
loginProvider: (provider: "bitbucket" | "github" | "gitlab" | "google") => void
33+
acceptInviteExternalUrl: (provider: "bitbucket" | "github" | "gitlab" | "google", token: string) => string
34+
settings: () => Promise<Settings>
1835
}
19-
export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam = () => {}) {
36+
37+
export const IdentityContext = React.createContext<ReactNetlifyIdentityAPI | undefined>(undefined)
38+
export function IdentityContextProvider({
39+
url,
40+
children,
41+
onAuthChange = () => {}
42+
}: {
43+
url: string
44+
children: React.ReactNode
45+
onAuthChange: authChangeParam
46+
}) {
2047
/******** SETUP */
21-
if (!domain || !validateUrl(domain)) {
48+
if (!url || !validateUrl(url)) {
2249
// just a safety check in case a JS user tries to skip this
2350
throw new Error(
24-
"invalid netlify instance URL: " + domain + ". Please check the docs for proper usage or file an issue."
51+
"invalid netlify instance URL: " + url + ". Please check the docs for proper usage or file an issue."
2552
)
2653
}
54+
const identity = React.useMemo(() => useNetlifyIdentity(url, onAuthChange), [url, onAuthChange])
55+
return <IdentityContext.Provider value={identity}>{children}</IdentityContext.Provider>
56+
}
57+
58+
// // Deprecated for now
59+
// interface NIProps {
60+
// children: any
61+
// url: string
62+
// onAuthChange?: authChangeParam
63+
// }
64+
// export default function NetlifyIdentity({ children, url, onAuthChange }: NIProps) {
65+
// return children(useNetlifyIdentity(url, onAuthChange))
66+
// }
67+
export function useNetlifyIdentity(url: string, onAuthChange: authChangeParam = () => {}): ReactNetlifyIdentityAPI {
2768
const goTrueInstance = new GoTrue({
28-
APIUrl: `${domain}/.netlify/identity`,
69+
APIUrl: `${url}/.netlify/identity`,
2970
setCookie: true
3071
})
3172

@@ -48,7 +89,7 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
4889

4990
const loginProvider = (provider: Provider) => {
5091
const url = goTrueInstance.loginExternalUrl(provider)
51-
if (window) window.location.href = url
92+
window.location.href = url
5293
}
5394
const acceptInviteExternalUrl = (provider: Provider, token: string) =>
5495
goTrueInstance.acceptInviteExternalUrl(provider, token)
@@ -89,7 +130,7 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
89130
}
90131
}
91132
const finalObj = Object.assign(defaultObj, { method }, obj)
92-
return fetch(endpoint, finalObj).then(res =>
133+
return fetch(endpoint, finalObj).then((res) =>
93134
finalObj.headers["Content-Type"] === "application/json" ? res.json() : res
94135
)
95136
}
@@ -103,7 +144,8 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
103144
/******* hook API */
104145
return {
105146
user,
106-
setUser, // use carefully!! mostly you should use the methods below
147+
/** not meant for normal use! you should mostly use one of the other exported methods to update the user instance */
148+
setUser,
107149
isConfirmedUser: !!(user && user.confirmed_at),
108150
isLoggedIn: !!user,
109151
signupUser,
@@ -115,7 +157,7 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
115157
getFreshJWT,
116158
authedFetch,
117159
_goTrueInstance: goTrueInstance,
118-
_domain: domain,
160+
_url: url,
119161
loginProvider,
120162
acceptInviteExternalUrl,
121163
settings

0 commit comments

Comments
 (0)