updating rest-api

This commit is contained in:
2026-07-17 11:56:13 -05:00
parent 39433a0044
commit c4d7f52d37
15 changed files with 276 additions and 9 deletions

View File

@@ -4,6 +4,7 @@ import (
"net/http"
"example.com/rest-api/models"
"example.com/rest-api/utils"
"github.com/gin-gonic/gin"
)
@@ -26,3 +27,29 @@ func signup(context *gin.Context) {
context.JSON(http.StatusCreated, gin.H{"message": "User created successfully"})
}
func login(context *gin.Context) {
var user models.User
err := context.ShouldBindJSON(&user)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"message": "Could not parse request data."})
return
}
err = user.ValidateCredentials()
if err != nil {
context.JSON(http.StatusUnauthorized, gin.H{"message": "Could not authenticate user."})
}
token, err := utils.GenerateToken(user.Email, user.ID)
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"message": "Could not authenticate user."})
return
}
context.JSON(http.StatusOK, gin.H{"message": "Login successful!", "token": token})
}