12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package users
- import (
- "log"
- "strings"
- "git.ali33.ru/fcg-xvii/go-tools/json"
- "git.ali33.ru/fcg-xvii/rest"
- "git.ali33.ru/fcg-xvii/rest/example_chat/chat"
- )
- type Register struct {
- Name string `rest:"require"`
- Password string `rest:"require"`
- }
- func (s *Register) Validate(req rest.IRequestIn) rest.IRequestOut {
- log.Println(s.Name, s.Password)
- s.Name = strings.TrimSpace(s.Name)
- if len(s.Name) < 3 {
- return req.OutError(rest.ErrorFiled("name", "minimum 3 symbols"))
- }
- if len(s.Password) < 5 {
- return req.OutError(rest.ErrorFiled("name", "minimum 5 symbols"))
- }
- return nil
- }
- func (s *Register) Execute(req rest.IRequestIn) rest.IRequestOut {
- core := req.RCore().(*chat.Chat)
- user := core.Register(s.Name, s.Password)
- fields, err := rest.Fields(user, nil, nil)
- if err != nil {
- return req.OutError(err)
- }
- authMap := json.Map{
- "id": user.ID,
- }
- token, tErr := req.GenerateToken(authMap, 0)
- if err != nil {
- return req.OutError(rest.ErrorMessage("ErrTokenGenerate", tErr.Error()))
- }
- req.SetAuth(json.Map{
- "user": user,
- "token": token,
- })
- //time.Sleep(time.Second * 5)
- return req.OutSuccess(fields, nil)
- }
|