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
42 lines
762 B
package users
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"git.aiterp.net/rpdata/logbot3/internal/api"
|
|
"git.aiterp.net/rpdata/logbot3/internal/models"
|
|
)
|
|
|
|
type checkTokenResult struct {
|
|
Token struct {
|
|
User models.User `json:"user"`
|
|
} `json:"token"`
|
|
}
|
|
|
|
var checkTokenGQL = `
|
|
query CheckTokenQuery {
|
|
token {
|
|
user {
|
|
id
|
|
permissions
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
// CheckToken checks the own token and returns the user information.
|
|
func CheckToken(ctx context.Context) (models.User, error) {
|
|
data, err := api.Global().Query(ctx, checkTokenGQL, nil, []string{"member"})
|
|
if err != nil {
|
|
return models.User{}, err
|
|
}
|
|
|
|
result := checkTokenResult{}
|
|
err = json.Unmarshal(data, &result)
|
|
if err != nil {
|
|
return models.User{}, err
|
|
}
|
|
|
|
return result.Token.User, nil
|
|
}
|