last day (24 days later) » 

8:57 PM
1
A: React Client-side fetched URL doesn't match server-side requested URL

Horatiu Jefleafetch('/auth/google/callback' requires a full URL (for example https://localhost:8080/auth/google/callback) EDIT headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }

 
I tried but doesn't work. got the same error.
 
does your server method work if you test it with Postman for example?
 
yes. If I'm reaching /auth/google directly I have can see the response from the API
 
looks like the client is the issue then, let's focus on that
also "fetched URL is not the same as the requested URL" what do you mean?
 
I think you solved it. Now I this error: localhost/:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
 
8:57 PM
hi
 
hey
sorry
thanks a lot
I was running around in a circle for over a week now
when the issue was actually very simple!
 
np, let me know if you need more help
 
I think I've already solved the json issue
now I have this issue bundle.js:30 Uncaught (in promise) ReferenceError: data is not defined
I will open a specific react questions
It is probably related to my react code
 
sure thing, horatiu.jeflea@gmail.com ping me when you have it
 
ha yes cool
wait give me a sec
I'll publish it in 1min
0
Q: Unexpected token < in JSON at position 0 error with valid json

Simon BretonI have what seems to be a valid JSON object according to jsonlint: {"data":[{"name":"undefined - blablabla (undefined)"},{"name":"undefined - blablabla (undefined)"},{"name":"undefined - blablabla (undefined)"},{"name":"undefined - On blablabla (undefined)"}]} however, I have the following er...

 
9:18 PM
I'm leaving. Thanks a lot for the help so far :)
 
9:50 PM
Hi Kevin
This was my initial question
0
Q: React Client-side fetched URL doesn't match server-side requested URL

Simon BretonI'm working on a small nodejs-express-react app. I'm sending a request against the Google Analytics Management API server-side then trying to fetch the response from the client-side. It doesn't work as the Status Code of the fetch is Status Code: 405. however, I'm seeing that the fetched URL is n...

 
o/
 
I'd love to have your insight
 
yeah that bit confused me at the end, but i figured i was missing something so i removed the related comment
 
yeah. I'm running around in a circle. Did a lot of modification in my code and change my fetching URL between two StackOverflow questions
so I was fetching / that is why it was HTML!
 
Yea that makes sense
as far as the CORS error goes... i'm trying to piece that together
i've worked with that kind of auth in the past, but it's been a while, and i don't think i handled it over ajax
 
9:54 PM
but I don't understand when you are saying this
what does this have to do with the auth
I mean I'm making my request
so I have my data
I can fetch without worrying about the auth no?
I mean what I can do is passing the response in a cookie for example
is that case it works perfectly
 
If it's a page that doesn't need authorization, yes, but if you have middleware in place that requires the user to be authenticated in order to reach that route,
 
but how can I structure my code so I can pass the response to a page that doesn't need authorization
do you understand what I mean?
 
Yes
if you don't need authorization on a particular route, just leave that middleware out of it
 
but I still need my middleware to get my response
but then once I have my response I need to pass it somewhere so I can fetch it freely
do you understand what I mean?
 
no
 
9:57 PM
well I have this app.get(
"/auth/google/callback",
passport.authenticate("google", { failureRedirect: "/error", session: false }),
function(req, res) {
var token = req.user.token;
request('https://www.googleapis.com/analytics/v3/management/accounts?access_token=' + token,
function (error, response, body) {
console.log(JSON.parse(body).items);
res.send({data:JSON.parse(body).items})
});
}
);
I need this to request the Google Analytics API
so I have my response in body.items
 
Right, and that's a route that google sends users after they've successfully authenticated with google
 
to tell your app that the user has authenticated
 
all this is good
what I want to do is send the response somewhere
 
I'm not sure how we get from there to returning the json file
 
9:58 PM
so I can fetch it freely from the front-end
but why is this complicated?
I mean I can pass it in a cookie for example
does the only option is to send the response in a json file I'm saving somewhere?
or even some kind of db?
It seems crazy to me as I have the response and can't simply pass it to the front-end
I don't understand why
 
I've never used it that way in terms of the google auth api, i usually forced the client to authenticate before doing anything, afterwhich they'd be able to bypass the middleware because they're already authenticated
 
but that's what I want to do
 
so i'm not sure how doing it other ways would work, it's just not something I've used or done the research on
 
How is my code different from your method
I mean what I doing is authenticating the client
 
I create a session, and as long as they're authenticated, they're authenticated for that entire session so the auth redirect/callback never happens after they've logged in.
 
10:02 PM
I mean I have the token because the client has been authenticated
 
and i handle that directly rather than while i'm making a request for json
 
hum ok
so you are using this session things
I've seen this module
 
i don't really have code to look at that would be meaningful, it was all written some 5-6 years ago, a lot has changed since then
 
yeah no but I have recent code with this
still I don't understand why we need to do things this way
I don't understand why I can't really simply pass the response to the front-end
session and all seems to be really overkill
I'm building a simple app I just need to do one api call
but you are not responsible for this I guess :)
 
So, the way google's auth is supposed to work is it has a process... If the user isn't logged in, they get redirected to google, they sign in, grant access, and then the user is sent back to the callback url you specify, which in turn should return the json. but i don't understand how that could work over ajax
the process would have to be different
the server can't force the client to redirect to a new location via an ajax request
that's why the auth has to be separate
 
10:07 PM
yes. but why is this so complicated for so to have this separate. I mean can't i do someone like this app.get('/api', (req, res) => res.send({ data: response }));
I mean I got this ```app.get(
"/auth/google/callback",
passport.authenticate("google", { failureRedirect: "/error", session: false }),
function(req, res) {
var token = req.user.token;
request('googleapis.com/analytics/v3/…' + token,
function (error, response, body) {
console.log(JSON.parse(body).items);
res.send({data:JSON.parse(body).items})
});
}
);```
then this app.get('/api', (req, res) => res.send({ data: response }));
so I can fetch the /api url
do you understand what I mean?
 
Yes to the first, but i don't understand what /auth/google/callback has to do with it
that's where i'm kinda getting hung up on
 
because that is where I got the token
all this work with passport
the step before is this `app.get(
"/auth/google",
passport.authenticate("google", { scope: ['Profile','https://www.googleapis.com/auth/analytics.readonly'] })
);`
 
app.get('/foo.json', (req, res) {
  res.json({foo: 'bar'});
});
for example world work fine
on it's own
and need no authentication
 
yes
so my question is how to I pass the response from the request to this
 
but you need that route to hit your google analytics account, for which you need authentication
When the user authenticates with google and gives your api access to it, you should then be able to use that token with your requests
 
10:11 PM
yes
but once I have the response
I've free to pass it the way I want no?
 
it'd be something like this:
app.get('/api/me',
  passport.authenticate(/*... whatever this is supposed to be for your setup ...*/),
  function(req, res) {
    res.json(yoData);
  });
and you'll be able to find the token on the req object
 
I don't understand why you are talking to me about the token
I already have the token
and I'm able to make the request succesfuly
 
Right, so, what does your json endpoint look like
 
sorry it is crazy but there is something I don't understand and I don't know what/why
 
so far you've only given us the /auth endpoints
which are for logging in
 
10:15 PM
but I have the response of my request at the end of the /auth process no?
I mean my question is just
how do I pass this JSON.parse(body).items to somewhere else so I can fetch it
It makes me crazy that I can console.log the response and see it in my browser, save it in a cookie
but I can't pass it to the front-end directly
 
10:28 PM
sorry had to step away, but, basically, once the user is authenticated, you should be able to hit regular api end points and send them the json directly
I dont think /auth/google/callback should be returning json
There are a few regulars in the JS room that are very familiar with this subject, might be better to ask there
 
yeah
I'm looking for a way to simply store the token server side
I think that is what I need
thanks!
 

last day (24 days later) »