首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何连接Firebase Auth与Google One Tap登录

如何连接Firebase Auth与Google One Tap登录
EN

Stack Overflow用户
提问于 2021-08-28 13:03:17
回答 1查看 532关注 0票数 0

我用Firebase和React.js创建了一个web应用程序,并实现了与谷歌的登录。然后,我尝试实现GoogleOneTapSignin,而一键登录用户界面正在成功地工作,因为我使用了react google-一键登录npm包。如果可能响应应用程序,我有一个监听AuthStateChange的函数,如果用户是新用户,可以注册他们,如果他们已经是成员,也可以在其中注册,如果他们登录,也会更新状态。出去吧。现在我已经实现了google一键登录,如果用户在使用google一键登录时签名,我就会期待onAuthSTaetChanged功能被触发,但事实并非如此。下面是我的App.js代码中处理用户auth的部分。

代码语言:javascript
复制
const classes = useStyles();
const dispatch = useDispatch();
const alert = useSelector(state => state.notification.alert);

// Handling Google-one-tap-signin
useGoogleOneTapLogin({
  onError: error => console.log(error),
  onSuccess: response => {
    console.log(response);
    const credential = provider.credential(response);
    auth.signInWithCredential(credential).then(result => {
      const {
        user
      } = result;
      console.log(user);
    });
  },
  googleAccountConfigs: {
    client_id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  }
});



//Handling firebase authentification

useEffect(() => {
  const unsubscribe = auth.onAuthStateChanged(async user => {
    // If there is a user create the user profile and update useState
    if (user) {
      // createUserProfile function creates the user profile in firestore if they are new
      const userRef = await createUserProfileDocument(user);
      userRef.onSnapshot(snapshot => {
        const doc = snapshot.data();
        dispatch(
          setUser({
            id: snapshot.id,
            ...doc
          })
        );
      });
    } else {
      dispatch(setUser(null));
    }
  });
  return () => {
    unsubscribe();
  };
}, [dispatch]);

我试图在这个StackOverflow question中实现第二个答案所建议的解决方案,但是我在控制台上得到了下面的错误。当我使用谷歌一体机登录的时候。记住,我不是在使用FirebaseUi库。到目前为止,我的应用程序只使用Google的登录。

代码语言:javascript
复制
t {
  code: "auth/argument-error",
  message: "credential failed: must provide the ID token and/or the access token.",
  a: null
}
a: null
code: "auth/argument-error"
message: "credential failed: must provide the ID token and/or the access token."

EN

回答 1

Stack Overflow用户

发布于 2022-03-18 14:23:37

Firebase的signInWithCredential函数所需的ID令牌存在于response对象的credential属性中。下面是一个使用Firebase V8的示例函数。

代码语言:javascript
复制
// firebase V8
function handleCredentialResponse(response) {
  if (response) {
    const cred = auth.GoogleAuthProvider.credential(response.credential);

    // Sign in with credential from the Google user.
    return auth().signInWithCredential(cred);
  }
}

火源v9

代码语言:javascript
复制
// firebase V9
import { getAuth, GoogleAuthProvider, signInWithCredential } from "firebase/auth";

const auth = getAuth();

function handleCredentialResponse(response) {
  if (response) {
    const cred = GoogleAuthProvider.credential(response.credential)

    // Sign in with credential from the Google user.
    return signInWithCredential(auth, cred);
  }
}

response参数是从Google抽头函数回调返回的凭据响应。

代码语言:javascript
复制
 google?.accounts.id.initialize({
    client_id: your-google-app-client-id.apps.googleusercontent.com,
    callback: handleCredentialResponse,
 });
 google?.accounts.id.prompt((notification) => {
    console.log(notification);
 });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68964619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档