updating rest-api
This commit is contained in:
@@ -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})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user