reactjs - 为什么 Firebase 身份验证模拟器使用生产?
问题描述
我正在尝试使用 Firebase 身份验证模拟器,我发现即使模拟器运行并启用了身份验证,也会发生在生产环境中。
我有以下内容firebase/auth.js
// imports and stuff...
firebase.initializeApp(firebaseConfig);
export const firebaseAuth = firebase.auth()
if (DEBUG_MODE) {
firebaseAuth.useEmulator('http://localhost:9099');
}
// declare a class to make mocking easier...maybe I just don't know how to properly mock constants?
export class FirebaseAuth {
static getAuth() {
return firebaseAuth
}
}
然后,我有一个 SignIn 组件,就像在https://github.com/firebase/firebaseui-web-react#using-styledfirebaseauth-with-local-stateSignInScreen
中找到的示例一样- 尽管一个区别是该组件使用的是从上面看到的导出。firebaseAuth
auth.js
import React from 'react';
import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth';
import { uiConfig, FirebaseAuth } from '../firebase/auth';
import { useQuery } from '../hooks';
function SignIn() {
let params = useQuery()
let redirectUrl = params.get('redirect_url');
const config = Object.assign({}, uiConfig, {signInSuccessUrl: redirectUrl})
return (
<div>
<p>Please sign-in:</p>
<StyledFirebaseAuth uiConfig={config} firebaseAuth={FirebaseAuth.getAuth()} />
</div>
);
}
export default SignIn
当我在本地运行我的 react 应用程序时,我看到 firebase 正在根据我的应用程序底部显示的此消息检测模拟器:
但是,我发现当我尝试登录生产实例时使用了 firebase auth。新用户是在https://console.firebase.google.com/u/0/project/sagereact/authentication/users而不是在我的本地模拟器中创建的。
此外,当我对我的 web 应用程序进行硬刷新时,我会看到以下日志行,这也会让我退出:
⚠ Received a signed JWT. Auth Emulator does not validate JWTs and IS NOT SECURE
我已经验证我的模拟器正在运行:
❮❮❮ firebase emulators:start
i emulators: Starting emulators: auth, functions, firestore, database, hosting, pubsub
...stuff
┌────────────────────────────────────────────────────────────────┐
│ ✔ All emulators ready! View status and logs at localhost:4000 │
└────────────────────────────────────────────────────────────────┘
┌────────────────┬────────────────┬──────────────────────────┐
│ Emulator │ Host:Port │ View in Emulator UI │
├────────────────┼────────────────┼──────────────────────────┤
│ Authentication │ localhost:9099 │ localhost:4000/auth │
├────────────────┼────────────────┼──────────────────────────┤
│ Functions │ localhost:5001 │ localhost:4000/functions │
├────────────────┼────────────────┼──────────────────────────┤
│ Firestore │ localhost:8080 │ localhost:4000/firestore │
├────────────────┼────────────────┼──────────────────────────┤
│ Database │ localhost:9000 │ localhost:4000/database │
├────────────────┼────────────────┼──────────────────────────┤
│ Hosting │ localhost:5055 │ n/a │
├────────────────┼────────────────┼──────────────────────────┤
│ Pub/Sub │ localhost:8085 │ n/a │
└────────────────┴────────────────┴──────────────────────────┘
Other reserved ports: 4400, 4500
Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
和
我怎样才能弄清楚为什么 firebase 不使用我的本地模拟器进行身份验证?
解决方案
您需要从您的客户端代码设置 Auth 模拟器。这是使用 Flutter 在 Dart 中的等价物:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
var app = await Firebase.initializeApp();
if (USE_FIRESTORE_EMULATOR) {
FirebaseFirestore.instance.settings = Settings(host: 'localhost:8080', sslEnabled: false, persistenceEnabled: false);
FirebaseFunctions.instance.useFunctionsEmulator(origin: 'http://localhost:5001');
FirebaseAuth.instanceFor(app: app).useEmulator('http://localhost:9099');
}
查看firebaseui-web-react
此拉取请求(https://github.com/firebase/firebaseui-web-react/pull/125)并确保您使用的是正确的版本。