Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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
Reactjs 反应:对Facebook登录呼叫Submit进行两次反应?_Reactjs - Fatal编程技术网

Reactjs 反应:对Facebook登录呼叫Submit进行两次反应?

Reactjs 反应:对Facebook登录呼叫Submit进行两次反应?,reactjs,Reactjs,我正在使用react创建一个登录页面,它允许用户使用Facebook、Google或用户名和密码登录。我只是意识到,每次我使用Facebook登录,都是因为我要给onSubmit打两次电话。只有Facebook身份验证导致此问题。因此,将调用我的后端验证并返回res.json消息,该消息将在前端返回错误消息。如果用户决定使用Facebook登录,如何避免这种情况发生?我正在使用react facebook登录和react google登录作为社交媒体登录 facebook按钮 const han

我正在使用react创建一个登录页面,它允许用户使用Facebook、Google或用户名和密码登录。我只是意识到,每次我使用Facebook登录,都是因为我要给onSubmit打两次电话。只有Facebook身份验证导致此问题。因此,将调用我的后端验证并返回res.json消息,该消息将在前端返回错误消息。如果用户决定使用Facebook登录,如何避免这种情况发生?我正在使用react facebook登录和react google登录作为社交媒体登录

facebook按钮

const handleFacebookLogin = async (res) => {
    dispatch(loginUser({ isFacebookId: res?.userID }));
  };

<FacebookLogin
              appId='code'
              callback={handleFacebookLogin}
              render={(renderProps) => (
                <button
                  onClick={renderProps.onClick}
                  className='w-full font-sans text-xs flex items-center focus:outline-none'
                >
                  <FontAwesomeIcon
                    icon={faFacebookF}
                    className='svg-size fill-current text-blue-600'
                  />
                  <span className='w-full'>Sign In with Facebook</span>
                </button>
              )}
            />
const handleFacebookLogin=async(res)=>{
分派(loginUser({isFacebookId:res?.userID}));
};
(
使用Facebook登录
)}
/>
后端 导出常量登录用户=异步(请求、恢复)=>{ 让{email,username,password,isFacebookId,isGoogleId}=req.body

  try {
    if (isFacebookId) {
      const chkUser = await userModel.findOne({ isFacebookId: isFacebookId });
      if (chkUser) {
        if (!chkUser.isActive) {
          return res.status(400).json({
            msg: `The account is not yet activated.
            \n Please check your email for account activation link.`,
            activation: true,
          });
        } else {
          return res.status(200).json({
            _id: chkUser._id,
            email: chkUser.email,
            username: chkUser.username,
            name: chkUser.name,
            isAdmin: chkUser.isAdmin,
            msg: 'Login Success',
            tokenId: createRefreshToken(chkUser._id),
          });
        }
      } else {
        return res.status(400).json({
          msg: 'This Facebook account is not register yet. Please register using this ',
          googleId: true,
        });
      }
    } else if (isGoogleId) {
      const chkUser = await userModel.findOne({ isGoogleId: isGoogleId });
      if (chkUser) {
        if (!chkUser.isActive) {
          return res.status(400).json({
            msg: `The account is not yet activated.
            \n Please check your email for account activation link.`,
            activation: true,
          });
        } else {
          return res.status(200).json({
            _id: chkUser._id,
            email: chkUser.email,
            username: chkUser.username,
            name: chkUser.name,
            isAdmin: chkUser.isAdmin,
            msg: 'Login Success',
            tokenId: createRefreshToken(chkUser._id),
          });
        }
      } else {
        return res.status(400).json({
          msg: 'This Google account is not register yet. Please register using this ',
          googleId: true,
        });
      }
    } else if (!username && !email) {
      console.log('ta3');
      return res.status(400).json({
        msg: 'Please Provide a Username or Email',
        username: true,
      });
    } else if (!password) {
      return res.status(400).json({
        msg: 'Please Provide a Password',
        password: true,
      });
    } else {
      if (username) {
        const isEmail = (val) => {
          var data =
            /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
          return data.test(val);
        };

        if (isEmail(username) === true) {
          email = username;
        }

        let chkUsernameEmail = email
          ? { email: username }
          : { username: username };

        const chkUser = await userModel.findOne(chkUsernameEmail);

        if (!chkUser) {
          return res.status(400).json({
            msg: 'Invalid Username or Email',
            username: true,
          });
        } else {
          if (!chkUser.isActive) {
            return res.status(400).json({
              msg: `The account is not yet activated.
              \n Please check your email for account activation link.`,
              activation: true,
            });
          } else {
            if (chkUser && (await chkUser.matchPassword(password))) {
              res.json({
                _id: chkUser._id,
                email: chkUser.email,
                username: chkUser.username,
                name: chkUser.name,
                isAdmin: chkUser.isAdmin,
                msg: 'Login Success',
                tokenId: createRefreshToken(chkUser._id),
              });
            } else {
              return res.status(400).json({
                msg: 'Invalid Password',
                password: true,
              });
            }
          }
        }
      }
    }
  } catch (error) {
    res.status(500).json({ msg: error.message });
  }
试试看{
如果(isFacebookId){
const chkUser=await userModel.findOne({isFacebookId:isFacebookId});
if(chkUser){
如果(!chkUser.isActive){
返回res.status(400).json({
msg:`帐户尚未激活。
\n请检查您的电子邮件中的帐户激活链接,
激活:对,
});
}否则{
返回res.status(200).json({
_id:chkUser.\u id,
电子邮件:chkUser.email,
用户名:chkUser.username,
姓名:chkUser.name,
伊萨明:切库瑟,伊萨明,
msg:'登录成功',
令牌id:createRefreshToken(chkUser.\u id),
});
}
}否则{
返回res.status(400).json({
msg:'此Facebook帐户尚未注册。请使用此'',
谷歌ID:没错,
});
}
}else if(iGoogle ID){
const chkUser=await userModel.findOne({isGoogleId:isGoogleId});
if(chkUser){
如果(!chkUser.isActive){
返回res.status(400).json({
msg:`帐户尚未激活。
\n请检查您的电子邮件中的帐户激活链接,
激活:对,
});
}否则{
返回res.status(200).json({
_id:chkUser.\u id,
电子邮件:chkUser.email,
用户名:chkUser.username,
姓名:chkUser.name,
伊萨明:切库瑟,伊萨明,
msg:'登录成功',
令牌id:createRefreshToken(chkUser.\u id),
});
}
}否则{
返回res.status(400).json({
msg:'此谷歌帐户尚未注册。请使用此'',
谷歌ID:没错,
});
}
}如果(!username&&!email),则为else{
console.log('ta3');
返回res.status(400).json({
msg:'请提供用户名或电子邮件',
用户名:true,
});
}否则如果(!密码){
返回res.status(400).json({
msg:'请提供密码',
密码:true,
});
}否则{
如果(用户名){
常量isEmail=(val)=>{
var数据=
/([^()[\]\\,;:\s@\\]+(\.[^()[\]\\,;:\s@\']+*)(“+\”)(\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.]124([a-zA Z-0-9]++.[a-zA Z]/]2,});
返回数据。测试(val);
};
如果(isEmail(用户名)==true){
电子邮件=用户名;
}
让chkusernamemail=电子邮件
?{电子邮件:用户名}
:{用户名:用户名};
const chkUser=await userModel.findOne(chkUsernameEmail);
如果(!chkUser){
返回res.status(400).json({
msg:'无效的用户名或电子邮件',
用户名:true,
});
}否则{
如果(!chkUser.isActive){
返回res.status(400).json({
msg:`帐户尚未激活。
\n请检查您的电子邮件中的帐户激活链接,
激活:对,
});
}否则{
if(chkUser&(等待chkUser.matchPassword(密码))){
res.json({
_id:chkUser.\u id,
电子邮件:chkUser.email,
用户名:chkUser.username,
姓名:chkUser.name,
伊萨明:切库瑟,伊萨明,
msg:'登录成功',
令牌id:createRefreshToken(chkUser.\u id),
});
}否则{
返回res.status(400).json({
消息:“无效密码”,
密码:true,
});
}
}
}
}
}
}捕获(错误){
res.status(500).json({msg:error.message});
}

我想在调用API时只需禁用UI上的按钮。

显示回调函数我已经添加了回调函数。非常确定这与FacebookLogin组件无关,因为我使用的是类似的代码。您提到的后端调用了两次,但没有显示调用后端的代码。我还包括我说react facebook登录导致调用onsubmit两次的原因是因为我试图在!username&&!email的if语句中添加一个console.log,出现了console.log,但当我使用google登录时,这种情况没有发生。