func UserLogout(c *gin.Context) { var cookie middleware.CookieManager cookie = c.MustGet("CM").(middleware.CookieManager) cookie.Delete("user_id") cookie.WriteCookies() c.Redirect(http.StatusMovedPermanently, "/") }
func UserLogin(c *gin.Context) { var cookie middleware.CookieManager cookie = c.MustGet("CM").(middleware.CookieManager) if c.Request.Method == "GET" { data := tool.GetTD(c) fmt.Println(data) c.HTML(http.StatusOK, "login.html", data) } else if c.Request.Method == "POST" { email := c.PostForm("email") password := c.PostForm("password") user := model.User{} model.T.DB.Debug().Where("email = ? and password = ?", email, password).First(&user) if user.Name != "" { cookie.Add("user_id", user.ID) cookie.WriteCookies() c.Redirect(http.StatusMovedPermanently, "/") } else { cookie.Flash("fail_msg", "login failed :(") c.Redirect(http.StatusMovedPermanently, "/user/login") } } }
func UserRegisterHandler(c *gin.Context) { var cookie middleware.CookieManager if c.Request.Method == "GET" { c.HTML(http.StatusOK, "register.html", nil) } else if c.Request.Method == "POST" { u := model.User{Email: c.PostForm("email"), Name: c.PostForm("name"), Password: c.PostForm("password")} if u.InsertToDB() { cookie = c.MustGet("CM").(middleware.CookieManager) cookie.Add("user_id", u.ID) cookie.WriteCookies() } c.Redirect(http.StatusMovedPermanently, "/") } }