Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
Add Command to Verify or Unverify Users Email
Browse files Browse the repository at this point in the history
This command allows you to set the status of the email address for one
or more users account on Firebase Auth.

Closes #19
  • Loading branch information
mainawycliffe committed Aug 9, 2020
1 parent fda70f9 commit ee91f38
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
43 changes: 43 additions & 0 deletions cmd/verifyEmail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"context"
"os"

"github.com/mainawycliffe/kamanda/firebase/auth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/spf13/cobra"
)

var verifyEmailCmd = &cobra.Command{
Use: "isEmailVerified",
Aliases: []string{"is-email-verified"},
Short: "Manually verify or un-verify a users email address.",
Example: "kamanda users set isEmailVerified --status=false [UID] [UID]",
Run: func(cmd *cobra.Command, args []string) {
emailVerifiedStatus, _ := cmd.Flags().GetBool("status")
hasError := false
for _, v := range args {
updatePassword := &auth.FirebaseUser{
ShouldUpdateEmailVerified: true,
EmailVerified: emailVerifiedStatus,
}
user, err := auth.UpdateFirebaseUser(context.Background(), v, updatePassword)
if err != nil {
hasError = true
utils.StdOutError(os.Stderr, "Error updating user %s email verified status\n", v)
continue
}
utils.StdOutSuccess(os.Stdout, "Email verified status for user %s [%s] has been set to %s\n", user.UID, user.Email, emailVerifiedStatus)
}
if hasError {
os.Exit(1)
}
os.Exit(0)
},
}

func init() {
setCmd.AddCommand(verifyEmailCmd)
verifyEmailCmd.Flags().BoolP("status", "s", false, "The email verified value to set to")
}
26 changes: 17 additions & 9 deletions firebase/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (
)

type FirebaseUser struct {
UID string `json:"uid" yaml:"uid"`
Email string `json:"email" yaml:"email"`
EmailVerified bool `json:"email_verified" yaml:"email_verified"`
PhoneNumber string `json:"phone" yaml:"phone"`
Password string `json:"password" yaml:"password"`
DisplayName string `json:"name" yaml:"name" `
Disabled bool `json:"disabled" yaml:"disabled"`
PhotoURL string `json:"photo_url" yaml:"photo_url"`
CustomClaims []FirebaseUserCustomClaims `json:"custom_claims" yaml:"custom_claims"`
UID string `json:"uid" yaml:"uid"`
Email string `json:"email" yaml:"email"`
ShouldUpdateEmailVerified bool `json:"-" yaml:"-"`
EmailVerified bool `json:"email_verified" yaml:"email_verified"`
PhoneNumber string `json:"phone" yaml:"phone"`
Password string `json:"password" yaml:"password"`
DisplayName string `json:"name" yaml:"name" `
ShouldUpdateDisabled bool `json:"-" yaml:"-"`
Disabled bool `json:"disabled" yaml:"disabled"`
PhotoURL string `json:"photo_url" yaml:"photo_url"`
CustomClaims []FirebaseUserCustomClaims `json:"custom_claims" yaml:"custom_claims"`
}

type FirebaseUserCustomClaims struct {
Expand Down Expand Up @@ -75,6 +77,12 @@ func UpdateFirebaseUser(ctx context.Context, UID string, user *FirebaseUser) (*a
if user.PhotoURL != "" {
params = params.PhotoURL(user.PhotoURL)
}
if user.ShouldUpdateDisabled {
params = params.Disabled(user.Disabled)
}
if user.ShouldUpdateEmailVerified {
params = params.EmailVerified(user.EmailVerified)
}
client, err := firebase.Auth(ctx, "", "")
if err != nil {
return nil, fmt.Errorf("Error authenticating firebase account: %w", err)
Expand Down

0 comments on commit ee91f38

Please sign in to comment.