Skip to content

Commit

Permalink
chore: signup user functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
puni9869 committed Feb 18, 2024
1 parent f1bc9cd commit c4584b5
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 22 deletions.
8 changes: 2 additions & 6 deletions internal/signup/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,14 @@ type signupClient struct {
func (s *signupClient) Register(c *gin.Context, user models.User) error {
ctx := middlewares.GetContext(c)
err := s.db.Create(&user).Error
if errors.Is(err, gorm.ErrDuplicatedKey) {
if err != nil {
s.log.WithError(err).Error("failed to create user")
ctx["Email_HasError"] = true
ctx["Email_Err"] = ErrDuplicateEmail.Error()
ctx["Email_Error"] = ErrDuplicateEmail.Error()
ctx["Password_HasError"] = false
ctx["ConfirmPassword_HasError"] = false
return ErrDuplicateEmail
}
if err != nil {
s.log.WithError(err).Error("failed to create user")
return err
}
s.log.Infoln("user is created successfully")
return nil
}
Expand Down
1 change: 0 additions & 1 deletion pkg/formbinding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func Errorf(data gin.H, errs validator.ValidationErrors) map[string]any {
if len(errs) == 0 {
return nil
}
data["HasError"] = false
var f Field = new(FieldErrors)
for _, err := range errs {
data[err.Field()+"_Error"] = fmt.Sprintf("%s%s", err.Field(), f.Error(err.Tag()))
Expand Down
17 changes: 5 additions & 12 deletions server/auth/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func SignupPost(signUp signup.Service) gin.HandlerFunc {
ctx["email"] = email
ctx["password"] = password
ctx["confirm_password"] = confirmPassword

// password check
if ctx["Email_HasError"] == false || field.IsValid(password) == false {

Check failure on line 48 in server/auth/signup.go

View workflow job for this annotation

GitHub Actions / checks

S1002: should omit comparison to bool constant, can be simplified to `!field.IsValid(password)` (gosimple)
ctx["Password_HasError"] = true
Expand All @@ -65,18 +66,10 @@ func SignupPost(signUp signup.Service) gin.HandlerFunc {
h.Write([]byte(password))
bs := h.Sum(nil)

user := models.User{
FirstName: "",
LastName: "",
DisplayName: "",
Password: fmt.Sprintf("%x", bs),
EmailVerifyHash: "",
IsEmailVerified: false,
IsActive: false,
IsProfilePublic: false,
Email: email,
}
if err := signUp.Register(c, user); err != nil {
user := models.User{Password: fmt.Sprintf("%x", bs), Email: email}

if err := signUp.Register(c, user); err == nil {
ctx["HasError"] = true
log.WithError(err).Error("error in registering user")
}
}
Expand Down
6 changes: 4 additions & 2 deletions server/middlewares/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ func GetContext(c *gin.Context) gin.H {
// BindForm binding a form obj to a handler's context data
func BindForm[T any](_ T) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set(ContextKey, make(map[string]any))
data := make(map[string]any)
data["HasError"] = false
c.Set(ContextKey, data)
var theObj = new(T) // create a new form obj for every request but not use obj directly
if errs := c.ShouldBindWith(theObj, binding.Form); errs != nil {
data := formbinding.Errorf(make(gin.H), errs.(validator.ValidationErrors))
data = formbinding.Errorf(make(gin.H), errs.(validator.ValidationErrors))
c.Set(ContextKey, data)
}
c.Set(formKey, theObj)
Expand Down
2 changes: 1 addition & 1 deletion templates/auth/signup.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
value="{{.email}}"
class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 px-3 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
/>
<p class="text-xs text-red-600 {{if .Email_HasError}}visible{{else}}hidden{{end}}">{{.Email_Error}}</p>
<p class="pt-1 text-xs text-red-600 {{if .Email_HasError}}visible{{else}}hidden{{end}}">{{.Email_Error}}</p>
</div>
</div>

Expand Down

0 comments on commit c4584b5

Please sign in to comment.