From 8f2180401fd05ba63633d9559dc156f5ca1573ba Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 9 Sep 2023 10:20:31 +0530 Subject: logging in with either username or email --- user/service.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'user/service.go') 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) -- cgit v1.2.3