Beispiel #1
0
// Sign Up form submit route. Registers new user or shows Sign Up route with appropriate messages set in session
func SignUpPost(c web.C, r *http.Request) (string, int) {
	email, password := r.FormValue("email"), r.FormValue("password")

	session := helpers.GetSession(c)
	database := helpers.GetDatabase(c)

	user, err := models.GetUserByEmail(database, email)

	if user != nil {
		session.AddFlash("User exists", "auth")
		return SignUp(c, r)
	}

	user = &models.User{
		Name:  email,
		Email: email,
	}
	err = user.HashPassword(password)
	if err != nil {
		session.AddFlash("Error whilst registering user.", "auth")
		glog.Errorf("Error whilst registering user: %v", err)
		return SignUp(c, r)
	}

	if err := models.InsertUser(database, user); err != nil {
		session.AddFlash("Error whilst registering user.", "auth")
		glog.Errorf("Error whilst registering user: %v", err)
		return SignUp(c, r)
	}

	session.Values["UserId"] = user.Id

	return "/", http.StatusSeeOther
}
Beispiel #2
0
// Sign In form submit route. Logs user in or set appropriate message in session if login was not succesful
func SignInPost(c web.C, r *http.Request) (string, int) {
	email, password := r.FormValue("email"), r.FormValue("password")

	session := helpers.GetSession(c)
	database := helpers.GetDatabase(c)

	user, err := helpers.Login(database, email, password)

	if err != nil {
		session.AddFlash("Invalid Email or Password", "auth")
		return SignIn(c, r)
	}

	session.Values["UserId"] = user.Id

	return "/", http.StatusSeeOther
}