Example #1
0
// RegisterPost handles registration form, and create a session for the new user if the registration
// process is complete.
//
//		Method           POST
//
//		Route            /auth/register
//
//		Restrictions     None
//
// 		Template         None (All actions redirect to other routes )
//
// Flash messages may be set before redirection.
func RegisterPost(ctx *echo.Context) error {
	var flashMessages = flash.New()
	f := forms.New(utils.GetLang(ctx))
	lf := f.RegisterForm()(ctx.Request())
	if !lf.IsValid() {

		// Case the form is not valid, ships it back with the errors exclusively
		utils.SetData(ctx, authForm, lf)
		return ctx.Render(http.StatusOK, tmpl.RegisterTpl, utils.GetData(ctx))
	}

	// we are not interested in the returned user, rather we make sure the user has
	// been created.
	_, err := query.CreateNewUser(lf.GetModel().(forms.Register))
	if err != nil {
		flashMessages.Err(msgAccountCreateFailed)
		flashMessages.Save(ctx)
		ctx.Redirect(http.StatusFound, "/auth/register")
		return nil
	}

	// TODO: improve the message to include directions to use the current email and
	// password to login?
	flashMessages.Success(msgAccountCreate)
	flashMessages.Save(ctx)

	// Don't create session in this route, its best to leave only one place which
	// messes with the main user session. So we redirect to the login page, and encourage
	// the user to login.
	ctx.Redirect(http.StatusFound, "/auth/login")
	return nil
}
Example #2
0
// JobsNewPost process the new job form.
//
//		Method           POST
//
//		Route            /dash/jobs/new
//
//		Restrictions     Yes
//
// 		Template         None
func JobsNewPost(ctx *echo.Context) error {
	var flashMessages = flash.New()
	f := forms.New(utils.GetLang(ctx))
	jf := f.JobForm()(ctx.Request())
	if !jf.IsValid() {
		// TODO: improve flash message ?
		flashMessages.Err(msgInvalidorm)
		flashMessages.Save(ctx)
		ctx.Redirect(http.StatusFound, "/dash/jobs/new")
		return nil
	}

	if isLoged := ctx.Get("IsLoged"); isLoged != nil {
		person := ctx.Get("User").(*models.Person)
		if jerr := query.PersonCreateJob(person, jf.GetModel().(forms.JobForm)); jerr != nil {
			// TODO: improve flash message ?
			flashMessages.Err("some really bad fish happened")
			flashMessages.Save(ctx)
			ctx.Redirect(http.StatusFound, "/dash/jobs/new")
			return nil
		}
		// add flash message
		flashMessages.Success("new job was created successful")
		flashMessages.Save(ctx)

		ctx.Redirect(http.StatusFound, "/dash/")
		return nil
	}
	he := echo.NewHTTPError(http.StatusUnauthorized)
	ctx.Error(he)
	return nil
}
Example #3
0
func TestLangs(t *testing.T) {
	e := echo.New()
	e.Use(Langs())
	e.Get("/", func(ctx *echo.Context) error {
		lang := utils.GetLang(ctx)
		return ctx.String(http.StatusOK, lang)
	})
	ts := httptest.NewServer(e)
	defer ts.Close()
	jar, err := cookiejar.New(nil)
	if err != nil {
		t.Error(err)
	}
	client := &http.Client{Jar: jar}

	resp, err := client.Get(ts.URL)
	if err != nil {
		t.Error(err)
	}
	defer resp.Body.Close()

	buf := &bytes.Buffer{}
	io.Copy(buf, resp.Body)

	// The default language should be en.
	if buf.String() != "en" {
		t.Errorf("expected en got %s", buf.String())
	}

}
Example #4
0
// Register renders registration form.
//
//		Method           GET
//
//		Route            /auth/register
//
//		Restrictions     None
//
// 		Template         auth/register.html
func Register(ctx *echo.Context) error {
	f := forms.New(utils.GetLang(ctx))
	utils.SetData(ctx, authForm, f.RegisterForm()())

	// set page tittle to register
	utils.SetData(ctx, "PageTitle", "register")
	return ctx.Render(http.StatusOK, tmpl.RegisterTpl, utils.GetData(ctx))
}
Example #5
0
// LoginPost handlers login form, and logs in the user. If the form is valid, the user is
// redirected to "/auth/login" with the form validation errors. When the user is validated
// redirection is made to "/".
//
//		Method           POST
//
//		Route            /auth/login
//
//		Restrictions     None
//
// 		Template         None (All actions redirect to other routes )
//
// Flash messages may be set before redirection.
func LoginPost(ctx *echo.Context) error {
	var flashMessages = flash.New()

	f := forms.New(utils.GetLang(ctx))
	lf := f.LoginForm()(ctx.Request())
	if !lf.IsValid() {
		utils.SetData(ctx, authForm, lf)
		ctx.Redirect(http.StatusFound, "/auth/login")
		return nil
	}

	// Check email and password
	user, err := query.AuthenticateUserByEmail(lf.GetModel().(forms.Login))
	if err != nil {
		log.Error(ctx, err)

		// We want the user to try again, but rather than rendering the form right
		// away, we redirect him/her to /auth/login route(where the login process with
		// start aflsesh albeit with a flash message)
		flashMessages.Err(msgLoginErr)
		flashMessages.Save(ctx)
		ctx.Redirect(http.StatusFound, "/auth/login")
		return nil
	}

	// create a session for the user after the validation has passed. The info stored
	// in the session is the user ID, where as the key is userID.
	ss, err := sessStore.Get(ctx.Request(), settings.App.Session.Name)
	if err != nil {
		log.Error(ctx, err)
	}
	ss.Values["userID"] = user.ID
	err = ss.Save(ctx.Request(), ctx.Response())
	if err != nil {
		log.Error(ctx, err)
	}
	person, err := query.GetPersonByUserID(user.ID)
	if err != nil {
		log.Error(ctx, err)
		flashMessages.Err(msgLoginErr)
		flashMessages.Save(ctx)
		ctx.Redirect(http.StatusFound, "/auth/login")
		return nil
	}

	// add context data. IsLoged is just a conveniece in template rendering. the User
	// contains a models.Person object, where the PersonName is already loaded.
	utils.SetData(ctx, "IsLoged", true)
	utils.SetData(ctx, "User", person)
	flashMessages.Success(msgLoginSuccess)
	flashMessages.Save(ctx)
	ctx.Redirect(http.StatusFound, "/")

	log.Info(ctx, "login success")
	return nil
}
Example #6
0
// Login renders login form.
//
//		Method           GET
//
//		Route            /auth/login
//
//		Restrictions     None
//
// 		Template         auth/login.html
//
func Login(ctx *echo.Context) error {

	f := forms.New(utils.GetLang(ctx))
	utils.SetData(ctx, authForm, f.LoginForm()())

	// set page tittle to login
	utils.SetData(ctx, "PageTitle", "login")

	return ctx.Render(http.StatusOK, tmpl.LoginTpl, utils.GetData(ctx))
}
Example #7
0
// ProfileName updates Person's names.
//
//		Method       POST
//
//		Route         /dash/profile/name
//
//		Restricted    Yes
//
//		Template      None (everything is redirected to '/dash/profile' )
//
// When there are validation errors flash messages are set.
func ProfileName(ctx *echo.Context) error {
	r := ctx.Request()
	v := forms.NewValid(utils.GetLang(ctx))
	r.ParseForm()
	pName := &models.PersonName{}
	if err := formDecoder.Decode(pName, r.PostForm); err != nil {
		// TODO: do something?
	}
	errs := v.ValidatePersonName(pName)
	if errs != nil {
		// TODO: do somethins?
	}

	person := ctx.Get("User").(*models.Person)
	person.UpdateNames(pName)
	err := query.Update(person)
	if err != nil {
		// TODO: do somethins?
	}

	ctx.Redirect(http.StatusFound, "/dash/profile")
	return nil
}
Example #8
0
// JobsNewGet renders the new job form.
//
//		Method           GET
//
//		Route            /dash/jobs/new
//
//		Restrictions     Yes
//
// 		Template         dash/jobs_new.html
func JobsNewGet(ctx *echo.Context) error {
	f := forms.New(utils.GetLang(ctx))
	utils.SetData(ctx, "PageTitle", "new job")
	utils.SetData(ctx, "JobForm", f.JobForm()())
	return ctx.Render(http.StatusOK, tmpl.DashJobTpl, utils.GetData(ctx))
}
Example #9
0
// Home renders dashboard home page.
//
//		Method           GET
//
//		Route            /dash/
//
//		Restrictions     Yes
//
// 		Template         dash/home.html
func Home(ctx *echo.Context) error {
	utils.SetData(ctx, "PageTitle", "dashboard")
	f := forms.New(utils.GetLang(ctx))
	utils.SetData(ctx, "JobForm", f.JobForm()())
	return ctx.Render(http.StatusOK, tmpl.DashHomeTpl, utils.GetData(ctx))
}