Skip to content

Commit

Permalink
readme: add jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Jan 30, 2024
1 parent 8ffc9c8 commit 40719e9
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import {
Application,
ConnectionHandle,
broadcaster,
identificator,
} from "@anycable/serverless-js";

// Some custom authentication logic
import { verifyToken } from "./auth";

// The identifiers type describe connection identifiers—e.g., user ID, username, etc.
export type CableIdentifiers = {
userId: string;
Expand Down Expand Up @@ -159,6 +161,25 @@ export const broadcastTo = broadcaster(broadcastURL, broadcastToken);

Currently, this package only supports broadcasting over HTTP. However, AnyCable provides different [broadcasting adapters](https://docs.anycable.io/ruby/broadcast_adapters) (e.g., Redis, NATS, etc.) that you can integrate yourself.

### Using AnyCable JWT

You can use [AnyCable JWT](https://docs.anycable.io/anycable-go/jwt_identification) and perform authentication and identification fully at the AnyCable side without calling the `connect` callback. For that, you can use the identificator object and generate auth tokens with it:

```js
import { identificator } from "@anycable/serverless-js";

const jwtSecret = "very-secret";
const jwtTTL = "1h";

export const identifier = identificator(jwtSecret, jwtTTL);

// Then, somewhere in your code, generate a token and provide it to the client
const userId = authenticatedUser.id;
const token = await identifier.generateToken({ userId });

const cableURL = `${CABLE_URL}?jid=${token}`
```

### HTTP handlers

To glue our HTTP layer with the channels, we need to configure HTTP handlers. Below you can find an examples for popular serverless platforms.
Expand Down

0 comments on commit 40719e9

Please sign in to comment.