From edcc329614e79fe07be95578ceea1813b8b55fd0 Mon Sep 17 00:00:00 2001 From: Beomgi Kim Date: Thu, 22 Sep 2022 23:40:10 +0900 Subject: [PATCH] Change the redirect handling --- handler/handler.go | 9 +++------ main.go | 39 +++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/handler/handler.go b/handler/handler.go index bd805d1..a9e64d3 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -39,22 +39,19 @@ func (h *Handler) SessionAuth(c *gin.Context) { cookie, err := c.Cookie("userId") if err != nil { log.Println(err) - c.Redirect(http.StatusFound, "/login") - c.Abort() + c.AbortWithStatus(http.StatusUnauthorized) return } user, err := h.dbClient.FindUserByUsername(cookie) if err != nil { log.Println(err) - c.Redirect(http.StatusFound, "/login") - c.Abort() + c.AbortWithStatus(http.StatusUnauthorized) return } if user == nil { - c.Redirect(http.StatusFound, "/login") - c.Abort() + c.AbortWithStatus(http.StatusUnauthorized) return } c.Next() diff --git a/main.go b/main.go index 7bad9f9..adfd320 100644 --- a/main.go +++ b/main.go @@ -10,29 +10,28 @@ func main() { router := gin.Default() handler := handler.New() - router.GET("/", handler.SessionAuth, static.Serve("/", static.LocalFile("/Frontend", true))) router.Use(static.Serve("/", static.LocalFile("/Frontend", true))) + router.Use(static.Serve("/login", static.LocalFile("/Frontend", true))) + router.Use(static.Serve("/signup", static.LocalFile("/Frontend", true))) - router.GET("/login", static.Serve("/login", static.LocalFile("/Frontend", true))) - router.GET("/signup", static.Serve("/signup", static.LocalFile("/Frontend", true))) - - router.POST("/login", handler.PostLogin) - router.POST("/signup", handler.PostSignup) - - router.Use(handler.SessionAuth) - - router.POST("/logout", handler.PostLogout) - - userApi := router.Group("/user") - { - userApi.GET("/", handler.GetUserData) - } - - imageApi := router.Group("/image") + router.Group("/api") { - imageApi.GET("/:id", handler.GetImage) - imageApi.GET("/list", handler.GetImageList) - imageApi.POST("/", handler.PostImage) + router.Use(handler.SessionAuth) + router.POST("/login", handler.PostLogin) + router.POST("/signup", handler.PostSignup) + router.POST("/logout", handler.PostLogout) + + userApi := router.Group("/user") + { + userApi.GET("/", handler.GetUserData) + } + + imageApi := router.Group("/image") + { + imageApi.GET("/:id", handler.GetImage) + imageApi.GET("/list", handler.GetImageList) + imageApi.POST("/", handler.PostImage) + } } router.Run(":8005")