package users import ( "log" "git.ali33.ru/fcg-xvii/go-tools/json" "git.ali33.ru/fcg-xvii/rest" "git.ali33.ru/fcg-xvii/rest/example_chat/chat" ) type Auth struct { Name string `rest:"required"` Password string `rest:"required"` } func (s *Auth) Validate(req rest.IRequestIn) rest.IRequestOut { log.Println("VALIDATE") if req.Auth() == nil { return req.OutError(rest.ErrorMessage("ErrAuth", "already auth")) } return nil } func (s *Auth) Execute(req rest.IRequestIn) rest.IRequestOut { core := req.RCore().(*chat.Chat) user, check := core.Auth(s.Name, s.Password) if !check { return req.OutError(rest.ErrorMessage("ErrAuthData", "user is not found")) } authMap := json.Map{ "id": user.ID, } token, err := req.GenerateToken(authMap, 0) if err != nil { return req.OutError(rest.ErrorMessage("ErrTokenGenerate", err.Error())) } req.SetAuth(authMap) return req.OutSuccess( json.Map{ "user": user, "token": token, }, nil, ) }