diff --git a/web/api/utils.go b/web/api/utils.go index a736e7e..629d40a 100644 --- a/web/api/utils.go +++ b/web/api/utils.go @@ -32,7 +32,6 @@ func MiddlewareAuth(c *gin.Context) { c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"}) return } - c.Set("authorized", ok) c.Set("user_id", user_id) c.Next() } diff --git a/web/server/handlers.go b/web/server/handlers.go index 8015ea0..5e8a543 100644 --- a/web/server/handlers.go +++ b/web/server/handlers.go @@ -7,8 +7,8 @@ import ( ) func root(c *gin.Context) { - authorized := c.GetBool("authorized") - if authorized { + user_id := c.GetString("user_id") + if user_id != "" { c.Redirect(http.StatusSeeOther, "/quotes") } else { c.HTML(http.StatusOK, "auth.html", nil) @@ -16,10 +16,5 @@ func root(c *gin.Context) { } func quotes(c *gin.Context) { - authorized := c.GetBool("authorized") - if authorized { - c.HTML(http.StatusOK, "quotes.html", nil) - } else { - c.Redirect(http.StatusSeeOther, "/") - } + c.HTML(http.StatusOK, "quotes.html", nil) } diff --git a/web/server/server.go b/web/server/server.go index c2836b4..45597b4 100644 --- a/web/server/server.go +++ b/web/server/server.go @@ -20,7 +20,7 @@ func Serve(addr string) { r.Static("/static", "./static") r.GET("/", api.MiddlewareAuth, root) - r.GET("/quotes", api.MiddlewareAuth, quotes) + r.GET("/quotes", api.MiddlewareAuth, middlewareAuth, quotes) r.Run(addr) } diff --git a/web/server/utils.go b/web/server/utils.go new file mode 100644 index 0000000..085a859 --- /dev/null +++ b/web/server/utils.go @@ -0,0 +1,16 @@ +package server + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func middlewareAuth(c *gin.Context) { + user_id := c.GetString("user_id") + if user_id == "" { + c.Redirect(http.StatusSeeOther, "/") + return + } + c.Next() +}