action_signin.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package actions
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. utils "golang-fave/engine/wrapper/utils"
  6. )
  7. func (this *Action) Action_signin() {
  8. if err := this.use_database(); err != nil {
  9. this.msg_error(err.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 !utils.EmailIsValid(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. if this.wrapper.Session.GetIntDef("UserId", 0) > 0 {
  29. this.msg_error(`You already logined`)
  30. return
  31. }
  32. var user_id int
  33. err := this.db.QueryRow(
  34. "SELECT `id` FROM `users` WHERE `email` = ? and `password` = MD5(?) LIMIT 1;",
  35. pf_email, pf_password).Scan(&user_id)
  36. if err != nil && err != sql.ErrNoRows {
  37. this.msg_error(err.Error())
  38. return
  39. }
  40. if err == sql.ErrNoRows {
  41. this.msg_error(`Incorrect email or password`)
  42. return
  43. }
  44. // Save to current session
  45. this.wrapper.Session.SetInt("UserId", user_id)
  46. // Reload current page
  47. this.write(`window.location.reload(false);`)
  48. }