微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 wordpress 登录 Google 用户注册良好做法

如何解决使用 wordpress 登录 Google 用户注册良好做法

我的项目,使用 ReactJS 和 wordpress headless,我使用 JWT 作为身份验证,现在我正在开发一种让用户使用 Google 登录方法

我检查是否有使用 google 电子邮件的帐户,如果没有,我通过传递带有 google 值的元提供程序来注册用户

我不理解的是以下场景:

wordpress 需要用户名和密码,但 Goole 没有给我提供密码,我的想法是使用标准密码,但是当用户已经存在或者用户想要更改密码时怎么办?

我会留下我的一部分代码,以便您可以给我一个想法。

google 登录函数,使用组件'react-google-login',该函数由下面的组件触发,登录完成后验证Token。

  const signInWithGoogle = async (googleData) => {
    const res = await axios.post('api/auth/google',{
      token: googleData.tokenId,});
    console.log('Google Profile');
    console.log(res);

    const googleUser = {
      username: res.data.create.email,email: res.data.create.email,password: '123456',provider: 'google',picture: res.data.create.picture,};

    await api
      .post('wp/v2/users/register',googleUser)
      .then((response) => {
        console.log(response);
        onSubmit(googleUser);
      })
      .catch((err) => {
        console.log(err);
      });
  };

组件 GoogleLogin

 <GoogleLogin
            clientId={process.env.GOOGLE_ID}
            buttonText="Login"
            onSuccess={signInWithGoogle}
            onFailure={signInWithGoogle}
            cookiePolicy={'single_host_origin'}
            render={(renderProps) => (
              <button
                type="button"
                className="sign-in"
                onClick={renderProps.onClick}
                disabled={renderProps.disabled}
              >
                <img
                  className="icon google"
                  src={GoogleIcon}
                  alt="google"
                />
                Faça login com o Google
              </button>
            )}
          />

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。