Exemple #1
0
func RegisterPostHandler(c *gin.Context) {
	redirect := c.DefaultQuery(auth.RedirectParam, "/")
	a := auth.Default(c)
	if a.User.IsAuthenticated() {
		c.Redirect(http.StatusMovedPermanently, redirect)
		return
	}

	registerURL := fmt.Sprintf("/register?%s=%s", auth.RedirectParam, redirect)

	var form LoginForm
	if c.Bind(&form) == nil {
		model := models.Default(c)
		u := model.AddUserWithNicknamePwd(form.Nickname, form.Password)
		if u != nil {
			session := sessions.Default(c)
			err := auth.AuthenticateSession(session, u)
			if err != nil {
				c.JSON(http.StatusBadRequest, err)
			}
			c.Redirect(http.StatusMovedPermanently, redirect)
			return
		} else {
			log.Print("Register user add error")
			c.Redirect(http.StatusMovedPermanently, registerURL)
			return
		}
	} else {
		log.Print("Register form bind error")
		c.Redirect(http.StatusMovedPermanently, registerURL)
		return
	}
}
Exemple #2
0
func LoginPostHandler(c *gin.Context) {
	redirect := c.DefaultQuery(auth.RedirectParam, "/")
	a := auth.Default(c)
	if a.User.IsAuthenticated() {
		c.Redirect(http.StatusMovedPermanently, redirect)
		return
	}

	loginURL := fmt.Sprintf("/login?%s=%s", auth.RedirectParam, redirect)

	var form LoginForm
	if c.Bind(&form) == nil {
		model := models.Default(c)
		u := model.GetUserByNicknamePwd(form.Nickname, form.Password)
		if u != nil {
			session := sessions.Default(c)
			err := auth.AuthenticateSession(session, u)
			if err != nil {
				c.JSON(http.StatusBadRequest, err)
			}
			c.Redirect(http.StatusMovedPermanently, redirect)
			return
		} else {
			c.Redirect(http.StatusMovedPermanently, loginURL)
			return
		}
	} else {
		c.Redirect(http.StatusMovedPermanently, loginURL)
		return
	}
}
Exemple #3
0
func PostsHandler(c *gin.Context) {

	userIdStr := c.Param("userId")
	userId, err := strconv.ParseUint(userIdStr, 10, 64)
	if err != nil {
		panic(err)
	}

	page, err := strconv.Atoi(c.Param("p"))
	if err != nil {
		panic(err)
	}
	size, err := strconv.Atoi(c.Param("s"))
	if err != nil {
		panic(err)
	}

	model := models.Default(c)
	posts := model.GetUserPostsByUserId(userId, page, size)

	c.JSON(http.StatusOK, gin.H{
		"title": "Post",
		"posts": posts,
	})
}
Exemple #4
0
func PostSaveHandler(c *gin.Context) {
	model := models.Default(c)
	model.PostSave()

	c.JSON(http.StatusOK, gin.H{
		"title": "Post Add",
	})
}
Exemple #5
0
func HomeHandler(c *gin.Context) {
	id, err := strconv.ParseUint("1", 10, 64)
	if err != nil {
		panic(err)
	}
	model := models.Default(c)
	u := model.GetUserById(id)

	c.Set("tmpl", "www/home")
	c.Set("data", map[string]interface{}{
		"title": "Home",
		"user":  u,
	})
}
Exemple #6
0
func PostHandler(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		panic(err)
	}

	model := models.Default(c)
	post := model.GetPostById(id)

	c.JSON(http.StatusOK, gin.H{
		"title": "Post",
		"post":  post,
	})
}
Exemple #7
0
func UserHandler(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		panic(err)
	}

	model := models.Default(c)
	u := model.GetUserById(id)

	c.Set("tmpl", "www/user")
	c.Set("data", map[string]interface{}{
		"title": "User",
		"user":  u,
	})
}