Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 需要帮助让Passport JS将令牌(或任何东西)返回到客户端吗_Javascript_Node.js_Reactjs_Passport.js - Fatal编程技术网

Javascript 需要帮助让Passport JS将令牌(或任何东西)返回到客户端吗

Javascript 需要帮助让Passport JS将令牌(或任何东西)返回到客户端吗,javascript,node.js,reactjs,passport.js,Javascript,Node.js,Reactjs,Passport.js,我已经搞乱这个有一段时间了,需要一些帮助。我有一个创建用户的网站。我已经设置了passport js oauth 20,当用户点击“使用谷歌注册”按钮时,后端的一切都很好,用户就被创建了。我无法将代码传递到前端。。我希望能够通过设置仪表板来确定用户是否经过身份验证 git集线器链接 相关代码: 客户 import { setAlert } from "./alert"; import { REGISTER_FAIL, REGISTER_SUCCESS, REGISTER_GO

我已经搞乱这个有一段时间了,需要一些帮助。我有一个创建用户的网站。我已经设置了passport js oauth 20,当用户点击“使用谷歌注册”按钮时,后端的一切都很好,用户就被创建了。我无法将代码传递到前端。。我希望能够通过设置仪表板来确定用户是否经过身份验证 git集线器链接

相关代码:
客户

import { setAlert } from "./alert";
import { REGISTER_FAIL, REGISTER_SUCCESS, REGISTER_GOOGLE } from "./types";

// Register USER

export const register = ({ name, email, password }) => async (dispatch) => {
  // handle google?

  const config = {
    headers: {
      "Content-Type": "application/json",
    },
  };

  const body = JSON.stringify({ name, email, password });

  try {
    const res = await axios.post("/api/users", body, config);

    dispatch({
      type: REGISTER_SUCCESS,
      payload: res.data,
    });
  } catch (error) {
    const errors = error.response.data.errors;

    if (errors) {
      errors.forEach((error) => dispatch(setAlert(error.msg, "error")));
    }
    dispatch({
      type: REGISTER_FAIL,
    });
  }
};

export const registerGoogle = () => async (dispatch) => {

  const config = {
    headers: {
      "Content-Type": "application/json",
    }
  }
  
  
  try {
    const res = await axios.get("/api/users/google", config);

    const body = await res.profile

    console.log(body)


  } catch (error) {
    
    dispatch({
      type: REGISTER_FAIL,
    });
  }
};```
BackEnd


```passport.use(
  new GoogleStategy(
    {
      // options for stragety
      callbackURL: "/api/users/google/redirect",
      clientID: clientId,
      clientSecret: cs,
       
    },
    async (accessToken, refreshToken, profile, done) => {
      // passport callback function
      
     

      const { id, displayName } = profile;
      const email = profile._json.email;
      const avatar = profile._json.picture;

      let user = await User.findOne({ email });
      if (!user) {
        user = new User({
          name: displayName,
          email,
          avatar,
          password: id,
        });

        await user.save();
      }

      const payload = {
        user: {
          id: user._id,
        },
      };

      jwt.sign(
        payload,
        config.get("jwtSecret"),
        { expiresIn: 360000 },
        (err, token) => {
          if (err) throw err;
         

          res.json({ token });
        }
      );

      await done(null, profile);  
    }
  )
);