module_index_act_signin.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package modules
  2. import (
  3. "golang-fave/engine/sqlw"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  6. )
  7. func (this *Modules) RegisterAction_IndexUserSignIn() *Action {
  8. return this.newAction(AInfo{
  9. WantDB: true,
  10. Mount: "index-user-sign-in",
  11. }, func(wrap *wrapper.Wrapper) {
  12. pf_email := utils.Trim(wrap.R.FormValue("email"))
  13. pf_password := utils.Trim(wrap.R.FormValue("password"))
  14. if pf_email == "" {
  15. wrap.MsgError(`Please specify user email`)
  16. return
  17. }
  18. if !utils.IsValidEmail(pf_email) {
  19. wrap.MsgError(`Please specify correct user email`)
  20. return
  21. }
  22. if pf_password == "" {
  23. wrap.MsgError(`Please specify user password`)
  24. return
  25. }
  26. if wrap.S.GetInt("UserId", 0) > 0 {
  27. wrap.MsgError(`You already logined`)
  28. return
  29. }
  30. var user_id int
  31. err := wrap.DB.QueryRow(
  32. `SELECT
  33. id
  34. FROM
  35. users
  36. WHERE
  37. email = ? and
  38. password = MD5(?) and
  39. admin = 1 and
  40. active = 1
  41. LIMIT 1;`,
  42. pf_email,
  43. pf_password,
  44. ).Scan(
  45. &user_id,
  46. )
  47. if err != nil && err != sqlw.ErrNoRows {
  48. wrap.LogCpError(&err)
  49. wrap.MsgError(err.Error())
  50. return
  51. }
  52. if err == sqlw.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. }