This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |