diff --git a/go-crud.exe b/go-crud.exe index c9eddab..d5af0c2 100644 Binary files a/go-crud.exe and b/go-crud.exe differ diff --git a/initializers/syncDatabase.go b/initializers/syncDatabase.go new file mode 100644 index 0000000..ac44c85 --- /dev/null +++ b/initializers/syncDatabase.go @@ -0,0 +1,9 @@ +package initializers + +import "go-crud/models" + +func SyncDatabase() { + DB.AutoMigrate(&models.Post{}) + DB.AutoMigrate(&models.Event{}) + DB.AutoMigrate(&models.User{}) +} \ No newline at end of file diff --git a/main.go b/main.go index 11f9c14..c6d563b 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( func init() { initializers.LoadEnvVariables() initializers.ConnectToDB() + initializers.SyncDatabase() } func main() { r := gin.Default() @@ -19,6 +20,7 @@ func main() { r.POST("/login", controllers.LogIn) r.GET("/validate", middleware.RequireAuth, controllers.Validate) + r.Use(middleware.RequireAuth) r.POST("/posts", controllers.PostsCreate) r.PUT("/posts/:id", controllers.PostsUpdate) r.GET("/posts", controllers.PostsIndex) diff --git a/middleware/requireAuth.go b/middleware/requireAuth.go index cb74e64..abbdc1b 100644 --- a/middleware/requireAuth.go +++ b/middleware/requireAuth.go @@ -52,11 +52,18 @@ func RequireAuth(c *gin.Context) { var user models.User initializers.DB.First(&user, claims["sub"]) + var post models.Post + initializers.DB.First(&post, claims["sub"]) + + var event models.Event + initializers.DB.First(&event, claims["sub"]) + if user.ID == 0 { c.AbortWithStatus(http.StatusUnauthorized) return } + // Attach to req c.Set("user", user) diff --git a/migrate/migrate.go b/migrate/migrate.go deleted file mode 100644 index 72cb7ab..0000000 --- a/migrate/migrate.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import "go-crud/initializers" -import "go-crud/models" - -func init() { - initializers.LoadEnvVariables() - initializers.ConnectToDB() -} - -func main() { - initializers.DB.AutoMigrate(&models.Post{}) - initializers.DB.AutoMigrate(&models.Event{}) - initializers.DB.AutoMigrate(&models.User{}) -} \ No newline at end of file