Skip to content

Commit 16f3a04

Browse files
author
sw-yx
committed
fix readme
1 parent c89057c commit 16f3a04

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed

README.md

+46-40
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
[![NPM](https://img.shields.io/npm/v/react-netlify-identity.svg)](https://www.npmjs.com/package/react-netlify-identity) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
66

7-
Use [Netlify Identity](https://www.netlify.com/docs/identity/) easier with React! This is a thin wrapper over the [gotrue-js](https://github.com/netlify/gotrue-js) library for easily accessing Netlify Identity functionality in your app, with React Hooks. Types are provided.
7+
Use [Netlify Identity](https://www.netlify.com/docs/identity/) easier with React! This is a thin wrapper over the [gotrue-js](https://github.com/netlify/gotrue-js) library for easily accessing Netlify Identity functionality in your app, with React Context and Hooks. Types are provided.
88

99
Two demos:
1010

@@ -35,9 +35,9 @@ yarn add react-netlify-identity
3535

3636
## Usage
3737

38-
⚠️ **Important:** You will need to have an active Netlify site running with Netlify Identity turned on. [Click here for instructions](https://www.netlify.com/docs/identity/#getting-started) to get started/double check that it is on. We will need your site's url (e.g. `https://mysite.netlify.com`) to initialize `useNetlifyIdentity`.
38+
⚠️ **Important:** You will need to have an active Netlify site running with Netlify Identity turned on. [Click here for instructions](https://www.netlify.com/docs/identity/#getting-started) to get started/double check that it is on. We will need your site's url (e.g. `https://mysite.netlify.com`) to initialize `IdentityContextProvider`.
3939

40-
**As a React Hook**, you can destructure these variables and methods:
40+
**When you call useIdentityCtx()**, you can destructure these variables and methods:
4141

4242
- `user: User`
4343
- `setUser`: directly set the user object. Not advised; use carefully!! mostly you should use the methods below
@@ -53,14 +53,17 @@ yarn add react-netlify-identity
5353
- `authedFetch(endpoint: string, obj = {})` (a thin axios-like wrapper over `fetch` that has the user's JWT attached, for convenience pinging Netlify Functions with Netlify Identity)
5454

5555
```tsx
56-
import * as React from "react"
56+
import React from 'react';
5757

58-
import { useNetlifyIdentity } from "react-netlify-identity"
58+
import { IdentityContextProvider } from 'react-netlify-identity';
5959

60-
const IdentityContext = React.createContext() // not necessary but recommended
6160
function App() {
62-
const identity = useNetlifyIdentity(url) // supply the url of your Netlify site instance. VERY IMPORTANT
63-
return <IdentityContext.Provider value={identity}>{/* rest of your app */}</IdentityContext.Provider>
61+
const url = process.env.NETLIFY_URL; // supply the url of your Netlify site instance. VERY IMPORTANT
62+
return (
63+
<IdentityContextProvider url={url}>
64+
{/* rest of your app */}
65+
</IdentityContextProvider>
66+
);
6467
}
6568
```
6669

@@ -74,32 +77,32 @@ Click for More Example code
7477
```tsx
7578
// log in/sign up example
7679
function Login() {
77-
const { loginUser, signupUser } = React.useContext(IdentityContext)
78-
const formRef = React.useRef()
79-
const [msg, setMsg] = React.useState("")
80+
const { loginUser, signupUser } = useIdentityCtx();
81+
const formRef = React.useRef();
82+
const [msg, setMsg] = React.useState('');
8083
const signup = () => {
81-
const email = formRef.current.email.value
82-
const password = formRef.current.password.value
84+
const email = formRef.current.email.value;
85+
const password = formRef.current.password.value;
8386
signupUser(email, password)
84-
.then((user) => {
85-
console.log("Success! Signed up", user)
86-
navigate("/dashboard")
87+
.then(user => {
88+
console.log('Success! Signed up', user);
89+
navigate('/dashboard');
8790
})
88-
.catch((err) => console.error(err) || setMsg("Error: " + err.message))
89-
}
91+
.catch(err => console.error(err) || setMsg('Error: ' + err.message));
92+
};
9093
return (
9194
<form
9295
ref={formRef}
93-
onSubmit={(e) => {
94-
e.preventDefault()
95-
const email = e.target.email.value
96-
const password = e.target.password.value
96+
onSubmit={e => {
97+
e.preventDefault();
98+
const email = e.target.email.value;
99+
const password = e.target.password.value;
97100
load(loginUser(email, password, true))
98-
.then((user) => {
99-
console.log("Success! Logged in", user)
100-
navigate("/dashboard")
101+
.then(user => {
102+
console.log('Success! Logged in', user);
103+
navigate('/dashboard');
101104
})
102-
.catch((err) => console.error(err) || setMsg("Error: " + err.message))
105+
.catch(err => console.error(err) || setMsg('Error: ' + err.message));
103106
}}
104107
>
105108
<div>
@@ -120,55 +123,58 @@ function Login() {
120123
{msg && <pre>{msg}</pre>}
121124
</div>
122125
</form>
123-
)
126+
);
124127
}
125128

126129
// log out user
127130
function Logout() {
128-
const { logoutUser } = React.useContext(IdentityContext)
129-
return <button onClick={logoutUser}>You are signed in. Log Out</button>
131+
const { logoutUser } = useIdentityCtx();
132+
return <button onClick={logoutUser}>You are signed in. Log Out</button>;
130133
}
131134

132135
// check `identity.user` in a protected route
133136
function PrivateRoute(props) {
134-
const identity = React.useContext(IdentityContext)
135-
let { as: Comp, ...rest } = props
137+
const identity = useIdentityCtx();
138+
let { as: Comp, ...rest } = props;
136139
return identity.user ? (
137140
<Comp {...rest} />
138141
) : (
139142
<div>
140143
<h3>You are trying to view a protected page. Please log in</h3>
141144
<Login />
142145
</div>
143-
)
146+
);
144147
}
145148

146149
// check if user has confirmed their email
147150
// use authedFetch API to make a request to Netlify Function with the user's JWT token,
148151
// letting your function use the `user` object
149152
function Dashboard() {
150-
const { isConfirmedUser, authedFetch } = React.useContext(IdentityContext)
151-
const [msg, setMsg] = React.useState("Click to load something")
153+
const { isConfirmedUser, authedFetch } = useIdentityCtx();
154+
const [msg, setMsg] = React.useState('Click to load something');
152155
const handler = () => {
153-
authedFetch.get("/.netlify/functions/authEndPoint").then(setMsg)
154-
}
156+
authedFetch.get('/.netlify/functions/authEndPoint').then(setMsg);
157+
};
155158
return (
156159
<div>
157160
<h3>This is a Protected Dashboard!</h3>
158161
{!isConfirmedUser && (
159-
<pre style={{ backgroundColor: "papayawhip" }}>
160-
You have not confirmed your email. Please confirm it before you ping the API.
162+
<pre style={{ backgroundColor: 'papayawhip' }}>
163+
You have not confirmed your email. Please confirm it before you ping
164+
the API.
161165
</pre>
162166
)}
163167
<hr />
164168
<div>
165169
<p>You can try pinging our authenticated API here.</p>
166-
<p>If you are logged in, you should be able to see a `user` info here.</p>
170+
<p>
171+
If you are logged in, you should be able to see a `user` info here.
172+
</p>
167173
<button onClick={handler}>Ping authenticated API</button>
168174
<pre>{JSON.stringify(msg, null, 2)}</pre>
169175
</div>
170176
</div>
171-
)
177+
);
172178
}
173179
```
174180

0 commit comments

Comments
 (0)