diff --git a/server/internal/controllers/auth.go b/server/internal/controllers/auth.go index b8a644b..c917223 100644 --- a/server/internal/controllers/auth.go +++ b/server/internal/controllers/auth.go @@ -5,6 +5,7 @@ import ( "AREA/internal/pkg" "AREA/internal/utils" "github.com/gin-gonic/gin" + "log" "net/http" ) @@ -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}) } @@ -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 diff --git a/server/internal/models/requests.go b/server/internal/models/requests.go new file mode 100644 index 0000000..f9bf3c7 --- /dev/null +++ b/server/internal/models/requests.go @@ -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"` +}