JWT User Authentication
JSON Web Tokens (JWT) are a convenient way to authenticate your users with external apps. AnnounceKit accepts JWTs to identify a user in the widget and on your Portal.
1. Get your secret key
Copy the JWT secret from the JWT card in Settings > Privacy & Access. It signs the user data. Keep it secret and use it only on your server.
2. Generate tokens on your server
Now you can generate a signed token, user_token, with the user properties id, name and email. Sign it with the HS256 algorithm (named in the JWT header). You can use any library for your language.
3. Authenticate the user
Pass the signed user_token in your widget configuration, or as the user_token query parameter of your Portal URL.
https://announcekit.app/acmeco/updates?user_token=eyJhbGciOiJIU6IkpXVCJ9.ey...
Custom User Data
To send segmentation data in the token, add a data object next to the user properties when you sign it. Its keys work like the widget’s data option (Segmentation):
import * as jsonwebtoken from "jsonwebtoken";
const user = await getCurrentUser();
const YOUR_SECRET_KEY = "YOUR_SECRET_KEY";
const user_token = jsonwebtoken.sign(
{
id: user.id,
email: user.email,
name: user.display_name,
data: {
plan: "pro"
}
},
YOUR_SECRET_KEY,
{ algorithm: "HS256", expiresIn: "24 hours" }
);
Optional step: a private Portal that requires sign-in
This applies when your Portal is private and Require JWT is on (Settings > Privacy & Access, part of the Private & Authenticated add-on). When a visitor without a token opens the Portal, AnnounceKit sends them to the Login redirect URL you set in the JWT card, with a redirect_url parameter holding the Portal page they asked for.
The Login redirect URL should point to a route of your application that signs the user in and generates the token from Step 2. Then send the visitor back to redirect_url with user_token added as a query parameter.