in go-gin-react/go-gin-react-part2/main.go [106:132]
func login(c *gin.Context, db *sql.DB) {
// Parse JSON request body into User struct
var user User
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Query database for user
row := db.QueryRow("SELECT id FROM users WHERE username = ? AND password = ?", user.Username, user.Password)
// Get ID of user
var id int
err := row.Scan(&id)
if err != nil {
// Check if user was not found
if err == sql.ErrNoRows {
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid username or password"})
return
}
// Return error if other error occurred
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
// Return ID of user
c.JSON(http.StatusOK, gin.H{"id": id})
}