action_signin.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package actions
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. )
  6. func (this *Action) Action_signin() {
  7. if dbe := this.use_database(); dbe != nil {
  8. this.msg_error(dbe.Error())
  9. return
  10. } else {
  11. defer this.db.Close()
  12. }
  13. pf_email := this.wrapper.R.FormValue("email")
  14. pf_password := this.wrapper.R.FormValue("password")
  15. if pf_email == "" {
  16. this.msg_error(`Please specify user email`)
  17. return
  18. }
  19. if !this.is_valid_email(pf_email) {
  20. this.msg_error(`Please specify correct user email`)
  21. return
  22. }
  23. if pf_password == "" {
  24. this.msg_error(`Please specify user password`)
  25. return
  26. }
  27. if this.wrapper.Session.GetIntDef("UserId", 0) > 0 {
  28. this.msg_error(`You already logined`)
  29. return
  30. }
  31. var user_id int
  32. err := this.db.QueryRow(
  33. "SELECT `id` FROM `users` WHERE `email` = ? and `password` = MD5(?) LIMIT 1;",
  34. pf_email, pf_password).Scan(&user_id)
  35. if err != nil && err != sql.ErrNoRows {
  36. this.msg_error(err.Error())
  37. return
  38. }
  39. if err == sql.ErrNoRows {
  40. this.msg_error(`Incorrect email or password`)
  41. return
  42. }
  43. // Save to current session
  44. this.wrapper.Session.SetInt("UserId", user_id)
  45. // Reload current page
  46. this.write(`window.location.reload(false);`)
  47. }