From 794a9c07ffc7dc72993f5240796dfe643d73ed82 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 10 Sep 2023 13:49:34 +0530 Subject: validating email upon user sign up --- user/user.go | 2 +- user/validators.go | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'user') diff --git a/user/user.go b/user/user.go index ee36e95..132acb6 100644 --- a/user/user.go +++ b/user/user.go @@ -40,7 +40,7 @@ type User struct { gorm.Model Username string Email string - Password string + Password string `json:"-"` IsVerified bool } diff --git a/user/validators.go b/user/validators.go index 88239c6..647da06 100644 --- a/user/validators.go +++ b/user/validators.go @@ -19,6 +19,7 @@ package user import ( "strings" + "net/mail" e "errors" "github.com/spf13/viper" "vidhukant.com/openbills/errors" @@ -73,9 +74,15 @@ func (u *User) validate() error { u.IsVerified = false // TODO: validate username length and stuff - // TODO: validate if email is valid var err error + + // validate email + _, err = mail.ParseAddress(u.Email) + if err != nil { + return errors.ErrInvalidEmail + } + for _, i := range [][]string{{"username", u.Username}, {"email", u.Email}} { err = validateUserField(i[0], i[1]) if err != nil { -- cgit v1.2.3