action_signin.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package actions
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "fmt"
  6. )
  7. func (this *Action) Action_signin() {
  8. if dbe := this.use_database(); dbe != nil {
  9. this.msg_error(dbe.Error())
  10. return
  11. } else {
  12. defer this.db.Close()
  13. }
  14. pf_email := this.wrapper.R.FormValue("email")
  15. pf_password := this.wrapper.R.FormValue("password")
  16. if pf_email == "" {
  17. this.msg_error(`Please specify user email`)
  18. return
  19. }
  20. if !this.is_valid_email(pf_email) {
  21. this.msg_error(`Please specify correct user email`)
  22. return
  23. }
  24. if pf_password == "" {
  25. this.msg_error(`Please specify user password`)
  26. return
  27. }
  28. var user_id int
  29. err := this.db.QueryRow(
  30. "SELECT `id` FROM `users` WHERE `email` = ? and `password` = MD5(?) LIMIT 1;",
  31. pf_email, pf_password).Scan(&user_id)
  32. if err != nil && err != sql.ErrNoRows {
  33. this.msg_error(err.Error())
  34. return
  35. }
  36. if err == sql.ErrNoRows {
  37. this.msg_error(`Incorrect email or password`)
  38. return
  39. }
  40. /*
  41. if !this.Session.IsSetInt("UserId") {
  42. this.Session.SetInt("UserId", 0)
  43. }
  44. if !this.Session.IsSetBool("IsLogged") {
  45. this.Session.SetBool("IsLogged", false)
  46. }
  47. */
  48. var session_user_id int
  49. session_user_id, _ = this.wrapper.Session.GetInt("UserId")
  50. this.msg_success(fmt.Sprintf(
  51. `Test: (%d), (%d)`,
  52. user_id, session_user_id))
  53. // Reload current page
  54. //this.write(fmt.Sprintf(`window.location.reload(false);`))
  55. //this.msg_success(`Hello from web server`)
  56. //this.write(fmt.Sprintf(``))
  57. /*
  58. if count <= 0 {
  59. return this.wrapper.TmplBackEnd(templates.CpFirstUser, nil)
  60. }
  61. */
  62. //this.msg_success(`Hello from web server`)
  63. }