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

Commit

Permalink
feat: add method to find user
Browse files Browse the repository at this point in the history
can find user by id, email or phone
  • Loading branch information
mainawycliffe committed Feb 9, 2020
1 parent b28170a commit a5ba18a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions firebase/auth/findUser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package auth

import (
"context"
"fmt"

"firebase.google.com/go/auth"
"github.com/mainawycliffe/kamanda/firebase"
)

type FindUserCriteria int

const (
ByUserUIDCriteria FindUserCriteria = 0
ByUserEmailCriteria FindUserCriteria = 1
ByUserPhoneCriteria FindUserCriteria = 2
)

//IsValid check if user criteria is valid i.e by uid, email or phone
func (c FindUserCriteria) IsValid() bool {
if c != ByUserUIDCriteria && c != ByUserEmailCriteria && c != ByUserPhoneCriteria {
return false
}
return true
}

// GetUser find a user by either uid, email or phone number
func GetUser(ctx context.Context, query string, criteria FindUserCriteria, includeCustomClaims bool) (*auth.UserRecord, error) {
if isValid := criteria.IsValid(); !isValid {
return nil, fmt.Errorf("Invalid find user criteria.")
}
client, err := firebase.Auth(ctx, "")
if err != nil {
return nil, fmt.Errorf("Error authenticating firebase account: %w", err)
}
var user *auth.UserRecord
var getUserErr error
switch criteria {
case ByUserEmailCriteria:
user, getUserErr = client.GetUserByEmail(ctx, query)
case ByUserPhoneCriteria:
user, getUserErr = client.GetUserByPhoneNumber(ctx, query)
default: // by UID
user, getUserErr = client.GetUser(ctx, query)
}
if getUserErr != nil {
return nil, fmt.Errorf("Error while find user: %w", getUserErr)
}
return user, nil
}

0 comments on commit a5ba18a

Please sign in to comment.