register.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package users
  2. import (
  3. "log"
  4. "strings"
  5. "git.ali33.ru/fcg-xvii/go-tools/json"
  6. "git.ali33.ru/fcg-xvii/rest"
  7. "git.ali33.ru/fcg-xvii/rest/example_chat/chat"
  8. )
  9. type Register struct {
  10. Name string `rest:"require"`
  11. Password string `rest:"require"`
  12. }
  13. func (s *Register) Validate(req rest.IRequestIn) rest.IRequestOut {
  14. log.Println(s.Name, s.Password)
  15. s.Name = strings.TrimSpace(s.Name)
  16. if len(s.Name) < 3 {
  17. return req.OutError(rest.ErrorFiled("name", "minimum 3 symbols"))
  18. }
  19. if len(s.Password) < 5 {
  20. return req.OutError(rest.ErrorFiled("name", "minimum 5 symbols"))
  21. }
  22. return nil
  23. }
  24. func (s *Register) Execute(req rest.IRequestIn) rest.IRequestOut {
  25. core := req.RCore().(*chat.Chat)
  26. user := core.Register(s.Name, s.Password)
  27. fields, err := rest.Fields(user, nil)
  28. if err != nil {
  29. return req.OutError(err)
  30. }
  31. authMap := json.Map{
  32. "id": user.ID,
  33. }
  34. token, tErr := req.GenerateToken(authMap, 0)
  35. if err != nil {
  36. return req.OutError(rest.ErrorMessage("ErrTokenGenerate", tErr.Error()))
  37. }
  38. req.SetAuth(json.Map{
  39. "user": user,
  40. "token": token,
  41. })
  42. //time.Sleep(time.Second * 5)
  43. return req.OutSuccess(fields, nil)
  44. }