diff options
| author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-09-09 10:20:31 +0530 | 
|---|---|---|
| committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-09-09 10:20:31 +0530 | 
| commit | 8f2180401fd05ba63633d9559dc156f5ca1573ba (patch) | |
| tree | f3d2f59b04d5940bc5028904d6ce9d551aace86a /user | |
| parent | cba9f19ba50c60576a5961e89c24caefca52d740 (diff) | |
logging in with either username or email
Diffstat (limited to 'user')
| -rw-r--r-- | user/service.go | 19 | ||||
| -rw-r--r-- | user/user.go | 5 | 
2 files changed, 21 insertions, 3 deletions
diff --git a/user/service.go b/user/service.go index 5e0632b..4544cb4 100644 --- a/user/service.go +++ b/user/service.go @@ -27,6 +27,25 @@ func (u *User) Create() error {  	return res.Error  } +func GetUserWithAccountName(user *User, accountName, method string) error { +	if method != "username" && method != "email" { +		return e.ErrInvalidLoginMethod +	} + +	res := db.Where(method + " = ?", accountName).Find(&user) + +	// TODO: handle potential errors +	if res.Error != nil { +		return res.Error +	} + +	if res.RowsAffected == 0 { +		return e.ErrNotFound +	} + +	return nil +} +  func GetUser(user *User, id uint) error {  	res := db.Find(&user, id) diff --git a/user/user.go b/user/user.go index 68ceb47..ee36e95 100644 --- a/user/user.go +++ b/user/user.go @@ -44,9 +44,8 @@ type User struct {  	IsVerified bool  } -func CheckPassword(id uint, pass string) error { -	var user User -	err := GetUser(&user, id) +func CheckPassword(user *User, accountName, method, pass string) error { +	err := GetUserWithAccountName(user, accountName, method)  	if err != nil {  		return err  	}  |