Ejemplo n.º 1
0
// Login handler
func LoginHandler(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
	}

	invalidUser := false

	if req.Method == "POST" {
		email := req.FormValue("username")
		if isValidUser(email, req.FormValue("password"), req) {
			// Set the session
			session.CreateSession(&res, req, session.User{Email: email})
			// Redirecting the user to profile page.
			http.Redirect(res, req, URL_ROOT, http.StatusFound)
			return
		} else {
			// Invalid User
			invalidUser = true
		}
	}

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

}
Ejemplo n.º 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"),
			}

			// 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)
}