// loginHandleFunc - render template for login page func loginHandleFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { login := template.Must(getTemlates("login")) login.Execute(w, nil) } else { r.ParseForm() // logic part of log in user := &m.User{ Login: r.FormValue("login"), Password: r.FormValue("password"), } result, err := valid.ValidateStruct(user) if err == nil || !result { err := user.GetUserByLoginPass(user.Login, user.Password) if !err { sess := s.Instance(r) s.Clear(sess) sess.Values["id"] = user.ID err := sess.Save(r, w) if err != nil { log.Println(err) return } url, err := RedirectFunc("home") if err != nil { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } http.Redirect(w, r, url, http.StatusMovedPermanently) } else { http.Error(w, fmt.Sprintf("User %s not found", user.Login), http.StatusNotFound) } } } }
// signHandleFunc - render template for sign page func signHandleFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { templates, err := getTemlates("sign") sign := template.Must(templates, err) sign.Execute(w, nil) } else { r.ParseForm() // logic part of sign in user := &m.User{ Login: r.FormValue("login"), Email: r.FormValue("email"), Password: r.FormValue("password"), } result, err := valid.ValidateStruct(user) if err == nil || !result { sess := s.Instance(r) s.Clear(sess) err := user.CreateUser() if err != nil { http.Error(w, err.Error(), http.StatusNotAcceptable) return } sess.Values["id"] = user.ID err = sess.Save(r, w) if err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } url, err := RedirectFunc("home") if err != nil { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } http.Redirect(w, r, url, http.StatusMovedPermanently) } } }