The new logbot, not committed from the wrong terminal window this time.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
762 B

6 years ago
  1. package users
  2. import (
  3. "context"
  4. "encoding/json"
  5. "git.aiterp.net/rpdata/logbot3/internal/api"
  6. "git.aiterp.net/rpdata/logbot3/internal/models"
  7. )
  8. type checkTokenResult struct {
  9. Token struct {
  10. User models.User `json:"user"`
  11. } `json:"token"`
  12. }
  13. var checkTokenGQL = `
  14. query CheckTokenQuery {
  15. token {
  16. user {
  17. id
  18. permissions
  19. }
  20. }
  21. }
  22. `
  23. // CheckToken checks the own token and returns the user information.
  24. func CheckToken(ctx context.Context) (models.User, error) {
  25. data, err := api.Global().Query(ctx, checkTokenGQL, nil, []string{"member"})
  26. if err != nil {
  27. return models.User{}, err
  28. }
  29. result := checkTokenResult{}
  30. err = json.Unmarshal(data, &result)
  31. if err != nil {
  32. return models.User{}, err
  33. }
  34. return result.Token.User, nil
  35. }