Skip to content

Commit

Permalink
Enhance user login and validation with username support
Browse files Browse the repository at this point in the history
This commit updates the user login and validation functionalities to include username support. Modified the `Login` and `UserExist` methods to handle usernames. Adjusted the middleware to incorporate the new field and propagated changes throughout relevant service methods.
  • Loading branch information
mukulmantosh committed Aug 27, 2024
1 parent 3ab1cdc commit 630c612
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
3 changes: 2 additions & 1 deletion cmd/api/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

type UserClaims struct {
UserID int64 `json:"user_id"`
UserID int64 `json:"user_id"`
Name string `json:"name"`
jwt.RegisteredClaims
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/service/user/add_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (usrSrv *UsrService) Add(ctx context.Context, user *user.User) (bool, error) {
accountExists, _, _ := usrSrv.UserExist(ctx, user.Email, false)
accountExists, _, _, _ := usrSrv.UserExist(ctx, user.Email, false)
if accountExists {
return false, errors.New("the user you are trying to register already exists")
} else {
Expand Down
16 changes: 8 additions & 8 deletions pkg/service/user/login_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"time"
)

func (usrSrv *UsrService) Login(ctx context.Context, userID int64) (string, error) {
func (usrSrv *UsrService) Login(_ context.Context, userID int64, Name string) (string, error) {

claims := middleware.UserClaims{UserID: userID,
claims := middleware.UserClaims{UserID: userID, Name: Name,
RegisteredClaims: jwt.RegisteredClaims{

ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * time.Duration(1))),
Expand All @@ -24,27 +24,27 @@ func (usrSrv *UsrService) Login(ctx context.Context, userID int64) (string, erro
return token.SignedString([]byte(os.Getenv("JWT_SECRET_KEY")))
}

func (usrSrv *UsrService) UserExist(ctx context.Context, email string, recordRequired bool) (bool, int64, error) {
func (usrSrv *UsrService) UserExist(ctx context.Context, email string, recordRequired bool) (bool, int64, string, error) {
count, err := usrSrv.db.Count(ctx, "users", "COUNT(*)", "email", email)
if err != nil {
slog.Info("UserService.UserExist::Error %v", err)

Check failure on line 30 in pkg/service/user/login_user.go

View workflow job for this annotation

GitHub Actions / Run Tests

slog.Info arg "err" should be a string or a slog.Attr (possible missing key or value)
return false, 0, err
return false, 0, "", err
}
if count == 0 {
return false, 0, nil
return false, 0, "", nil
}

if recordRequired == true {
// Fetch User Detail
var accountInfo user.User
err = usrSrv.db.Select(ctx, &accountInfo, "email", email)
if err != nil {
return false, 0, err
return false, 0, "", err
}
return true, accountInfo.ID, nil
return true, accountInfo.ID, accountInfo.Name, nil
}

return true, 0, nil
return true, 0, "", nil
}

func (usrSrv *UsrService) ValidatePassword(ctx context.Context, userInput *user.LoginUser) (bool, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/service/user/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"errors"
)

func ValidateAccount(login func(ctx context.Context, userID int64) (string, error),
accountExists func(ctx context.Context, email string, recordRequired bool) (bool, int64, error),
func ValidateAccount(login func(ctx context.Context, userID int64, userName string) (string, error),
accountExists func(ctx context.Context, email string, recordRequired bool) (bool, int64, string, error),
validatePassword func(ctx context.Context, user *user.LoginUser) (bool, error)) func(ctx context.Context, user *user.LoginUser) (string, error) {
return func(ctx context.Context, user *user.LoginUser) (string, error) {
exists, userDetail, err := accountExists(ctx, user.Email, true)
exists, userId, name, err := accountExists(ctx, user.Email, true)
if err != nil {
return "", err
}
Expand All @@ -23,6 +23,6 @@ func ValidateAccount(login func(ctx context.Context, userID int64) (string, erro
return "", err
}

return login(ctx, userDetail)
return login(ctx, userId, name)
}
}

0 comments on commit 630c612

Please sign in to comment.