Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage in node environment #46

Open
canibanoglu opened this issue Apr 10, 2019 · 12 comments
Open

Usage in node environment #46

canibanoglu opened this issue Apr 10, 2019 · 12 comments

Comments

@canibanoglu
Copy link

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
Copy link
Contributor

swyxio commented Apr 13, 2019

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
Copy link
Author

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
Copy link
Contributor

swyxio commented Apr 26, 2019

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
Copy link
Contributor

swyxio commented Apr 26, 2019

netlify/micro-api-client#7 lol we even have an issue open.

do lets be mindful of the bundle size.

@pukhalski
Copy link

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

@swyxio
Copy link
Contributor

swyxio commented Jul 14, 2019

pr welcome

@scottbram
Copy link

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

@swyxio
Copy link
Contributor

swyxio commented Jul 22, 2019

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
Copy link

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
Copy link

derit commented Jan 15, 2020

in nodejs like this

global.fetch = require("node-fetch");

@yearofthedan
Copy link

yearofthedan commented Dec 31, 2020

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:

var result = window.atob(output); //polifyll https://github.com/davidchambers/Base64.js
.

@eivindml
Copy link

eivindml commented Jun 9, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants