简体   繁体   English

发送到客户端后无法设置标题 - 谷歌登录错误

[英]Cannot set headers after they are sent to the client - google sign in error

I am working through an authentication application from devchallenges.io .我正在处理来自devchallenges.io的身份验证应用程序。 So far, I've been able to set up the basic login with email and password.到目前为止,我已经能够使用 email 和密码设置基本登录。 I'm now trying to set up google login auth.我现在正在尝试设置谷歌登录身份验证。 When testing the server with Postman I get the error Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.使用 Postman 测试服务器时,我收到错误Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. The app seems to stills work as a json web token is returned so not too sure why I'm getting this error.该应用程序似乎仍然可以作为 json web 令牌返回,因此不太清楚为什么我会收到此错误。

在此处输入图像描述

server.js服务器.js

// @route    POST auth/google
// @desc     auth user with google
// @access   Public
app.post('/auth/google', (req, res) => {
  const { token } = req.body;
  const CLIENT_ID =
    '1068367393637-jtuoidgq5mi5krd5q31u9ncjovt3fvgh.apps.googleusercontent.com';
  const client = new OAuth2Client(CLIENT_ID);

  let { email, name, picture, password } = '';

  async function verify() {
    const ticket = await client.verifyIdToken({
      idToken: token,
      audience: CLIENT_ID,
    });
    const payload = ticket.getPayload();
    const userid = payload['sub'];

    email = payload.email;
    name = payload.name;
    picture = payload.picture;
    password = generator.generateMultiple({
      length: 6,
    });

    try {
      // See if user exists
      let user = await AuthUser.findOne({ email });
      if (user) {
        // Return jsonwebtoken
        const payload = {
          user: {
            id: user.id,
          },
        };

        jwt.sign(
          payload,
          config.get('jwtSecret'),
          { expiresIn: '5 days' },
          (err, token) => {
            if (err) throw err;
            res.json({ token });
          }
        );
      }

      user = new AuthUser({
        name,
        email,
        picture,
        password,
      });
      // Encrypt password
      const salt = await bcrypt.genSalt(10);
      user.password = await bcrypt.hash(password, salt);
      await user.save();
      // Return jsonwebtoken
      const payload = {
        user: {
          id: user.id,
        },
      };
      jwt.sign(
        payload,
        config.get('jwtSecret'),
        { expiresIn: '5 days' },
        (err, token) => {
          if (err) throw err;
          res.json({ token });
        }
      );
    } catch (error) {
      console.error(error.message);
      res.status(500).send('Server error');
    }
  }
  verify().catch(console.error);
});

All I had to do was add an else conditional statement.我所要做的就是添加一个 else 条件语句。

if (user) {
        // Return jsonwebtoken
        const payload = {
          user: {
            id: user.id,
          },
        };

        jwt.sign(
          payload,
          config.get('jwtSecret'),
          { expiresIn: '5 days' },
          (err, token) => {
            if (err) throw err;
            res.json({ token });
          }
        );
      } else {
        user = new AuthUser({
          name,
          email,
          picture,
          password,
        });
        // Encrypt password
        const salt = await bcrypt.genSalt(10);
        user.password = await bcrypt.hash(password, salt);
        await user.save();
        // Return jsonwebtoken
        const payload = {
          user: {
            id: user.id,
          },
        };
        jwt.sign(
          payload,
          config.get('jwtSecret'),
          { expiresIn: '5 days' },
          (err, token) => {
            if (err) throw err;
            res.json({ token });
          }
        );
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将标头发送到客户端后无法设置标头 - 错误 - Cannot set headers after they are sent to the client - error 发送到客户端错误后无法设置标头 - Cannot set headers after they are sent to the client error 发布请求后出现“发送到客户端后无法设置标头”错误 - “Cannot set headers after they are sent to the client” error after post request 将标头发送到客户端错误后无法设置标头 nodejs 错误 - Cannot set headers after they are sent to the client error nodejs error 节点JS错误:(将标头发送到客户端后无法设置标头) - Node JS Error: (Cannot set headers after they are sent to the client) Auth Error 在发送到客户端后无法设置标头 - Auth Error Cannot set headers after they are sent to the client 错误:在快递中间件中将标头发送回客户端后无法设置标头 - Error: Cannot set headers after they are sent back to the client in express middleware Nestjs 错误:将标头发送到客户端后无法设置标头 - Nestjs Error: Cannot set headers after they are sent to the client 将标头发送到客户端后无法设置标头 node.js 中的错误 - Cannot set headers after they are sent to the client Error in node.js Mongodb 错误:发送到客户端后无法设置标头 - Mongodb error: Cannot set headers after they are sent to the client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM