Skip to content

Commit abdcdad

Browse files
committedJan 16, 2020
fix(readme): update to latest changes
1 parent 226aea3 commit abdcdad

File tree

1 file changed

+87
-7
lines changed

1 file changed

+87
-7
lines changed
 

‎README.md

+87-7
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ Three demos:
1212
- [example with Reach Router](https://github.com/sw-yx/react-netlify-identity/tree/master/examples/example-reach-router) with [a demo hosted here](https://react-netlify-identity.netlify.com) and [deployed logs here](https://app.netlify.com/sites/react-netlify-identity/deploys)
1313
- [example with React Router](https://github.com/sw-yx/react-netlify-identity/tree/master/examples/example-react-router) with [a demo hosted here](https://react-netlify-identity-example.netlify.com)
1414

15-
1615
**This library is not officially maintained by Netlify.** This is written by swyx for his own use (and others with like minds 😎) and will be maintained as a personal project unless formally adopted by Netlify. See below for official alternatives.
1716

1817
## Blogposts
1918

2019
- [Add Netlify Identity Authentication to any React App in 5 minutes with React Context, Hooks and Suspense](https://dev.to/swyx/add-netlify-identity-authentication-to-any-react-app-in-5-minutes-with-react-context-hooks-and-suspense-5gci)
2120

22-
2321
## List of Alternatives
2422

2523
**Lowest level JS Library**: If you want to use the official Javascript bindings to GoTrue, Netlify's underlying Identity service written in Go, use https://github.com/netlify/gotrue-js
@@ -51,13 +49,19 @@ yarn add react-netlify-identity
5149
- `isConfirmedUser: boolean`: if they have confirmed their email
5250
- `isLoggedIn: boolean`: if the user is logged in
5351
- `signupUser(email: string, password: string, data: Object)`
54-
- `loginUser(email: string, password: string, remember: Boolean)` - we default the `remember` term to `true` since you'll usually want to remember the session in localStorage. set it to false if you need to
52+
- `loginUser(email: string, password: string, remember: boolean = true)` - we default the `remember` term to `true` since you'll usually want to remember the session in localStorage. set it to false if you need to
5553
- `logoutUser()`
5654
- `requestPasswordRecovery(email: string)`
57-
- `recoverAccount(token: string, remember?: boolean | undefined)`
5855
- `updateUser(fields: { data: object })`
5956
- `getFreshJWT()`
60-
- `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)
57+
- `authedFetch(endpoint: string, obj: RequestInit = {})` a thin axios-like wrapper over `fetch` that has the user's JWT attached, for convenience pinging Netlify Functions with Netlify Identity
58+
- `param: TokenParam`
59+
- a token exposing Netlify tokens a dev has to implement the actions for; namely `invite`, `recovery`, `email_change` and `access_denied`
60+
- for further reference, please check the [type definition](https://github.com/sw-yx/react-netlify-identity/tree/master/src/token.ts)
61+
- implementation for a Recovery Example below
62+
- **Important:** tokens this package exposes no methods for are automatically handled and will not be passed down - see [runRoutes implementation](https://github.com/sw-yx/react-netlify-identity/master/src/runRoutes.tsx)
63+
- if you don't want this behaviour (added [here](https://github.com/sw-yx/react-netlify-identity/issues/12) in v.0.1.8), pass `runRoutes={false}` to the exposed hook
64+
- `recoverAccount(remember?: boolean)`:
6165

6266
```tsx
6367
import React from 'react';
@@ -89,16 +93,19 @@ function Login() {
8993
const { loginUser, signupUser } = useIdentityContext();
9094
const formRef = React.useRef();
9195
const [msg, setMsg] = React.useState('');
96+
9297
const signup = () => {
9398
const email = formRef.current.email.value;
9499
const password = formRef.current.password.value;
100+
95101
signupUser(email, password)
96102
.then(user => {
97103
console.log('Success! Signed up', user);
98104
navigate('/dashboard');
99105
})
100106
.catch(err => console.error(err) || setMsg('Error: ' + err.message));
101107
};
108+
102109
return (
103110
<form
104111
ref={formRef}
@@ -189,6 +196,81 @@ function Dashboard() {
189196

190197
</details>
191198

199+
<details>
200+
<summary>
201+
<h3>
202+
How to handle a Recovery Action (since v0.2)
203+
</h3>
204+
</summary>
205+
206+
Of course you can alternatively inline this logic into app.
207+
208+
```tsx
209+
import { useIdentityContext } from 'react-netlify-identity';
210+
import {
211+
BrowserRouter as Router,
212+
Switch,
213+
Route,
214+
useLocation,
215+
useHistory,
216+
} from 'react-router-dom';
217+
218+
export default function App() {
219+
const { isLoggedIn } = useIdentityContext();
220+
221+
return (
222+
<Router>
223+
<CatchNetlifyRecoveryNullComponent />
224+
<Switch>
225+
{isLoggedIn ? (
226+
<>
227+
<Route path="/dashboard" exact component={DashboardPage} />
228+
<Route component={() => <Redirect to="/dashbard" />} />
229+
</>
230+
) : (
231+
<>
232+
<Route path="/" exact component={LandingPage} />
233+
<Route path="/register" exact component={RegisterPage} />
234+
<Route path="/login" exact component={LoginPage} />
235+
{/* etc */}
236+
<Route path="/recovery" exact component={RecoveryPage} />
237+
<Route component={() => <Redirect to="/" />} />
238+
</>
239+
)}
240+
</Switch>
241+
</Router>
242+
);
243+
}
244+
245+
function CatchNetlifyRecoveryNullComponent() {
246+
const {
247+
param: { token, type },
248+
} = useIdentityContext();
249+
const { replace } = useHistory();
250+
const { pathname } = useLocation();
251+
252+
// important to check for the current pathname here because else you land
253+
// in a infinite loop
254+
if (token && type === 'recovery' && pathname === '/') {
255+
replace(`/recovery`, { token });
256+
}
257+
258+
return null;
259+
}
260+
261+
function RecoveyPage() {
262+
const {
263+
location: { state },
264+
} = useHistory();
265+
// this state _might_ not be needed, it was needed in my specific implementation
266+
const [token] = useState(state?.token);
267+
268+
return null; // do something with the token
269+
}
270+
```
271+
272+
</details>
273+
192274
## Lower level API: `useNetlifyIdentity`
193275

194276
If you'd like to handle your own context yourself, you can use this library as a hook as well:
@@ -201,8 +283,6 @@ function useNetlifyIdentity(
201283
): ReactNetlifyIdentityAPI;
202284
```
203285

204-
the library watches for and handles confirmation routes by default. If you don't like this, pass `enableRunRoutes: false`. This was added here https://github.com/sw-yx/react-netlify-identity/issues/12 in v0.1.8
205-
206286
## License
207287

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

0 commit comments

Comments
 (0)
Please sign in to comment.