Beispiel #1
0
// Profile handler
func ProfileHandler(res http.ResponseWriter, req *http.Request) {

	var errs []string

	if req.Method == "POST" {
		// Validation comes here
		pass1 := req.FormValue("password1")
		pass2 := req.FormValue("password2")
		errs = validateProfile(pass1, pass2)
		if errs == nil {
			// Saving the form
			du := datastore.User{
				Email:     session.GetUser(req).Email,
				Password:  Encrypt(pass1),
				FirstName: req.FormValue("fname"),
				LastName:  req.FormValue("lname"),
			}
			// Store the user
			util.SaveUser(req, du)

			// Redirect the user to front page
			http.Redirect(res, req, URL_ROOT, http.StatusFound)
		}
	}

	//Parsing the template
	tpl := template.Must(template.ParseFiles("template/profile.html"))
	// Fetching user's data
	pt := ProfileTemp{
		User:   util.GetUser(req),
		Errors: errs,
	}
	err := tpl.Execute(res, GetAPlusTemplateHeader(req, pt))
	log.LogError(err)
}
Beispiel #2
0
// Signup handler
func SignupHandler(res http.ResponseWriter, req *http.Request) {

	if req.Method != "POST" && session.GetUser(req).Email != "" {
		// If user is already in session, we want to redirect him to front page.
		http.Redirect(res, req, URL_ROOT, http.StatusFound)
		return
	}

	var errs []string

	if req.Method == "POST" {
		email := req.FormValue("email")
		pass1 := req.FormValue("password1")
		pass2 := req.FormValue("password2")
		// Validatation comes here
		errs = validate(email, pass1, pass2, req)

		if errs == nil {
			// Create the user
			du := datastore.User{
				Email:     email,
				Password:  Encrypt(pass1),
				FirstName: req.FormValue("fname"),
				LastName:  req.FormValue("lname"),
			}

			// Store the user
			util.SaveUser(req, du)

			// Create session
			session.CreateSession(&res, req, session.User{Email: du.Email})

			// Redirect the user to front page
			http.Redirect(res, req, URL_ROOT, http.StatusFound)
		}
	}

	//Parsing the template
	tpl := template.Must(template.ParseFiles("template/signup.html"))
	err := tpl.Execute(res, errs)
	log.LogError(err)
}