Exemplo n.º 1
0
// This route logs user out
func Logout(c web.C, r *http.Request) (string, int) {
	session := helpers.GetSession(c)

	delete(session.Values, "UserId")

	return "/", http.StatusSeeOther
}
Exemplo n.º 2
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
}
Exemplo n.º 3
0
// Sign in route
func SignIn(c web.C, r *http.Request) (string, int) {
	t := helpers.GetTemplate(c)
	session := helpers.GetSession(c)

	// With that kind of flags template can "figure out" what route is being rendered
	c.Env["IsSignIn"] = true

	c.Env["Flash"] = session.Flashes("auth")
	var widgets = helpers.Parse(t, "auth/signin", c.Env)

	c.Env["Title"] = "Default Project - Sign In"
	c.Env["Content"] = template.HTML(widgets)

	return helpers.Parse(t, "main", c.Env), http.StatusOK
}
Exemplo n.º 4
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
}