Пример #1
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
}
Пример #2
0
func init() {
	BaseDB = NewDB(settings.App)
	d, err := BaseDB.Connect()
	if err != nil {
		log.Error(nil, err)
	}
	Conn = d
}
Пример #3
0
// RenderTo renders the templaet with name name and passes data as contx, the result is written
// to out.
func (t *Template) RenderTo(out io.Writer, name string, data interface{}) error {
	err := t.tpl.ExecuteTemplate(out, name, data)
	if err != nil {
		log.Error(nil, err)
		return err
	}
	return nil
	//return t.tpl.ExecuteTemplate(out, name, data)
}
Пример #4
0
// GetFlashes retieves all flash messages found in a cookie session associated with ctx..
//
// BUG multipleflash messages are not propery set. the flashes contains only the first
// message to be set.
func GetFlashes(ctx *echo.Context) Flashes {
	ss, err := store.Get(ctx.Request(), settings.App.Session.Flash)
	if err != nil {
		//log.Error(nil, err)
	}
	if v, ok := ss.Values[settings.FlashKey]; ok {
		delete(ss.Values, settings.FlashKey)
		serr := ss.Save(ctx.Request(), ctx.Response())
		if serr != nil {
			log.Error(ctx, err)
		}
		return v.(Flashes)
	}
	return nil
}
Пример #5
0
func getDocIndex(lang string) []*Doc {
	rst := []*Doc{}
	fdir := settings.DocsPath + "/" + lang
	files, err := static.AssetDir(fdir)
	if err != nil {
		log.Error(nil, err)
		return rst
	}
	for _, v := range files {
		fURL := settings.App.AppURL + docsRoute + v
		name := strings.TrimSuffix(v, filepath.Ext(v))
		rst = append(rst, &Doc{name, fURL})
	}
	return rst
}
Пример #6
0
func init() {
	config := &Config{
		Name: "base",
		IncludesDirs: []string{
			"base",
			"partials",
			"auth",
			"dash",
			"errors",
			"resume",
		},
	}
	t, err := New(config)
	if err != nil {
		log.Error(nil, err)
	}
	TPL = t
}