Skip to content

Commit

Permalink
fix(Server) now binding JSON (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
Djangss authored Dec 4, 2024
1 parent fa573d1 commit 47a34b7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
51 changes: 30 additions & 21 deletions server/internal/controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"AREA/internal/pkg"
"AREA/internal/utils"
"github.com/gin-gonic/gin"
"log"
"net/http"
)

Expand All @@ -14,26 +15,31 @@ import (
// @Tags auth
// @Accept x-www-form-urlencoded
// @Produce json
// @Param email formData string true "Email"
// @Param password formData string true "Password"
// @Param email json string true "email"
// @Param password json string true "password"
// @Success 200 {object} map[string]string
// @Failure 401 {object} map[string]string
// @Router /login [post]
func Login(c *gin.Context) {
email := c.PostForm("email")
password := c.PostForm("password")
var LoginData models.LoginRequest
err := c.ShouldBindJSON(&LoginData)
log.Println(LoginData)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var user models.User
db.DB.Where("email = ?", email).First(&user)
db.DB.Where("email = ?", LoginData.Email).First(&user)
if user.ID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
return
}
if err := utils.VerifyPassword(password, user.Password, user.Salt); err != nil {
if err := utils.VerifyPassword(LoginData.Password, user.Password, user.Salt); err != nil {
println(err)
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
return
}

tokenString := utils.NewToken(c, email)
tokenString := utils.NewToken(c, LoginData.Email)
db.DB.Model(&user).Update("token", tokenString)
c.JSON(http.StatusOK, gin.H{"jwt": tokenString})
}
Expand All @@ -44,38 +50,41 @@ func Login(c *gin.Context) {
// @Tags auth
// @Accept x-www-form-urlencoded
// @Produce json
// @Param email formData string true "Email"
// @Param username formData string true "Username"
// @Param password formData string true "Password"
// @Param email json string true "email"
// @Param username json string true "username"
// @Param password json string true "password"
// @Success 200 {object} map[string]string
// @Failure 409 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /register [post]
func Register(c *gin.Context) {
email := c.PostForm("email")
tokenString := utils.NewToken(c, email)
username := c.PostForm("username")
var RegisterData models.RegisterRequest
err := c.ShouldBindJSON(&RegisterData)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
tokenString := utils.NewToken(c, RegisterData.Email)
var user models.User
db.DB.Where("email = ?", email).First(&user)
db.DB.Where("email = ?", RegisterData.Email).First(&user)
if user.ID != 0 {
c.JSON(http.StatusConflict, gin.H{"error": "User already exists"})
return
}
db.DB.Where("username = ?", username).First(&user)
db.DB.Where("username = ?", RegisterData.Username).First(&user)
if user.ID != 0 {
c.JSON(http.StatusConflict, gin.H{"error": "Username already exists"})
return
}
password, salt := utils.HashPassword(c.PostForm("password"))

password, salt := utils.HashPassword(RegisterData.Password)
db.DB.Create(&models.User{
Email: email,
Username: username,
Email: RegisterData.Email,
Username: RegisterData.Username,
Password: password,
Salt: salt,
Token: tokenString,
})
c.JSON(http.StatusOK, gin.H{"username": username, "email": email, "jwt": tokenString})
c.JSON(http.StatusOK, gin.H{"username": RegisterData.Username, "email": RegisterData.Email, "jwt": tokenString})
}

// Health godoc
Expand Down
12 changes: 12 additions & 0 deletions server/internal/models/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

type LoginRequest struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
}

type RegisterRequest struct {
Email string `json:"email" binding:"required"`
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}

0 comments on commit 47a34b7

Please sign in to comment.