module_index_act_signin.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package modules
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "golang-fave/engine/wrapper"
  6. "golang-fave/utils"
  7. )
  8. func (this *Modules) RegisterAction_IndexUserSignIn() *Action {
  9. return this.newAction(AInfo{
  10. WantDB: true,
  11. Mount: "index-user-sign-in",
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_email := wrap.R.FormValue("email")
  14. pf_password := wrap.R.FormValue("password")
  15. if pf_email == "" {
  16. wrap.MsgError(`Please specify user email`)
  17. return
  18. }
  19. if !utils.IsValidEmail(pf_email) {
  20. wrap.MsgError(`Please specify correct user email`)
  21. return
  22. }
  23. if pf_password == "" {
  24. wrap.MsgError(`Please specify user password`)
  25. return
  26. }
  27. if wrap.S.GetInt("UserId", 0) > 0 {
  28. wrap.MsgError(`You already logined`)
  29. return
  30. }
  31. var user_id int
  32. err := wrap.DB.QueryRow(
  33. `SELECT
  34. id
  35. FROM
  36. users
  37. WHERE
  38. email = ? and
  39. password = MD5(?) and
  40. admin = 1 and
  41. active = 1
  42. LIMIT 1;`,
  43. pf_email,
  44. pf_password,
  45. ).Scan(
  46. &user_id,
  47. )
  48. if err != nil && err != sql.ErrNoRows {
  49. wrap.MsgError(err.Error())
  50. return
  51. }
  52. if err == sql.ErrNoRows {
  53. wrap.MsgError(`Incorrect email or password`)
  54. return
  55. }
  56. // Save to current session
  57. wrap.S.SetInt("UserId", user_id)
  58. // Reload current page
  59. wrap.Write(`window.location.reload(false);`)
  60. })
  61. }