我对 React Native 很陌生;我一直在尝试通过Firebase添加谷歌身份验证,但一直遇到以下警告:
[Unhandled promise rejection: ReferenceError: Can't find variable: auth]
at node_modules@babelruntimehelpersregeneratorRuntime.js:86:13 in tryCatch
at node_modules@babelruntimehelpersregeneratorRuntime.js:66:31 in <anonymous>
at node_modules@babelruntimehelpersregeneratorRuntime.js:86:13 in tryCatch
at node_modules@babelruntimehelpersregeneratorRuntime.js:124:27 in invoke
at node_modules@babelruntimehelpersregeneratorRuntime.js:148:16 in PromiseImpl$argument_0
at node_modulespromisesetimmediatecore.js:45:6 in tryCallTwo
at node_modulespromisesetimmediatecore.js:200:22 in doResolve
at node_modulespromisesetimmediatecore.js:66:11 in Promise
at node_modules@babelruntimehelpersregeneratorRuntime.js:147:15 in callInvokeWithMethodAndArg
at node_modules@babelruntimehelpersregeneratorRuntime.js:152:154 in _invoke
at node_modules@babelruntimehelpersregeneratorRuntime.js:238:57 in exports.async
at node_modulespromisesetimmediatecore.js:37:13 in tryCallOne
at node_modulespromisesetimmediatecore.js:123:24 in setImmediate$argument_0
at node_modulesreact-nativeLibrariesCoreTimersJSTimers.js:123:14 in _callTimer
at node_modulesreact-nativeLibrariesCoreTimersJSTimers.js:177:14 in _callImmediatesPass
at node_modulesreact-nativeLibrariesCoreTimersJSTimers.js:437:30 in callImmediates
at node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:388:6 in __callImmediates
at node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:132:6 in __guard$argument_0
at node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:365:10 in __guard
at node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:131:4 in flushedQueue
这是我的代码:
import React, { createContext, useContext } from 'react';
import * as Google from "expo-google-app-auth";
import{
GoogleAuthProvider,
onAuthStateChanged,
signInWithCredential,
signOut,
} from "@firebase/auth";
const AuthContext = createContext({});
const config = {
androidClientId: key,
iosClientId: key,
scopes: ['profile', 'email'],
permissions: ['public_profile', 'email', 'gender', 'location'],
}
export const AuthProvider = ({children}) => {
const signInWithGoogle = async () => {
await Google.logInAsync(config).then(async (logInResult) => {
if(logInResult.type === 'success'){
const { idToken, accessToken } = logInResult;
const credential = GoogleAuthProvider.credential(idToken, accessToken);
await signInWithCredential(auth, credential);
}
return Promise.reject();
});
};
return (
<AuthContext.Provider value ={{
user: null,
signInWithGoogle
}}
>
{children}
</AuthContext.Provider>
);
};
export default function useAuth() {
return useContext(AuthContext);
}
我在这里传递变量身份验证:
await signInWithCredential(auth, credential);
但是,Firebase 的身份验证页面上没有用户显示,并且我不断收到此警告。
感谢您的帮助!
正如错误所说,您正在尝试将auth
变量传递给signInWithCredential
,但您没有在任何地方声明或初始化它。如有关开始使用网页版 Firebase 身份验证的文档所示,您可以通过以下方式获取此信息:
import { getAuth } from "firebase/auth";
// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firebase Authentication and get a reference to the service
const auth = getAuth(app);
请参考 Firebase 提供的官方文档进行谷歌身份验证
Google 登录库提供了官方 Google 登录库的包装器,允许您创建凭据并登录 Firebase。
在将 Google 登录与 Firebase 配合使用时,大多数配置都已设置好,但您需要确保您的计算机 SHA1 密钥已配置为与 Android 配合使用。您可以在入门文档中了解如何生成密钥。
确保在 Firebase 控制台上启用了"Google"登录提供程序。
按照以下说明安装和设置google-signin
在触发登录请求之前,您必须使用所需的任何范围和webClientId
来初始化 Google SDK,可以在android/app/google-services.json
文件中作为client/oauth_client/client_id
属性找到(id 以.apps.googleusercontent.com
结尾)。确保选择带有client_type: 3
的client_id
import { GoogleSignin } from '@react-native-google-signin/google-signin';
GoogleSignin.configure({
webClientId: '',
});
初始化后,请将应用设置为使用signIn
方法触发与 Google 的登录请求。
import React from 'react';
import { Button } from 'react-native';
function GoogleSignIn() {
return (
<Button
title="Google Sign-In"
onPress={() => onGoogleButtonPress().then(() => console.log('Signed in with Google!'))}
/>
);
}
然后可以按如下方式实现onGoogleButtonPress
:
import auth from '@react-native-firebase/auth';
import { GoogleSignin } from '@react-native-google-signin/google-signin';
async function onGoogleButtonPress() {
// Get the users ID token
const { idToken } = await GoogleSignin.signIn();
// Create a Google credential with the token
const googleCredential = auth.GoogleAuthProvider.credential(idToken);
// Sign-in the user with the credential
return auth().signInWithCredential(googleCredential);
}
成功登录后,任何 onAuthStateChanged 侦听器都将使用用户的新身份验证状态触发。