module_index_act_signin.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. wrap.R.Context(),
  33. `SELECT
  34. id
  35. FROM
  36. fave_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 != sqlw.ErrNoRows {
  49. wrap.LogCpError(&err)
  50. wrap.MsgError(err.Error())
  51. return
  52. }
  53. if err == sqlw.ErrNoRows {
  54. wrap.MsgError(`Incorrect email or password`)
  55. return
  56. }
  57. // Save to current session
  58. wrap.S.SetInt("UserId", user_id)
  59. // Reload current page
  60. wrap.Write(`window.location.reload(false);`)
  61. })
  62. }