dongzhuo3059
2017-01-17 10:52
浏览 38
已采纳

如何使用golang在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.

图片转代码服务由CSDN问答提供 功能建议

有什么方法可以在Firebase中使用Userame,电子邮件和Golang密码创建用户。 可以使用 createUserWithEmailAndPassword(email,password)用Javascript创建用户,但Golang也需要相同的用户。 是否有可用的软件包或功能? 我正在使用firego连接Firebase。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douye7033 2017-01-17 14:58
    已采纳

    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.

    打赏 评论
  • doubaoxue5788 2017-12-19 15:07

    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
    ", err)
    }
    log.Printf("Successfully created user: %v
    ", 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
    ", err)
    }
    log.Printf("User created successfully : %v
    ", 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
    ", err)
    }
    log.Printf("Successfully updated user: %v
    ", u)
    
    打赏 评论

相关推荐 更多相似问题