diff --git a/server/handlers/auth/login.go b/server/handlers/auth/login.go index bc5ed4f..4b5e93f 100644 --- a/server/handlers/auth/login.go +++ b/server/handlers/auth/login.go @@ -19,30 +19,42 @@ type LoginBody struct { func Login(c *fiber.Ctx) error { var body LoginBody if err := c.BodyParser(&body); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Invalid request body."}) + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "message": "Invalid request body.", + }) } var validate = validator.New() if err := validate.Struct(&body); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Missing or invalid fields."}) + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "message": "Missing or invalid fields.", + }) } auth := models.ValidateUser(c) if auth.IsAuthenticated { - c.Status(fiber.StatusConflict).JSON(fiber.Map{"message": "User already authenticated."}) + c.Status(fiber.StatusConflict).JSON(fiber.Map{ + "message": "User already authenticated.", + }) return nil } user, err := models.GetUserByUsername(body.Username) if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Error fetching user."}) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "message": "Error fetching user.", + }) } if user.Username == "" { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Invalid credentials."}) + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + "message": "Invalid credentials.", + }) } if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(body.Password)); err != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Invalid credentials."}) + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + "message": "Invalid credentials.", + }) } claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{ @@ -52,7 +64,9 @@ func Login(c *fiber.Ctx) error { token, err := claims.SignedString([]byte(os.Getenv("JWT_SECRET"))) if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Error creating token."}) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "message": "Error creating token.", + }) } cookie := &fiber.Cookie{ diff --git a/server/handlers/auth/me.go b/server/handlers/auth/me.go index d232be7..3fc0ddf 100644 --- a/server/handlers/auth/me.go +++ b/server/handlers/auth/me.go @@ -8,7 +8,9 @@ import ( func User(c *fiber.Ctx) error { user, ok := c.Locals("user").(models.User) if !ok { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "User not authenticated."}) + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + "message": "User not authenticated.", + }) } user.Password = "" diff --git a/server/handlers/auth/register.go b/server/handlers/auth/register.go index 38648ae..07cf035 100644 --- a/server/handlers/auth/register.go +++ b/server/handlers/auth/register.go @@ -17,20 +17,28 @@ type RegisterBody struct { func Register(c *fiber.Ctx) error { var body RegisterBody if err := c.BodyParser(&body); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Invalid request body."}) + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "message": "Invalid request body.", + }) } var validate = validator.New() if err := validate.Struct(&body); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Missing or invalid fields."}) + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "message": "Missing or invalid fields.", + }) } user, err := models.GetUserByUsername(body.Username) if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Error checking if user exists."}) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "message": "Error checking if user exists.", + }) } if user.Username != "" { - return c.Status(fiber.StatusConflict).JSON(fiber.Map{"message": "User already exists."}) + return c.Status(fiber.StatusConflict).JSON(fiber.Map{ + "message": "User already exists.", + }) } hash, _ := bcrypt.GenerateFromPassword([]byte(body.Password), 10) @@ -41,8 +49,12 @@ func Register(c *fiber.Ctx) error { Password: string(hash), } if err := models.CreateUser(user); err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Error creating user."}) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "message": "Error creating user.", + }) } - return c.Status(fiber.StatusCreated).JSON(fiber.Map{"message": "User created."}) + return c.Status(fiber.StatusCreated).JSON(fiber.Map{ + "message": "User created.", + }) } diff --git a/server/handlers/comments/create_comment.go b/server/handlers/comments/create_comment.go index d8a56f9..3cea817 100644 --- a/server/handlers/comments/create_comment.go +++ b/server/handlers/comments/create_comment.go @@ -14,23 +14,31 @@ type NewCommentBody struct { func CreateComment(c *fiber.Ctx) error { user, ok := c.Locals("user").(models.User) if !ok { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "User not authenticated."}) + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + "message": "User not authenticated.", + }) } id := c.Params("post_id") postID, err := primitive.ObjectIDFromHex(id) if err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Invalid post ID."}) + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "message": "Invalid post ID.", + }) } var body NewCommentBody if err := c.BodyParser(&body); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Invalid request body."}) + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "message": "Invalid request body.", + }) } var validate = validator.New() if err := validate.Struct(&body); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Missing or invalid fields."}) + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "message": "Missing or invalid fields.", + }) } user.Email = "" @@ -44,7 +52,9 @@ func CreateComment(c *fiber.Ctx) error { createdComment, err := models.CreateComment(comment) if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Error creating comment."}) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "message": "Error creating comment.", + }) } return c.Status(fiber.StatusCreated).JSON(fiber.Map{ diff --git a/server/handlers/ping.go b/server/handlers/ping.go index e454eff..2cd20f2 100644 --- a/server/handlers/ping.go +++ b/server/handlers/ping.go @@ -5,7 +5,9 @@ import ( ) func Ping(c *fiber.Ctx) error { - c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "pong"}) + c.Status(fiber.StatusOK).JSON(fiber.Map{ + "message": "pong", + }) return nil } diff --git a/server/handlers/posts/create_post.go b/server/handlers/posts/create_post.go index 8dc8e66..5809230 100644 --- a/server/handlers/posts/create_post.go +++ b/server/handlers/posts/create_post.go @@ -13,17 +13,23 @@ type NewPostBody struct { func CreatePost(c *fiber.Ctx) error { user, ok := c.Locals("user").(models.User) if !ok { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "User not authenticated."}) + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + "message": "User not authenticated.", + }) } var body NewPostBody if err := c.BodyParser(&body); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Invalid request body."}) + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "message": "Invalid request body.", + }) } var validate = validator.New() if err := validate.Struct(&body); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Missing or invalid fields."}) + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "message": "Missing or invalid fields.", + }) } user.Email = "" @@ -36,7 +42,9 @@ func CreatePost(c *fiber.Ctx) error { createdPost, err := models.CreatePost(post) if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Error creating post."}) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "message": "Error creating post.", + }) } return c.Status(fiber.StatusCreated).JSON(fiber.Map{ diff --git a/server/middlewares/auth.go b/server/middlewares/auth.go index b4ba054..56e8446 100644 --- a/server/middlewares/auth.go +++ b/server/middlewares/auth.go @@ -17,9 +17,12 @@ func AuthMiddleware() fiber.Handler { }) } - token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) { - return []byte(os.Getenv("JWT_SECRET")), nil - }) + token, err := jwt.ParseWithClaims( + cookie, + &jwt.StandardClaims{}, + func(token *jwt.Token) (interface{}, error) { + return []byte(os.Getenv("JWT_SECRET")), nil + }) if err != nil { return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "message": "User not authenticated.",