Axios получил запрос успешно, но атрибут данных пуст? - PullRequest
3 голосов
/ 10 июня 2019

У меня есть простой компонент реагирования:

import React from "react"
import axios from "axios"

export default () => {
  const axios = require("axios")

  axios.get("http://localhost:8000/user").then(function(res) {
    // handle success
    console.log(res)
  })

  return (
    <div>
        Hello
    </div>
  )
}

Я что-то получаю, с кодом состояния 200, и это делает console.log, но поле данных пустое.Это просто data: "";Я получаю запрос get на свой собственный сервер nodejs.Я должен получить обратно зарегистрированного пользователя.Это работает, когда я захожу на URL напрямую (localhost:8000/user).Выглядит это так:

"displayName": "thomas",
"id": "google-oauth2|197913719739127391723",
"user_id": "google-oauth2|197913719739127391723",
"name": {
"givenName": "thomas3000"
},
"emails": [
{
"value": "thomas3k@tmail.com"
}
],
"picture": "https://akfaf/afaf/photo.jpg",
"locale": "en",
"nickname": "thomas3000",
"_json": {
"sub": "google-oauth2|197913719739127391723",
"given_name": "thomas3k",
"nickname": "tom",
//... and so on; changed the values here.

Тем не менее, вот console.log ответа axios: Response seems ok

Атрибут данных - пустая строка.Я не знаю, почему я не могу получить эти данные в моем запросе на получение axios?

edit: (запрос кода бэкенда)

Backend

//imports here, then:    
passport.use(
  new Auth0Strategy(
    {
      domain: "bla.eu.auth0.com",
      clientID: "afjgnrtkngewmofmwlefmlwems",
      clientSecret:
        "jngnsknkankjangkjangjknKJJKGKAHBJVvgjvVgjVbhJBjhbJbj",
      callbackURL: "http://localhost:8000/callback",
      audience: "https://bla.eu.auth0.com/userinfo",
      responseType: "code",
      scope: "openid email profile",
    },
    function(accessToken, refreshToken, extraParam, profile, done) {
      return done(null, profile)
    }
  )
)

passport.serializeUser(function(user, done) {
  done(null, user)
})

passport.deserializeUser(function(user, done) {
  done(null, user)
})

const app = express()
app.use(cors({ origin: "http://localhost:8001" }))

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

app.use(
  session({ secret: "secret_key", resave: true, saveUninitialized: true })
)

app.use(passport.initialize())
app.use(passport.session())

app.use(function(req, res, next) {
  res.locals.loggeedIn = false
  if (req.session.passport && typeof req.session.passport.user !== undefined) {
    res.locals.loggedIn = true
  }
  next()
})

app.get("/", function(req, res, next) {
  res.send("Root")
})

app.get(
  "/login",
  passport.authenticate(
    "auth0",
    {
      domain: "bla.auth0.com",
      clientID: "kafmkafmkamfkamfka",
      redirectUri: "http://localhost:8000/callback",
      audience: "https://bla.auth0.com/userinfo",
      responseType: "code",
      scope: "openid email profile",
    },
    function(req, res, next) {
      res.redirect("/")
    }
  )
)

app.get("/logout", function(req, res, next) {
  req.logout()
  res.redirect("/")
})

app.get(
  "/callback",
  passport.authenticate("auth0", {
    failureRedirect: "/failure",
  }),
  function(req, res) {
    res.redirect("/user")
  }
)

app.get("/user", function(req, res, next) {
  res.send(req.user)
})

app.get("/failure", function(req, res, next) {
  res.send("Failure")
})

app.listen(8000, function() {
  console.log("running")
})
...