github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume4/section3/gopherface/handlers/login.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/common/authenticate" 10 "github.com/EngineerKamesh/gofullstack/volume4/section3/gopherface/common/utility" 11 "github.com/EngineerKamesh/gofullstack/volume4/section3/gopherface/validationkit" 12 ) 13 14 type LoginForm struct { 15 PageTitle string 16 FieldNames []string 17 Fields map[string]string 18 Errors map[string]string 19 } 20 21 // DisplayLoginForm displays the Sign Up form 22 func DisplayLoginForm(w http.ResponseWriter, r *http.Request, l *LoginForm) { 23 fmt.Println("reached display login form") 24 RenderTemplate(w, WebAppRoot+"/templates/loginform.html", l) 25 } 26 27 func PopulateLoginFormFields(r *http.Request, l *LoginForm) { 28 29 for _, fieldName := range l.FieldNames { 30 l.Fields[fieldName] = r.FormValue(fieldName) 31 } 32 33 } 34 35 // ValidateLoginForm validates the Sign Up form's fields 36 func ValidateLoginForm(w http.ResponseWriter, r *http.Request, l *LoginForm, e *common.Env) { 37 38 PopulateLoginFormFields(r, l) 39 // Check if username was filled out 40 if r.FormValue("username") == "" { 41 l.Errors["usernameError"] = "The username field is required." 42 } 43 44 // Check if e-mail address was filled out 45 if r.FormValue("password") == "" { 46 l.Errors["passwordError"] = "The password field is required." 47 } 48 49 // Check username syntax 50 if validationkit.CheckUsernameSyntax(r.FormValue("username")) == false { 51 52 usernameErrorMessage := "The username entered has an improper syntax." 53 if _, ok := l.Errors["usernameError"]; ok { 54 l.Errors["usernameError"] += " " + usernameErrorMessage 55 } else { 56 l.Errors["usernameError"] = usernameErrorMessage 57 } 58 } 59 60 if len(l.Errors) > 0 { 61 DisplayLoginForm(w, r, l) 62 } else { 63 ProcessLoginForm(w, r, l, e) 64 } 65 66 } 67 68 // ProcessLoginForm 69 func ProcessLoginForm(w http.ResponseWriter, r *http.Request, l *LoginForm, e *common.Env) { 70 71 authResult := authenticate.VerifyCredentials(e, r.FormValue("username"), r.FormValue("password")) 72 fmt.Println("auth result: ", authResult) 73 74 // Successful login, let's create a cookie for the user and redirect them to the feed route 75 if authResult == true { 76 77 sessionID := utility.GenerateUUID() 78 fmt.Println("sessid: ", sessionID) 79 u, err := e.DB.GetUser(r.FormValue("username")) 80 if err != nil { 81 log.Print("Encountered error when attempting to fetch user record: ", err) 82 http.Redirect(w, r, "/login", 302) 83 return 84 } 85 86 err = authenticate.CreateSecureCookie(u, sessionID, w, r) 87 if err != nil { 88 log.Print("Encountered error when attempting to create secure cookie: ", err) 89 http.Redirect(w, r, "/login", 302) 90 return 91 92 } 93 94 err = authenticate.CreateUserSession(u, sessionID, w, r) 95 if err != nil { 96 log.Print("Encountered error when attempting to create user session: ", err) 97 http.Redirect(w, r, "/login", 302) 98 return 99 100 } 101 102 http.Redirect(w, r, "/feed", 302) 103 104 } else { 105 106 l.Errors["usernameError"] = "Invalid login." 107 DisplayLoginForm(w, r, l) 108 109 } 110 111 } 112 113 func LoginHandler(e *common.Env) http.Handler { 114 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 115 116 l := LoginForm{} 117 l.FieldNames = []string{"username"} 118 l.Fields = make(map[string]string) 119 l.Errors = make(map[string]string) 120 l.PageTitle = "Log In" 121 122 switch r.Method { 123 124 case "GET": 125 DisplayLoginForm(w, r, &l) 126 case "POST": 127 ValidateLoginForm(w, r, &l, e) 128 default: 129 DisplayLoginForm(w, r, &l) 130 } 131 132 }) 133 }