Sqlite for login

This commit is contained in:
2023-07-16 16:14:18 +01:00
parent 1b11b4ed7a
commit 2d106ee612

35
main.go
View File

@@ -1,7 +1,10 @@
package main package main
import ( import (
"database/sql"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"net/http"
) )
// define global db // define global db
@@ -36,11 +39,33 @@ type historyData struct {
// log the user into their account // log the user into their account
func login(c *gin.Context) { 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) { 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) { func completeTask(c *gin.Context) {
@@ -65,3 +90,9 @@ func main() {
router.Run("localhost:8080") router.Run("localhost:8080")
} }
func checkErr(err error) {
if err != nil {
panic(err)
}
}