Skip to content

Commit

Permalink
fix: Add admin middleware and update user route
Browse files Browse the repository at this point in the history
  • Loading branch information
peterxcli committed Feb 26, 2024
1 parent 7b92f8c commit 04a94f9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
24 changes: 24 additions & 0 deletions pkg/middleware/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package middleware

import (
"net/http"

"gorm.io/gorm"

"bikefest/pkg/model"

"github.com/gin-gonic/gin"
)

func AdminMiddleware(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
api_key := c.Query("api_key")
if api_key != "peter12345" {
c.AbortWithStatusJSON(http.StatusUnauthorized, model.Response{
Msg: "還敢偷看歐?",
})
return
}
c.Next()
}
}
6 changes: 3 additions & 3 deletions pkg/router/event_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

func RegisterEventRouter(app *bootstrap.Application, controller *controller.EventController) {
r := app.Engine.Group("/events")
authMiddleware := middleware.AuthMiddleware(app.Env.JWT.AccessTokenSecret, app.Cache)
// authMiddleware := middleware.AuthMiddleware(app.Env.JWT.AccessTokenSecret, app.Cache)

r.GET("", controller.GetAllEvent)
//r.GET("/user", authMiddleware, controller.GetUserEvent)
r.GET("/:id", controller.GetEventByID)
//r.POST("", controller.SubscribeEvent)
r.PUT("/:id", authMiddleware, controller.UpdateEvent)
r.GET("/test-store-all", controller.StoreAllEvent)
// r.PUT("/:id", authMiddleware, controller.UpdateEvent)
r.GET("/test-store-all", middleware.AdminMiddleware(app.Conn), controller.StoreAllEvent)
//r.DELETE("/:event_id", controller.DeleteEvent)
}
2 changes: 1 addition & 1 deletion pkg/router/user_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func RegisterUserRoutes(app *bootstrap.Application, controller *controller.UserC
r.GET("/profile", authMiddleware, controller.Profile)
r.GET("/:user_id", controller.GetUserByID)
r.POST("/refresh_token", authMiddleware, controller.RefreshToken)
r.GET("", controller.GetUsers)
r.GET("", middleware.AdminMiddleware(app.Conn), controller.GetUsers)
r.POST("/logout", authMiddleware, controller.Logout)
r.GET("/login/:user_id", controller.FakeLogin)
r.POST("/register", controller.FakeRegister)
Expand Down

0 comments on commit 04a94f9

Please sign in to comment.