Guest User

Untitled

a guest
Nov 17th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. func userLogin(w http.ResponseWriter, req *http.Request) {
  2.  
  3. if req.Method != "POST" {
  4. return
  5. }
  6.  
  7. db := createConnection()
  8.  
  9. w.Header().Set("Content-type", "application/json")
  10.  
  11. if err := db.Connect(); err != nil {
  12. json.NewEncoder(w).Encode(map[string]bool{"success": false})
  13. log.Println(err)
  14. return
  15. }
  16.  
  17. username := req.FormValue("User")
  18. pass := req.FormValue("Password")
  19. if len(username) == 0 || len(pass) == 0 {
  20. sendJSON(w, "Missing username or password")
  21. return
  22. }
  23.  
  24. user := User{
  25. User: username,
  26. Password: pass,
  27. }
  28.  
  29. // Prepare the statement and execute
  30. stmt, err := db.Prepare("SELECT * FROM tblusers WHERE userid=(?)")
  31. if err != nil {
  32. sendJSON(w, err.Error())
  33. return
  34. }
  35. resp, err := stmt.Run(user.User)
  36. if err != nil {
  37. log.Println(err)
  38. sendJSON(w, err)
  39. return
  40. }
  41. row, err := resp.GetRow()
  42. if err != nil {
  43. log.Println(err)
  44. sendJSON(w, err)
  45. return
  46. }
  47. if len(row) == 0 {
  48. sendJSON(w, "User not found")
  49. return
  50. }
  51. // Create a User instance from the SQL Results.
  52. req_user := User{
  53. row.Str(1),
  54. row.Str(2),
  55. }
  56.  
  57. // if we've got here, we either are logged in or not.
  58. sendJSON(w, user == req_user)
  59. return
  60. }
Add Comment
Please, Sign In to add comment