Skip to content

Commit

Permalink
style(server): improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Sep 6, 2024
1 parent e8944ea commit 9368b1a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 27 deletions.
28 changes: 21 additions & 7 deletions server/handlers/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down
4 changes: 3 additions & 1 deletion server/handlers/auth/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
24 changes: 18 additions & 6 deletions server/handlers/auth/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.",
})
}
20 changes: 15 additions & 5 deletions server/handlers/comments/create_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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{
Expand Down
4 changes: 3 additions & 1 deletion server/handlers/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 12 additions & 4 deletions server/handlers/posts/create_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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{
Expand Down
9 changes: 6 additions & 3 deletions server/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down

0 comments on commit 9368b1a

Please sign in to comment.