Giter Site home page Giter Site logo

Comments (10)

FinnWoelm avatar FinnWoelm commented on May 23, 2024 2

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

from next-on-netlify.

FinnWoelm avatar FinnWoelm commented on May 23, 2024 1

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

from next-on-netlify.

kevee avatar kevee commented on May 23, 2024

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

from next-on-netlify.

FinnWoelm avatar FinnWoelm commented on May 23, 2024

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

from next-on-netlify.

lednhatkhanh avatar lednhatkhanh commented on May 23, 2024

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

from next-on-netlify.

FinnWoelm avatar FinnWoelm commented on May 23, 2024

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

from next-on-netlify.

lednhatkhanh avatar lednhatkhanh commented on May 23, 2024

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

from next-on-netlify.

philippbosch avatar philippbosch commented on May 23, 2024

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

from next-on-netlify.

idappstack-io avatar idappstack-io commented on May 23, 2024

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 ?

from next-on-netlify.

dan-cooke avatar dan-cooke commented on May 23, 2024

@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

from next-on-netlify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.