action_signin.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // Save to current session
  41. this.wrapper.Session.SetInt("UserId", user_id)
  42. this.wrapper.Session.SetBool("IsLogged", true)
  43. // Reload current page
  44. this.write(fmt.Sprintf(`window.location.reload(false);`))
  45. }