Subscriptions - Configuring Auth with 8base SDK

Issue Description: What’s happening?

When using the following subscription with the SDK I get an unknown error back when a mutation occurs.

subscription ($id: ID!) {
  Forms(filter: {
    node: { id: { equals: $id}}
    mutation_in: [update]
  }) {
    node { id }
    updatedFields
    mutation
  }
}

Reproduce the Issue: What steps can someone take to replicate the problem?

  1. Set up the sdk (or a ws client)
  2. Open a subscription
api.subscription(<schema above>, {
      variables: { id: "123"},
      data: console.log,
      error: console.error,
    })
  1. Make an update on a form record
  2. An error comes back as a log
{code: "undefined"
details: {error: "Something went wrong. Please contact support@8base.com."}
message: "Something went wrong. Please contact support@8base.com."}

Expected Behavior: What did you expect to happen?

To get the mutation event.

Actual Behavior: What actually happened?

An unknown error occurs.

Keep me posted, looking forward to using subscriptions. We’d like to show off features based on it in a few days to partners.

Thanks for the help,
Gabin

Hi,

I’ve checked the logs and noticed that we return wrong error type in this case. It supposed to be NotAuthorizedError and not generic error message. It’s happening cos you are subscribing as a “guest”.

We are working on a fix to return the correct error.

2 Likes

Ok, I see. No shit it didn’t work :sweat_smile: I somehow felt like the Authorization header configured through the API config of the SDK was gonna be re-used. Silly idea.

Looking at the source code I found the solution which is to configure a connection param for the subscription with a key token with for value your auth token.

Example of SDK config:

export const { api, auth } = eightBase.configure({
  workspaceId: process.env.VUE_APP_8BASE_WORKSPACE_ID,
  Api: {
    headers: () => {
      const token = <RETRIEVE-CACHED-TOKEN>
  
      if (<TOKEN-HASNT-EXPIRED>) {
        return {
          Authorization: `Bearer ${token}`,
        }
      }
  
      return {}
    },
    subscription: {
      connectionParams() {
        const token = <RETRIEVE-CACHED-TOKEN>
  
        if (<TOKEN-HASNT-EXPIRED>) {
          return {
            token,
          }
        }
  
        return {}
      },
    },
  },
})
1 Like