예제 #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
	}
}
예제 #2
0
func LogoutHandler(c *gin.Context) {
	session := sessions.Default(c)
	a := auth.Default(c)
	auth.Logout(session, a.User)

	c.Redirect(http.StatusMovedPermanently, "/")
}
예제 #3
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
	}
}
예제 #4
0
파일: render.go 프로젝트: hobo-c/gin_sample
func getCommonContext(c *gin.Context) map[string]interface{} {
	a := auth.Default(c)
	userId := a.User.UniqueId().(int64)

	// 公共模板数据
	commonDatas := make(map[string]interface{})
	commonDatas["UserId"] = userId
	commonDatas["UserName"] = "******"
	commonDatas["requestUrl"] = c.Request.URL.String()

	return commonDatas
}
예제 #5
0
func LoginHandler(c *gin.Context) {
	redirect := c.DefaultQuery(auth.RedirectParam, "/")
	a := auth.Default(c)
	if a.User.IsAuthenticated() {
		c.Redirect(http.StatusMovedPermanently, redirect)
		return
	}

	c.Set("tmpl", "www/login")
	c.Set("data", map[string]interface{}{
		"title":         "Login",
		"redirectParam": auth.RedirectParam,
		"redirect":      redirect,
	})
}