Sqlite for login
This commit is contained in:
35
main.go
35
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user