auth.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package users
  2. import (
  3. "log"
  4. "git.ali33.ru/fcg-xvii/go-tools/json"
  5. "git.ali33.ru/fcg-xvii/rest"
  6. "git.ali33.ru/fcg-xvii/rest/example_chat/chat"
  7. )
  8. type Auth struct {
  9. Name string `rest:"required"`
  10. Password string `rest:"required"`
  11. }
  12. func (s *Auth) Validate(req rest.IRequestIn) rest.IRequestOut {
  13. log.Println("VALIDATE")
  14. if req.Auth() == nil {
  15. return req.OutError(rest.ErrorMessage("ErrAuth", "already auth"))
  16. }
  17. return nil
  18. }
  19. func (s *Auth) Execute(req rest.IRequestIn) rest.IRequestOut {
  20. log.Println("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO")
  21. core := req.RCore().(*chat.Chat)
  22. user, check := core.Auth(s.Name, s.Password)
  23. if !check {
  24. return req.OutError(rest.ErrorMessage("ErrAuthData", "user is not found"))
  25. }
  26. authMap := json.Map{
  27. "id": user.ID,
  28. }
  29. token, err := req.GenerateToken(authMap, 0)
  30. if err != nil {
  31. return req.OutError(rest.ErrorMessage("ErrTokenGenerate", err.Error()))
  32. }
  33. req.SetAuth(authMap)
  34. return req.OutSuccess(
  35. json.Map{
  36. "user": user,
  37. "token": token,
  38. },
  39. nil,
  40. )
  41. }