[英]How to Connect Firebase Auth with Google One Tap Login
I have created a web app with Firebase and React.js and implemented sign-in with Google.我用 Firebase 和 React.js 创建了一个 web 应用程序,并实现了使用 Google 登录。 I then tried to implement GoogleOneTapSignin and the one-tap-sign-in UI is working successfully because I used the react-google-one-tap-login npm package. If may react app I have a function that listens for AuthStateChange and then either registers the user if they are new or sign in them if they are already a member and also updates the state if they logged.
然后我尝试实施 GoogleOneTapSignin 并且一键登录 UI 工作成功,因为我使用了 react-google-one-tap-login npm package。如果可能反应应用程序我有一个 function 监听 AuthStateChange 然后要么如果他们是新用户,则注册用户;如果他们已经是会员,则注册他们;如果他们登录,则更新 state。 out.
出去。 Now that I have implemented google-one-tap-login, I was expecting the onAuthSTaetChanged function to be triggered if a user signs in using the google-one-tap-login but it is not the case.
既然我已经实现了 google-one-tap-login,我期待在用户使用 google-one-tap-login 登录时触发 onAuthSTaetChanged function,但事实并非如此。 Below is the part of my App.js code that handles the user auth.
下面是我的 App.js 代码中处理用户身份验证的部分。
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]);
I tried to implement the solution suggested by the 2nd answer in this StackOverflow question but I get the error below on the console.我尝试实施此StackOverflow 问题中第二个答案建议的解决方案,但我在控制台上收到以下错误。 when I use google-one-tap-sign-in.
当我使用 google-one-tap-sign-in 时。 Remember I am not using the FirebaseUi library.
请记住,我没有使用 FirebaseUi 库。 So far my application only uses the sign in with Google
到目前为止,我的应用程序仅使用 Google 登录
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."
The ID token required by Firebase's signInWithCredential
function exists within the credential
property of the response
object. Here is a sample function below using Firebase V8. Firebase 的
signInWithCredential
function 所需的 ID 令牌存在于response
object 的credential
属性中。这是下面使用 Firebase V8 的示例 function。
// 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);
}
}
Firebase v9 Firebase v9
// 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);
}
}
The response
param is a credential response returned from the Google one-tap function callback. response
参数是从谷歌一键 function 回调返回的凭据响应。
google?.accounts.id.initialize({
client_id: your-google-app-client-id.apps.googleusercontent.com,
callback: handleCredentialResponse,
});
google?.accounts.id.prompt((notification) => {
console.log(notification);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.