package password import ( "clapclap/internal/dtos" "clapclap/internal/ginctx" "clapclap/internal/models" "strings" "github.com/gin-gonic/gin" "golang.org/x/crypto/bcrypt" ) func (a *Authenticator) Default() { a.getUserID = func(c *gin.Context) error { // get the request parameter from the context req, err := ginctx.GetDto[dtos.LoginRequest](c) if err != nil { return err } // get the id from the database repo, err := ginctx.GetRepo(c, "repo") if err != nil { return err } var user *models.User if strings.Contains(req.Identifier, "@") { user, err = repo.FindUserByEmail(req.Identifier) } else { user, err = repo.FindUserByUsername(req.Identifier, nil) } if err != nil { return err } // sets the userID in the context c.Set("userID", user.ID) return nil } a.getHashedPassword = func(c *gin.Context) (string, error) { // get the request parameter from the context req, err := ginctx.GetDto[dtos.LoginRequest](c) if err != nil { return "", err } return req.Password, nil } a.getExpectedHash = func(c *gin.Context) (string, error) { // get userID from context uid, err := ginctx.GetUUID(c, "userID") if err != nil { return "", err } // get password hash from db repo, err := ginctx.GetRepo(c, "repo") if err != nil { return "", err } user, err := repo.GetUserByID(uid, nil) if err != nil { return "", err } return user.HashedPassword, nil } a.compare = func(actual string, expected string) error { return bcrypt.CompareHashAndPassword([]byte(expected), []byte(actual)) } }