简体   繁体   English

如何使用 Firebase Auth REST API 在模拟器中创建用户

[英]How to use the Firebase Auth REST API to create users in an Emulator

When reading the firebase emulator documentation, I saw that it mentioned a REST API that would expose endpoints to create users.在阅读 firebase 模拟器文档时,我看到它提到了一个 REST API会暴露端点来创建用户。 I have found the docs to create users but they don't seem to be for the emulator.我找到了创建用户的文档,但它们似乎不适用于模拟器。

I've tried using the sample requests for signup but with the auth emulator domain ( localhost:9099 ).我试过使用示例注册请求,但使用的是 auth 模拟器域 ( localhost:9099 )。 I've tried copying the example requests specifically for the auth emulator but changing it to mimic how the signup api looks like, none of this has worked however and they have just thrown 404s.我已经尝试专门为 auth 模拟器复制示例请求,但将其更改为模仿注册 api 的样子,但是这些都没有用,他们刚刚抛出 404。

Does anyone have a sample request that can be made to create a user in the firebase emulator?有没有人有可以在 firebase 模拟器中创建用户的示例请求? Or is this something we need to create client code for?还是我们需要为此创建客户端代码?

I think I figured it out.我想我明白了。 I followed the HTTP requests being made from the emulator web UI and this is what worked for me:我遵循了从模拟器 web UI 发出的 HTTP 请求,这对我有用:

await fetch(`http://localhost:9099/identitytoolkit.googleapis.com/v1/accounts:signUp?key=${config.firebase.apiKey}`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    email: 'user@test.com',
    password: 'pass1234',
    returnSecureToken: true,
  }),
});

And for completeness sake, here's how I clear out the users:为了完整起见,以下是我清除用户的方法:

await fetch(`http://localhost:9099/emulator/v1/projects/${config.firebase.projectId}/accounts`, {
  method: 'DELETE',
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM