Exemple #1
0
//login provide JWT in response if login success.
func (a *App) login(c *echo.Context) error {
	loginData := &model.LoginJSON{}
	err := c.Bind(loginData)
	log.Printf("login:%s; passwd:%s\n", loginData.Email, loginData.Password)
	if err != nil {
		c.JSON(http.StatusBadRequest, err)
		return err
	}
	if loginData.Email == "" {
		err = errors.New("email could't be empty.")
		c.JSON(http.StatusNotFound, err)
		return err
	}
	if loginData.Password == "" {
		err = errors.New("password could't be empty.")
		c.JSON(http.StatusNotFound, err)
		return err
	}
	user := &model.User{}
	user.Email, user.Password = loginData.Email, loginData.Password
	status, err := user.CheckPass(a.GetDB())
	if err != nil {
		c.JSON(status, err)
		return err
	}
	tokenizer := jwt.NewTokenizer(a)
	status, err = tokenizer.Create(c, user)
	if err != nil {
		c.JSON(status, err)
		return err
	}
	return a.sendJWT(c)
}
Exemple #2
0
// apiRoute contains router groups for API
func (a *App) apiRoute(e *echo.Echo) error {
	apiUrl, err := a.conf.Get("api-url")
	if err != nil {
		return err
	}
	//General API
	g := e.Group(apiUrl.(string))
	//auth
	g.Post("/auth", a.login)
	//oauth
	a.fb, err = oauth.NewFacebook(a)
	if err != nil {
		return err
	}
	fbg := g.Group("/oauth")
	fbg.Get("/facebook", a.facebookAuth)
	fbg.Get("/facebook/redirect", a.facebookRedirect)
	//tasks
	tokenizer := jwt.NewTokenizer(a)
	tg := g.Group("/tasks", tokenizer.Check())
	tg.Post("", a.create)
	tg.Get("/:id", a.retrieve)
	tg.Get("", a.retrieveAll)
	tg.Put("/:id", a.update)
	tg.Delete("/:id", a.delete)
	return nil
}