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

Netlify Identity clientContext is not passed to API endpoints and pages #20

Closed
kevee opened this issue Jun 16, 2020 · 10 comments · Fixed by #119
Closed

Netlify Identity clientContext is not passed to API endpoints and pages #20

kevee opened this issue Jun 16, 2020 · 10 comments · Fixed by #119

Comments

@kevee
Copy link

kevee commented Jun 16, 2020

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.

@kevee
Copy link
Author

kevee commented Jun 17, 2020

Closing, it ends up it's passed as a third argument.

@kevee kevee closed this as completed Jun 17, 2020
@FinnWoelm
Copy link
Contributor

Hi @keeve,

Is it really passing the Netlify context to the API route? I'm surprised about that actually! I never checked, but I had assumed that this was something we'd still need to implement. 😮

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! 🔥
- Finn

@lednhatkhanh
Copy link

lednhatkhanh commented Sep 7, 2020

@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.

@FinnWoelm
Copy link
Contributor

@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:

patch-package 6.2.2
Applying patches...
Error: Patch file found for package next-aws-lambda which is not present at node_modules/next-aws-lambda

That is fine, we need to manually copy and apply the patches from next-on-netlify:

cp -r ./node_modules/next-on-netlify/patches ./patches
npx patch-package

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!

@FinnWoelm FinnWoelm reopened this Sep 10, 2020
@FinnWoelm FinnWoelm changed the title context not passed to API routes Netlify Identity clientContext is not passed to API endpoints and pages Sep 10, 2020
FinnWoelm added a commit that referenced this issue Dec 15, 2020
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))
@FinnWoelm
Copy link
Contributor

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 next-on-netlify. If you want to use it today, you can temporarily use the master branch of next-on-netlify: npm install netlify/next-on-netlify#main --save

@lednhatkhanh
Copy link

@FinnWoelm Thank you so much for your hard work implementing this, can't wait to try it!

@philippbosch
Copy link

@FinnWoelm this is great, thank you!
is there any chance this will also work in netlify dev at one point?

@idappstack-io
Copy link

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 next-on-netlify. If you want to use it today, you can temporarily use the master branch of next-on-netlify: npm install netlify/next-on-netlify#main --save

How to get clientContext and identity in express JS ? and if there is a way will it work on netlify dev ?

@FinnWoelm
Copy link
Contributor

Hi @philippbosch and @idappstack-io,

Happy to hear! 😊

Unfortunately, there is no built-in support for this in netlify dev at this point. I'm tagging @lindsaylevine, so that she is aware. Perhaps it's possible to manually implement this Netlify Identity logic in your page's getServerSideProps for now. I'm not completely sure how, but broadly speaking it should work like this:

  1. Get the Authorization Bearer token from the request object: req.headers.authorization
  2. Initialize the Netlify identity auth object: auth = new GoTrue({ ApiUrl: 'https://<your domain name>/.netlify/identity' }) with the bearer token
  3. Get the current user with auth.currentUser()

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 auth object with the Authorization Bearer token in step 2. Let us know if you want to explore this route and we might be able to touch base with the Netlify team working on Netlify Identity :)

@dan-cooke
Copy link

dan-cooke commented Mar 4, 2021

@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 clientContext if I am using apollo-server-micro ?

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
6 participants