From 40719e9edf802b4fdf6b3604b5bd0a79933934ff Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Tue, 30 Jan 2024 11:56:38 -0800 Subject: [PATCH] readme: add jwt --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 00f4393..66dbebc 100644 --- a/README.md +++ b/README.md @@ -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; @@ -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.