netlify / gotrue-js

JavaScript client library for GoTrue
MIT License
472 stars 68 forks source link

Usage in node environment #46

Open canibanoglu opened 5 years ago

canibanoglu commented 5 years ago

I couldn't be sure where to open this issue here or in the micro-api-client repo but I'm going to go with this one.

The problem that I ran into is that all the nice methods like .signup, .login are only available in browser environments as the requests rely on fetch. All of these methods fail when I try to use them in a node environment (for example from a lambda function) because fetch is not defined.

I believe the fix would be in micro-api-client but if this is really by design (if so please explain to me so I can understand) it should be documented on both repos.

In case it is useful, here's what I want to do:

I want to call an api endpoint, let's say /.netlify/functions/signup from my gatsby application and in that function I want to use gotrue-js to take care of creating the user and doing some DB operations.

I understand that I can achieve a similar thing by creating the user on the client and subscribing to the identity hooks but it is still not the same and the hidden dependency on fetch is really frustrating.

swyxio commented 5 years ago

indeed it was only designed for the browser. we could use node-fetch for node, or axios as a xplatform thing. not a hard thing to fix. PR welcome.

canibanoglu commented 5 years ago

I am sorry for getting back to you this late. I would be glad to submit a PR. I was thinking of using isomorphic-fetch, would that be agreeable for you?

swyxio commented 5 years ago

i dont have a strong opinion. whats the difference? 😅 i went digging a bit and it looks like the line to fix is actually here https://github.com/netlify/micro-api-client/blob/master/src/index.js#L64

pr welcome.

swyxio commented 5 years ago

https://github.com/netlify/micro-api-client/issues/7 lol we even have an issue open.

do lets be mindful of the bundle size.

pukhalski commented 5 years ago

any updates on this one so far? @sw-yx

swyxio commented 5 years ago

pr welcome

scottbram commented 5 years ago

If I understand this issue correctly, implementing gotrue-js as a Netlify Function would not work currently?

swyxio commented 5 years ago

more accurately, implementing gotrue-js IN a Netlify Function wouldnt work because it requires the browser's fetch implementation. but thats a matter of swapping it out for an isomorphic fetch implementation

scottbram commented 5 years ago

more accurately, implementing gotrue-js IN a Netlify Function

True.

thats a matter of swapping it out for an isomorphic fetch implementation

Which is just past the ragged fringe of my knowledge. I tried a workaround/kludge to require/bundle node-fetch but that was unsuccessful (unsurprisingly...)

I'll watch for someone more qualified to hopefully facilitate a solution.

Thanks!

derit commented 4 years ago

in nodejs like this

global.fetch = require("node-fetch");
yearofthedan commented 3 years ago

Just adding since I've stumbled onto it today, for this to work we would also need to modify this reference to the window object when decoding the token claims: https://github.com/netlify/gotrue-js/blob/cc2dc5e991fa6c1cd9c8bea700fc1d4d985d4bb5/src/user.js#L217.

eivindml commented 3 years ago

I see that this issue still is present. 😊 1+ for a fix. @derit's solution works great in the meantime though.