Comments (10)
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.
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:
- Get the Authorization Bearer token from the request object:
req.headers.authorization
- Initialize the Netlify identity auth object:
auth = new GoTrue({ ApiUrl: 'https://<your domain name>/.netlify/identity' })
with the bearer token - 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.
Closing, it ends up it's passed as a third argument.
from next-on-netlify.
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.
@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.
@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.
@FinnWoelm Thank you so much for your hard work implementing this, can't wait to try it!
from next-on-netlify.
@FinnWoelm this is great, thank you!
is there any chance this will also work in netlify dev
at one point?
from next-on-netlify.
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 ofnext-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.
@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)
- Add tests for root level index pages of all page types for i18n
- Use splats in SSG redirects instead of adding a redirect for every exact prerendered path HOT 5
- Investigate tradeoffs between redirects and copying files [to correct paths] HOT 1
- Netlify post processing/bundling causes some rendering to be blocked HOT 4
- "blocking" on getStaticPaths' fallback throwing ambiguous error HOT 1
- Cannot find module 'critters' HOT 2
- Missing `pages-manifest.json` HOT 1
- Failure when the publish directory is `public`. HOT 1
- Cannot find module 'jimp' HOT 12
- Header config causes CLI to throw "invalid rule: cannot contain anything after *" HOT 7
- _redirects sorted wrong when using catch all route and dynamic routes HOT 4
- Add clarification for deployment with submodules HOT 4
- Cannot find module critters HOT 15
- [i18n] preview redirects are failing with non-default locales HOT 8
- Forward options params to "nextOnNetlify" function from node command-line args HOT 1
- Allow API routes to optionally run as background function HOT 3
- "The 'path' argument must be of type string. Received undefined" when using next-auth HOT 18
- Missing locale files with next-i18next HOT 10
- Timeout error when using MongoDB in getServerSideProps HOT 4
- *** deprecating next-on-netlify *** (info & feedback) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-on-netlify.