question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

documentation request: working example for custom authorizer

See original GitHub issue

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

The README mentions an authorizer but does not provide much detail about options or the expected function signature of callback. Even if these had types, developers without TypeScript would still be left wondering.

What is the expected behavior?

Some description of what to do with options and why the callback receives false (when would it be true?) would be nice. Maybe a short example that uses fetch would be illustrative?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
cainlevycommented, Mar 20, 2018

@skellock That matches where I ended up as well. Here’s another version:

new Pusher(PUSHER_KEY, {
    cluster: PUSHER_CLUSTER,
    authorizer: (channel, _options) => ({
    authorize: async (socketID, callback) => {
        try {
            const authData = await getPusherAuth(name, socketID);

            callback(false, authData);
        } catch (e) {
            callback(true);
        }
    }
})

This is my best understanding of the authorizer API expressed as a TypeScript interface:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/9f4c75126167d0d8af759f58405d53d983e94ad0/types/pusher-js/index.d.ts#L126-L128

7reactions
skellockcommented, Mar 20, 2018

Apologies in advance for being +1 guy, but this really needs some love.

For future travelers of the internet, here’s what I ended up doing that worked for me.

const PUSHER_KEY = 'abcdef0123abcdef0123' // your 20 hex digits api key
const PUSHER_CLUSTER = '' // e.g. 'mt1'

new Pusher(PUSHER_KEY, {
  cluster: PUSHER_CLUSTER,
  encrypted: true,
  authorizer: ({ name }) => ({
    authorize: async (socketId, callback) => {
      // acquire the token somehow (fetch, or whatever)
      // let pretend this returns a string or null
      // a channel token looks like this:  '<20 hex digits>:<64 hex digits>'
      const channelToken = await loadPusherToken(name)

      if (channelToken) {
        // tell pusher about the token with this format:
        callback(false, { auth: channelToken })
      } else {
        // tell pusher we fail... null is probably not required, i just need symmetry.  :|
        callback(true, null)
      }
    },
  }),
})

FWIW, this was using pusher-js@4.2.2 and react-native@0.53.0.

Docs matter. And given my track record writing 'em, I’m probably the worst person to be dishing up that advice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use API Gateway Lambda authorizers - AWS Documentation
A Lambda authorizer (formerly known as a custom authorizer) is an API Gateway feature that uses a Lambda function to control access to...
Read more >
The Complete Guide to Custom Authorizers with AWS ...
In this post, you'll learn about using API Gateway custom authorizers. This post covers: Background on custom authorizers and their benefits ...
Read more >
Working with Amazon Lambda authorizers for HTTP APIs
Working with Amazon Lambda authorizers for HTTP APIs. PDFRSS. You use a Lambda authorizer to use a Lambda function to control access to...
Read more >
API Gateway LAMBDA AUTHORIZER (Custom Authorizer)
Hey When building serverless APIs with AWS Lambda and API Gateway, one of the most critical questions is how to secure the API....
Read more >
Secure your API Gateway with Lambda Authorizer - YouTube
Using AWS API Gateway and Lambda based authorizers, we can secure our API Gateway REST endpoint. Learn how to do it in this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found