refactor(api): move authentication into a middleware

This commit is contained in:
Masahiko AMANO 2025-01-06 20:17:07 +03:00
parent 521e140881
commit 55986bab36
3 changed files with 30 additions and 49 deletions

View File

@ -38,12 +38,7 @@ func userAuth(c *gin.Context) {
}
func userGet(c *gin.Context) {
session := sessions.Default(c)
user_id, ok := session.Get("user_id").(string)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"})
return
}
user_id := c.GetString("user_id")
user, err := db.UserGet(context.Background(), user_id)
if err != nil {
status, message := HandleDBError(err)
@ -54,12 +49,7 @@ func userGet(c *gin.Context) {
}
func userUpdate(c *gin.Context) {
session := sessions.Default(c)
user_id, ok := session.Get("user_id").(string)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"})
return
}
user_id := c.GetString("user_id")
var body map[string]string
err := c.BindJSON(&body)
if err != nil {
@ -130,12 +120,7 @@ func userLogout(c *gin.Context) {
//#region Quotes
func quotesGet(c *gin.Context) {
session := sessions.Default(c)
user_id, ok := session.Get("user_id").(string)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"})
return
}
user_id := c.GetString("user_id")
filter, ok := c.GetQuery("filter")
if !ok {
filter = ""
@ -178,12 +163,7 @@ func quotesGet(c *gin.Context) {
}
func quoteGet(c *gin.Context) {
session := sessions.Default(c)
user_id, ok := session.Get("user_id").(string)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"})
return
}
user_id := c.GetString("user_id")
quote_id := c.Param("id")
quote, err := db.QuoteGet(context.Background(), user_id, quote_id)
if err != nil {
@ -195,12 +175,7 @@ func quoteGet(c *gin.Context) {
}
func quoteAdd(c *gin.Context) {
session := sessions.Default(c)
user_id, ok := session.Get("user_id").(string)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"})
return
}
user_id := c.GetString("user_id")
var body map[string]string
err := c.BindJSON(&body)
if err != nil {
@ -238,12 +213,7 @@ func quoteAdd(c *gin.Context) {
}
func quoteUpdate(c *gin.Context) {
session := sessions.Default(c)
user_id, ok := session.Get("user_id").(string)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"})
return
}
user_id := c.GetString("user_id")
quote_id := c.Param("id")
var body map[string]string
err := c.BindJSON(&body)
@ -289,12 +259,7 @@ func quoteUpdate(c *gin.Context) {
}
func quoteDelete(c *gin.Context) {
session := sessions.Default(c)
user_id, ok := session.Get("user_id").(string)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"})
return
}
user_id := c.GetString("user_id")
quote_id := c.Param("id")
err := db.QuoteDelete(context.Background(), user_id, quote_id)
if err != nil {

View File

@ -21,14 +21,14 @@ func RegisterRoutes(r *gin.Engine) {
api := r.Group("/api")
{
api.POST("/auth", userAuth)
api.GET("/auth", userGet)
api.PATCH("/auth", userUpdate)
api.GET("/auth", MiddlewareAuth, userGet)
api.PATCH("/auth", MiddlewareAuth, userUpdate)
api.DELETE("/auth", userLogout)
api.GET("/quotes", quotesGet)
api.POST("/quotes", quoteAdd)
api.GET("/quotes/:id", quoteGet)
api.PATCH("/quotes/:id", quoteUpdate)
api.DELETE("/quotes/:id", quoteDelete)
api.GET("/quotes", MiddlewareAuth, quotesGet)
api.POST("/quotes", MiddlewareAuth, quoteAdd)
api.GET("/quotes/:id", MiddlewareAuth, quoteGet)
api.PATCH("/quotes/:id", MiddlewareAuth, quoteUpdate)
api.DELETE("/quotes/:id", MiddlewareAuth, quoteDelete)
}
}

View File

@ -1,8 +1,12 @@
package api
import (
"net/http"
"strconv"
"strings"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgconn"
)
@ -20,3 +24,15 @@ func HandleDBError(err error) (int, string) {
return 400, pgErr.Message
}
}
func MiddlewareAuth(c *gin.Context) {
session := sessions.Default(c)
user_id, ok := session.Get("user_id").(string)
if !ok && strings.HasPrefix(c.Request.URL.Path, "/api") {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"})
return
}
c.Set("authorized", ok)
c.Set("user_id", user_id)
c.Next()
}