Beispiel #1
0
// ログイン可能なユーザか否かを判定する
func (u *User) Auth() (int, error) {
	email := u.Email
	password := u.Password

	db := database.GetDB()
	user := User{}

	db.Where(&User{Email: email}).Find(&user)

	// https://godoc.org/golang.org/x/crypto/bcrypt#CompareHashAndPassword
	err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
	return int(user.ID), err
}
Beispiel #2
0
func GetCurrentUser(req *http.Request) model.User {
	var user model.User

	id := GetSession(req).Values["userId"]

	if id != nil {
		db := database.GetDB()
		db.First(&user, id)
	} else {
		user = model.User{}
	}

	return user
}
Beispiel #3
0
func (u *users) Create(c *gin.Context) {
	email := c.PostForm("email")
	password := c.PostForm("password")

	user := model.User{
		Email:    email,
		Password: model.PasswordHash(password),
	}

	db := database.GetDB()
	db.Create(&user)

	c.Redirect(http.StatusMovedPermanently, "/login")
}
Beispiel #4
0
// TODOを登録する
func (u *todos) Create(c *gin.Context) {
	var todo model.Todo

	if c.BindJSON(&todo) != nil {
		return
	}

	db := database.GetDB()
	user := c.MustGet("user").(model.User)

	todo.UserID = user.ID
	if err := db.Save(&todo).Error; err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
	} else {
		c.JSON(http.StatusCreated, todo)
	}
}
Beispiel #5
0
// TODO一覧を返却する
func (u *todos) List(c *gin.Context) {
	var todos []model.Todo

	db := database.GetDB()
	user := c.MustGet("user").(model.User)

	q := db.Where("user_id = ?", user.ID)
	completed := c.DefaultQuery("completed", "")

	if completed != "" {
		q = q.Where("completed = ?", completed)
	}

	if err := q.Find(&todos).Error; err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
	} else {
		c.JSON(http.StatusOK, gin.H{"todos": todos})
	}
}
Beispiel #6
0
func apiHandle() gin.HandlerFunc {
	return func(c *gin.Context) {
		var user model.User

		db := database.GetDB()
		if err := db.Where("token = ?", c.Request.Header.Get("X-GODEMO-TOKEN")).Find(&user).Error; err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"errors": []string{"User not found"}})
			c.Abort()
			return
		}

		c.Set("user", user)
		c.Next()

		errs := make([]string, 0, len(c.Errors))
		for _, e := range c.Errors {
			// 1. エラーの種類で判定
			switch e.Err {
			case io.EOF:
				errs = append(errs, msgInvalidJSON)
			default:
				// 2. エラーの型で判定
				switch e.Err.(type) {
				case *json.SyntaxError:
					errs = append(errs, msgInvalidJSON)
				case *json.UnmarshalTypeError:
					errs = append(errs, msgInvalidJSONType(e.Err.(*json.UnmarshalTypeError)))
				case *validator.StructErrors:
					for _, fieldErr := range e.Err.(*validator.StructErrors).Flatten() {
						errs = append(errs, msgValidationFailed(fieldErr))
					}
				default:
					errs = append(errs, e.Error())
				}
			}
		}

		if len(c.Errors) > 0 {
			c.JSON(-1, gin.H{"errors": errs}) // -1 == not override the current error code
		}
	}
}
Beispiel #7
0
func migrate() {
	db := database.GetDB()

	db.AutoMigrate(&model.User{}, &model.Todo{})
}