-
Notifications
You must be signed in to change notification settings - Fork 67
Netlify Identity clientContext is not passed to API endpoints and pages #20
Comments
Closing, it ends up it's passed as a third argument. |
Hi @keeve, Is it really passing the Netlify Well, great to hear!! I'll make sure to have a look at it soon and add an example to the demo repository! Let me know if you run into any other issues. Happy hacking! 🔥 |
@kevee @FinnWoelm Do you have any example code that I can follow? I couldn't make it to work, the third argument in Next.js API route is always undefined for me, even if I'm logged in. |
@lednhatkhanh: I had a closer look and it's not possible with the current production branch. The clientContext object is not passed to NextJS. I want to change that, but I need to do a bit more work to make sure it works well and is tested, etc... In order to make this work, we need to fork and modify one of our dependencies. So it's a bit of a bigger task. I'm reopening this issue. For now, you can use the following work-around, which hot-fixes the dependency using patch-package: npm install netlify/next-on-netlify#netlify-identity You will get an error/warning:
That is fine, we need to manually copy and apply the patches from next-on-netlify:
In your API, you can now access clientContext like this: export default (req, res) => {
const { clientContext } = req;
console.log(clientContext.identity)
console.log(clientContext.user)
} Let me know how this works for you and stay tuned for an actual fix that does not involve patching! |
When a page is being SSR-ed by a Netlify Function, allow users to access the function's event and context parameters. These can be accessed as a property on the `req` object in all SSR-ed pages and in API routes: - req.netlifyFunction.event - req.netlifyFunction.context This allows users to access/leverage Netlify identity for their Next.js page. See: #20 It also allows users to modify the callbackWaitsForEmptyEventLoop behavior. See: #66 (comment))
Hi @lednhatkhanh & @kevee: We just made it possible to access Netlify Identity in all server-side-rendered pages and API routes!! 🎉 See README for an example: https://github.com/netlify/next-on-netlify#using-netlify-identity More details here: #119 The feature has been merged into main, but won't be available until the next minor release of |
@FinnWoelm Thank you so much for your hard work implementing this, can't wait to try it! |
@FinnWoelm this is great, thank you! |
How to get clientContext and identity in express JS ? and if there is a way will it work on netlify dev ? |
Hi @philippbosch and @idappstack-io, Happy to hear! 😊 Unfortunately, there is no built-in support for this in
Here is some info on Netlify's gotrue-js library: https://github.com/netlify/gotrue-js I'm just not sure how to initialize the |
@FinnWoelm thanks for all this great info here. I've entered a bit of a rabbit hole this evening. Do you know how I would retrieve the The main issue here is that the exported function is inaccesible to me, so Its likely that the context is not being passed through to Apollo Server. /pages/api/graphql.ts const apolloServer = new ApolloServer({
schema,
context: ({ context }) => {
// I want to access the clientContext here
},
});
export default apolloServer.createHandler({ path: `/api/graphql` }); I may have to resort to the GoTrue API like you mentioned above |
When using API routes, it appears the
context
from Netlify is not passed to the page callback. This is important when using Netlify Identity for authenticated endpoints.The text was updated successfully, but these errors were encountered: