Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 1ba11f8

Browse files
committed
README: Explain how to use Netlify Identity with next-on-netlify
Add instructions, details, and an example for using Netlify Identity with next-on-netlify.
1 parent 356046e commit 1ba11f8

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The plugin can be found on [npm here](https://www.npmjs.com/package/@netlify/plu
4747
- [Preview Locally](#preview-locally)
4848
- [Custom Netlify Redirects](#custom-netlify-redirects)
4949
- [Custom Netlify Functions](#custom-netlify-functions)
50+
- [Using Netlify Identity](#using-netlify-identity)
5051
- [Caveats](#caveats)
5152
- [Fallbacks for Pages with `getStaticPaths`](#fallbacks-for-pages-with-getstaticpaths)
5253
- [Credits](#credits)
@@ -199,6 +200,52 @@ Currently, there is no support for redirects set in your `netlify.toml` file.
199200
`next-on-netlify` creates one Netlify Function for each of your
200201
SSR pages and API endpoints. It is currently not possible to create custom Netlify Functions. This feature is on our list to do.
201202

203+
#### Using Netlify Identity
204+
205+
You can use [Netlify Identity](https://docs.netlify.com/visitor-access/identity/) with `next-on-netlify`. For all pages with server-side rendering (getInitialProps*, getServerSideProps, and API routes), you can access the [clientContext object](https://docs.netlify.com/functions/functions-and-identity/#access-identity-info-via-clientcontext) via the `req` parameter.
206+
207+
For example:
208+
209+
```js
210+
const Page = () => <p>Hello World!</p>;
211+
212+
export const getServerSideProps = async ({ req }) => {
213+
// Get event and context from Netlify Function
214+
const {
215+
netlifyFunction: { event, context },
216+
} = req;
217+
218+
// Access Netlify identity
219+
const { identity, user } = context.clientContext;
220+
221+
return {
222+
props: {},
223+
};
224+
};
225+
226+
export default Page;
227+
```
228+
229+
To access Netlify Identity from pages without server-side rendering, you can create a [Next API route](https://nextjs.org/docs/api-routes/introduction) that performs identity-related logic:
230+
231+
```js
232+
export default async function getUser(req, res) {
233+
// Get event and context from Netlify Function
234+
const {
235+
netlifyFunction: { event, context },
236+
} = req;
237+
238+
// Access Netlify identity
239+
const { user } = context.clientContext;
240+
241+
// Respond with user object
242+
res.json({ user });
243+
}
244+
```
245+
246+
\* Note that pages using getInitialProps are only server-side rendered on initial page load and not when the user navigates client-side between pages.
247+
248+
202249
## Caveats
203250

204251
### Fallbacks for Pages with `getStaticPaths`

0 commit comments

Comments
 (0)