github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume4/section3/gopherface/handlers/signup.go (about) 1 package handlers 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 8 "github.com/EngineerKamesh/gofullstack/volume4/section3/gopherface/common" 9 "github.com/EngineerKamesh/gofullstack/volume4/section3/gopherface/models" 10 11 "github.com/EngineerKamesh/gofullstack/volume4/section3/gopherface/validationkit" 12 ) 13 14 type SignUpForm struct { 15 PageTitle string 16 FieldNames []string 17 Fields map[string]string 18 Errors map[string]string 19 } 20 21 // DisplaySignUpForm displays the Sign Up form 22 func DisplaySignUpForm(w http.ResponseWriter, r *http.Request, s *SignUpForm) { 23 RenderTemplate(w, WebAppRoot+"/templates/signupform.html", s) 24 } 25 26 func DisplayConfirmation(w http.ResponseWriter, r *http.Request, s *SignUpForm) { 27 RenderTemplate(w, WebAppRoot+"/templates/signupconfirmation.html", s) 28 } 29 30 func PopulateFormFields(r *http.Request, s *SignUpForm) { 31 32 for _, fieldName := range s.FieldNames { 33 s.Fields[fieldName] = r.FormValue(fieldName) 34 } 35 36 } 37 38 // ValidateSignUpForm validates the Sign Up form's fields 39 func ValidateSignUpForm(w http.ResponseWriter, r *http.Request, s *SignUpForm, e *common.Env) { 40 41 PopulateFormFields(r, s) 42 // Check if username was filled out 43 if r.FormValue("username") == "" { 44 s.Errors["usernameError"] = "The username field is required." 45 } 46 47 // Check if first name was filled out 48 if r.FormValue("firstName") == "" { 49 s.Errors["firstNameError"] = "The first name field is required." 50 } 51 52 // Check if last name was filled out 53 if r.FormValue("lastName") == "" { 54 s.Errors["lastNameError"] = "The last name field is required." 55 } 56 57 // Check if e-mail address was filled out 58 if r.FormValue("email") == "" { 59 s.Errors["emailError"] = "The e-mail address field is required." 60 } 61 62 // Check if e-mail address was filled out 63 if r.FormValue("password") == "" { 64 s.Errors["passwordError"] = "The password field is required." 65 } 66 67 // Check if e-mail address was filled out 68 if r.FormValue("confirmPassword") == "" { 69 s.Errors["confirmPasswordError"] = "The confirm password field is required." 70 } 71 72 // Check username syntax 73 if validationkit.CheckUsernameSyntax(r.FormValue("username")) == false { 74 75 usernameErrorMessage := "The username entered has an improper syntax." 76 if _, ok := s.Errors["usernameError"]; ok { 77 s.Errors["usernameError"] += " " + usernameErrorMessage 78 } else { 79 s.Errors["usernameError"] = usernameErrorMessage 80 } 81 } 82 83 // Check e-mail address syntax 84 if validationkit.CheckEmailSyntax(r.FormValue("email")) == false { 85 emailErrorMessage := "The e-mail address entered has an improper syntax." 86 if _, ok := s.Errors["usernameError"]; ok { 87 s.Errors["emailError"] += " " + emailErrorMessage 88 } else { 89 s.Errors["emailError"] = emailErrorMessage 90 } 91 } 92 93 // Check if password and confirm password field values match 94 if r.FormValue("password") != r.FormValue("confirmPassword") { 95 s.Errors["confirmPasswordError"] = "The password and confirm pasword fields do not match." 96 } 97 98 if len(s.Errors) > 0 { 99 DisplaySignUpForm(w, r, s) 100 } else { 101 ProcessSignUpForm(w, r, s, e) 102 } 103 104 } 105 106 // ProcessSignUpForm 107 func ProcessSignUpForm(w http.ResponseWriter, r *http.Request, s *SignUpForm, e *common.Env) { 108 109 u := models.NewUser(r.FormValue("username"), r.FormValue("firstName"), r.FormValue("lastName"), r.FormValue("email"), r.FormValue("password")) 110 //fmt.Println("user: ", u) 111 err := e.DB.CreateUser(u) 112 113 if err != nil { 114 log.Print(err) 115 } 116 117 user, err := e.DB.GetUser("EngineerKamesh") 118 if err != nil { 119 log.Print(err) 120 } else { 121 fmt.Printf("Fetch User Result: %+v\n", user) 122 } 123 124 // Display form confirmation message 125 DisplayConfirmation(w, r, s) 126 127 } 128 129 func SignUpHandler(e *common.Env) http.Handler { 130 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 131 132 s := SignUpForm{} 133 s.FieldNames = []string{"username", "firstName", "lastName", "email"} 134 s.Fields = make(map[string]string) 135 s.Errors = make(map[string]string) 136 s.PageTitle = "Sign Up" 137 138 switch r.Method { 139 140 case "GET": 141 DisplaySignUpForm(w, r, &s) 142 case "POST": 143 ValidateSignUpForm(w, r, &s, e) 144 default: 145 DisplaySignUpForm(w, r, &s) 146 } 147 148 }) 149 }