React+Firebase Auth谷歌登录报错:缺失初始状态求助
Firebase Auth Google 登录报错排查求助
此前React应用中Firebase Auth的Google登录功能一切正常,但自2024年6月24日起,登录时出现如下错误:
Unable to process request due to missing initial state. This may happen if browser sessionStorage is inaccessible or accidentally cleared. Some specific scenarios are - 1) Using IDP-Initiated SAML SSO 2) Using signInWithRedirect in a storage-partitioned browser environment.
登录流程代码
export const signInWithGoogle = () => { return new Promise(async (resolve, reject) => { const provider = new firebase.auth.GoogleAuthProvider() provider.setCustomParameters({ prompt: 'select_account' }) try { const result = await firebase.auth().signInWithPopup(provider) const user = result.user const usersRef = firebase.firestore().collection('users') const userDocByEmail = await usersRef.where('mail', '==', user.email).get() const newDocExistingAccount = firebase.firestore().collection('existingAccounts').doc().id if (!userDocByEmail.empty) { const existingUserRef = userDocByEmail.docs[0].ref await existingUserRef.update({ 'metadata.last_login': new Date() }) const existingUserDoc = await existingUserRef.get() resolve({ user: existingUserDoc.data(), isNewUser: false, success: true, message: 'Successfully signed in with Google.' }) } else { const userRef = usersRef.doc(user.uid) await userRef.set({ mail: user.email, first_name: user.displayName.split(' ')[0], last_name: user.displayName.split(' ')[1] || '', compagny: '', user_id: user.uid, role: 'Client', country: null, personalPhone: null, metadata: { status: 'PendingRegistration', last_login: new Date(), registration_date: new Date() }, emailVerfied: user.emailVerified }) await firebase.firestore().collection('existingAccounts').doc(newDocExistingAccount) .set({ mail: user.email, mail_id: newDocExistingAccount, user_id: user.uid, created_on: new Date().toISOString() }) const newUserDoc = await userRef.get() resolve({ user: newUserDoc.data(), isNewUser: true, success: true, message: `Your account is currently under validation. However, you are free to explore and use our platform at your convenience. Thank you for your understanding.` }) } } catch (error) { let errorMessage = 'An unexpected error occurred. Please try again later.' switch (error.code) { case 'auth/user-not-found': errorMessage = `Sorry, no user found with the provided login information. Please check your details and try again.` break case 'auth/email-already-exists': errorMessage = `The email address you entered is already associated with an existing account. If this email address belongs to you, please sign in through the login page. If you don't remember your password, you can reset it by following the recovery instructions on our login page.` break case 'auth/invalid-email': errorMessage = `The provided email address is not valid. Please check your information and try again.` break case 'auth/wrong-password': errorMessage = 'The provided password is incorrect. Please check your details and try again.' break case 'auth/user-disabled': errorMessage = `Your account has been disabled. Please contact our support team for more information.` break case 'auth/user-declined': errorMessage = `Your registration request was declined. Please contact our support team for more information.` break case 'auth/missing-password': errorMessage = `Please enter your password to continue.` break case 'auth/popup-closed-by-user': return } reject({ status: 'error', error: errorMessage }) } }) }
已排除的情况
- 浏览器sessionStorage可正常访问
- 未使用IDP-Initiated SAML SSO
- 不在存储分区浏览器环境中
- 应用使用自定义域名
- 部署在Firebase Hosting上
请问有人遇到过相同问题或知晓可能的原因吗?恳请提供帮助,谢谢。
内容的提问来源于stack exchange,提问作者Dani
相关产品推荐
相关产品推荐

