Skip to content

Commit

Permalink
fix README and code examples for the user authentication (#85)
Browse files Browse the repository at this point in the history
* fix README and code examples for the user authentication

* Bump to version 5.1.1

Co-authored-by: Pusher CI <[email protected]>
  • Loading branch information
agatav and pusher-ci authored Oct 24, 2022
1 parent 8c86c48 commit da1cab5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 5.1.1

- [CHANGED] readme example for user authentication

## 5.1.0

* [ADDED] SendToUser method
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ For more information see our [docs](http://pusher.com/docs/authenticating_users)
| Argument | Description |
| :-: | :-: |
| params `[]byte` | The request body sent by the client |
| userData `map[string]interface{}` | The map containing arbitrary user data. It must contain at least an `id` field with the user's id as a string. See below. |

###### Arbitrary User Data

```go
userData := map[string]interface{} { "id": "1234", "twitter": "jamiepatel" }
```

| Return Value | Description |
| :-: | :-: |
Expand All @@ -411,7 +418,8 @@ For more information see our [docs](http://pusher.com/docs/authenticating_users)
```go
func pusherUserAuth(res http.ResponseWriter, req *http.Request) {
params, _ := ioutil.ReadAll(req.Body)
response, err := pusherClient.AuthenticateUser(params)
userData := map[string]interface{} { "id": "1234", "twitter": "jamiepatel" }
response, err := pusherClient.AuthenticateUser(params, userData)
if err != nil {
panic(err)
}
Expand Down
21 changes: 10 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var pusherPathRegex = regexp.MustCompile("^/apps/([0-9]+)$")
var maxTriggerableChannels = 100

const (
libraryVersion = "5.1.0"
libraryVersion = "5.1.1"
libraryName = "pusher-http-go"
)

Expand Down Expand Up @@ -65,7 +65,6 @@ type Client struct {
ClientFromURL allows client instantiation from a specially-crafted Pusher URL.
c := pusher.ClientFromURL("http://key:[email protected]/apps/app_id")
*/
func ClientFromURL(serverURL string) (*Client, error) {
url2, err := url.Parse(serverURL)
Expand Down Expand Up @@ -104,8 +103,8 @@ func ClientFromURL(serverURL string) (*Client, error) {
ClientFromEnv allows instantiation of a client from an environment variable.
This is particularly relevant if you are using Pusher as a Heroku add-on,
which stores credentials in a `"PUSHER_URL"` environment variable. For example:
client := pusher.ClientFromEnv("PUSHER_URL")
client := pusher.ClientFromEnv("PUSHER_URL")
*/
func ClientFromEnv(key string) (*Client, error) {
url := os.Getenv(key)
Expand Down Expand Up @@ -139,7 +138,6 @@ be marshallable into JSON.
data := map[string]string{"hello": "world"}
client.Trigger("greeting_channel", "say_hello", data)
*/
func (c *Client) Trigger(channel string, eventName string, data interface{}) error {
_, err := c.validateChannelsAndTrigger([]string{channel}, eventName, data, TriggerParams{})
Expand Down Expand Up @@ -201,6 +199,7 @@ func (c *Client) TriggerWithParams(
/*
TriggerMulti is the same as `client.Trigger`, except one passes in a slice of
`channels` as the first parameter. The maximum length of channels is 100.
client.TriggerMulti([]string{"a_channel", "another_channel"}, "event", data)
*/
func (c *Client) TriggerMulti(channels []string, eventName string, data interface{}) error {
Expand All @@ -226,6 +225,7 @@ func (c *Client) TriggerMultiWithParams(
TriggerExclusive triggers an event excluding a recipient whose connection has
the `socket_id` you specify here from receiving the event.
You can read more here: http://pusher.com/docs/duplicates.
client.TriggerExclusive("a_channel", "event", data, "123.12")
Deprecated: use TriggerWithParams instead.
Expand All @@ -240,6 +240,7 @@ func (c *Client) TriggerExclusive(channel string, eventName string, data interfa
TriggerMultiExclusive triggers an event to multiple channels excluding a
recipient whose connection has the `socket_id` you specify here from receiving
the event on any of the channels.
client.TriggerMultiExclusive([]string{"a_channel", "another_channel"}, "event", data, "123.12")
Deprecated: use TriggerMultiWithParams instead.
Expand Down Expand Up @@ -466,7 +467,6 @@ method the channel name.
users, err := client.GetChannelUsers("presence-chatroom")
//users=> &{List:[{ID:13} {ID:90}]}
*/
func (c *Client) GetChannelUsers(name string) (*Users, error) {
path := fmt.Sprintf("/apps/%s/channels/%s/users", c.AppID, name)
Expand All @@ -482,10 +482,10 @@ func (c *Client) GetChannelUsers(name string) (*Users, error) {
}

/*
AuthenticateUser allows you to authenticate a user s subscription to a
private channel. It returns an authentication signature to send back to the client
AuthenticateUser allows you to authenticate a user's connection.
It returns an authentication signature to send back to the client
and authenticate them. In order to identify a user, this method acceps a map containing
arbitrary user data. It must contain at least and id field with the user's id as a string.
arbitrary user data. It must contain at least an id field with the user's id as a string.
For more information see our docs: http://pusher.com/docs/authenticating_users.
Expand All @@ -499,7 +499,8 @@ to send back to the client.
func pusherUserAuth(res http.ResponseWriter, req *http.Request) {
params, _ := ioutil.ReadAll(req.Body)
response, err := client.AuthenticateUser(params)
userData := map[string]interface{} { "id": "1234", "twitter": "jamiepatel" }
response, err := client.AuthenticateUser(params, userData)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -680,8 +681,6 @@ occupied or vacated, members being added or removed in presence-channels, or
after client-originated events. For more information see
https://pusher.com/docs/webhooks.
If the webhook is valid, a `*pusher.Webhook* will be returned, and the `err`
value will be nil. If it is invalid, the first return value will be nil, and an
error will be passed.
Expand Down

0 comments on commit da1cab5

Please sign in to comment.