diff --git a/main.go b/main.go index 86e18c8..b17c7b7 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,10 @@ package main import ( + "database/sql" "github.com/gin-gonic/gin" + _ "github.com/mattn/go-sqlite3" + "net/http" ) // define global db @@ -36,11 +39,33 @@ type historyData struct { // log the user into their account func login(c *gin.Context) { - // log the user into their account + // get the username and password from the request + var userData loginInput + if err := c.BindJSON(&userData); err != nil { + return + } + // check for user using given credentials + stmt, err := db.Prepare("SELECT * FROM users WHERE name=?") + checkErr(err) + defer stmt.Close() + var user loginOutput + err = stmt.QueryRow(userData.Name).Scan(&user) + if err != nil { + // search failed user not real + c.IndentedJSON(http.StatusNotAcceptable, userData) + } + if user.Name != userData.Name || user.Password != userData.Password { + // user not real + c.IndentedJSON(http.StatusNotAcceptable, userData) + } else { + // user is in + c.IndentedJSON(http.StatusOK, user) + } + } func getTasks(c *gin.Context) { - // return a list of all of the tasks + // return a list of all the tasks } func completeTask(c *gin.Context) { @@ -65,3 +90,9 @@ func main() { router.Run("localhost:8080") } + +func checkErr(err error) { + if err != nil { + panic(err) + } +}