英文:
How to create a user in Firebase using golang?
问题
有没有办法在Golang中使用用户名、电子邮件和密码创建Firebase用户。使用JavaScript可以使用createUserWithEmailAndPassword(email, password)
来创建用户,但我需要在Golang中实现相同的功能。是否有可用的包或函数?我正在使用firego与Firebase建立连接。
英文:
Is there any way to create a user in Firebase using Userame, Email and Password with Golang. A user can be created with Javascript using createUserWithEmailAndPassword(email, password)
But I need the same with Golang. Is there a package or function available? I am using firego to connect with Firebase.
答案1
得分: 20
最近,Google将Go语言添加到了使用Firebase Admin SDK支持的编程语言列表中,以便进行Firebase身份验证。
创建用户的代码示例:
params := (&auth.UserToCreate{}).
Email("user@example.com").
EmailVerified(false).
PhoneNumber("+1234567890").
Password("secretPassword").
DisplayName("Donald Drump").
PhotoURL("http://www.example.com/12345678/photo.png").
Disabled(false)
u, err := client.CreateUser(context.Background(), params)
if err != nil {
log.Fatalf("error creating user: %v\n", err)
}
log.Printf("Successfully created user: %v\n", u)
如果你想使用自己的用户ID创建用户,而不是使用Firebase自动生成的ID,可以使用以下代码:
params := (&auth.UserToCreate{}).
UID(uid).
Email("user@example.com").
PhoneNumber("+1234567890")
u, err := client.CreateUser(context.Background(), params)
if err != nil {
log.Fatalf("error creating user: %v\n", err)
}
log.Printf("User created successfully: %v\n", u)
更新用户的代码示例:
params := (&auth.UserToUpdate{}).
Email("user@example.com").
EmailVerified(true).
PhoneNumber("+1234567890").
Password("newPassword").
DisplayName("Donald Drump").
PhotoURL("http://www.example.com/12345678/photo.png").
Disabled(true)
u, err := client.UpdateUser(context.Background(), uid, params)
if err != nil {
log.Fatalf("error updating user: %v\n", err)
}
log.Printf("Successfully updated user: %v\n", u)
你可以在这里查看更多关于Firebase Admin SDK的文档。
英文:
Recently Google added Go Lang to their list of programming languages that are supported by Firebase Authentication using Firebase Admin SDK.
To create a user:
params := (&auth.UserToCreate{}).
Email("user@example.com").
EmailVerified(false).
PhoneNumber("+1234567890").
Password("secretPassword").
DisplayName("Donald Drump").
PhotoURL("http://www.example.com/12345678/photo.png").
Disabled(false)
u, err := client.CreateUser(context.Background(), params)
if err != nil {
log.Fatalf("error creating user: %v\n", err)
}
log.Printf("Successfully created user: %v\n", u)
if you want to create a user with your own user ID and don't want an automated generated ID by Firebase then:
params := (&auth.UserToCreate{}).
UID(uid).
Email("user@example.com").
PhoneNumber("+1234567890")
u, err := client.CreateUser(context.Background(), params)
if err != nil {
log.Fatalf("error creating user: %v\n", err)
}
log.Printf("User created successfully : %v\n", u)
to update a user:
params := (&auth.UserToUpdate{}).
Email("user@example.com").
EmailVerified(true).
PhoneNumber("+1234567890").
Password("newPassword").
DisplayName("Donald Drump").
PhotoURL("http://www.example.com/12345678/photo.png").
Disabled(true)
u, err := client.UpdateUser(context.Background(), uid, params)
if err != nil {
log.Fatalf("error updating user: %v\n", err)
}
log.Printf("Successfully updated user: %v\n", u)
答案2
得分: 2
Firebase已经为Go添加了一个Admin SDK。有关更多信息,请参阅Mujtaba的答案:https://stackoverflow.com/a/47889860
以前的答案:
<strike>没有适用于Go的Firebase SDK。但是,Firebase的某些部分具有REST API,允许您从几乎任何平台/技术使用这些功能。 Firebase数据库就是其中之一,而Firego库是针对Go开发人员的Firebase数据库REST API的包装器。
不幸的是,Firebase身份验证中没有用于创建用户的REST API。因此,无法通过Firego或通过Go代码中的公共REST API创建用户。
最简单的解决方案是在您控制的应用服务器上创建一个REST端点,然后使用Firebase Admin SDK来创建用户。</strike>
英文:
Firebase has added an Admin SDK for Go. See Mujtaba's answer for more information: https://stackoverflow.com/a/47889860
Previous answer:
<strike>There is no Firebase SDK for Go. But certain parts of Firebase have a REST API that allows you to use those features from almost any platform/technology. The Firebase Database is one of those features and the Firego library is a wrapper around the REST API of the Firebase Database for Go developers.
Unfortunately there is no REST API for creating users in Firebase Authentication. So it won't be possible to create users through Firego or through a public REST API from your Go code.
The simplest solution would be to create a REST endpoint on a app server you control, where you then use the Firebase Admin SDK to create the user.</strike>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论