JWT User Authentication

JWT is one of the convenient ways to authenticate users with external apps. AnnounceKit supports JSON Web Tokens for authenticating a user in the widget and on your Portal.

1. Get your secret key

First, you need your workspace's secret key, which signs the user data in the token. Remember, this key is secret: use it only on the server side of your application.

  1. Open Settings and go to Privacy & Access.
  2. Find the JWT card.
  3. Copy the value of JWT secret.

Here is the JWT card, with the secret hidden.

The JWT card on the Privacy & Access settings page, with the JWT secret field and the Login redirect URL field

Only a workspace member who can manage settings sees the secret.

2. Generate tokens on your server

Now you can generate a signed token, user_token, which holds the user properties id, name and email. You can also include an optional data object with custom fields (for example { "plan": "pro", "role": "admin" }). These are used for segmentation, just like the data option in the widget config. Make sure the HS256 algorithm is used and included in the JWT Header. You can use any library suitable for your codebase.

Node.js code that signs a user_token with the jsonwebtoken library, the secret key and the HS256 algorithm

3. Authenticate the user

Now that you have the signed user_token, pass it in your widget configuration, or as the user_token query parameter in your Portal's address, for example:
https://announcekit.app/acmeco/updates?user_token=eyJhbGciOiJIU6IkpXVCJ9.ey...

In the widget, user_token sits next to the other keys of the widget code:

The AnnounceKit widget embed code with a user_token key added to the widget configuration

When a token is sent, AnnounceKit checks it against your JWT secret. A token that does not verify is ignored, and the user is not identified.

Optional: send visitors to your login page

Suppose a visitor who is not signed in tries to open your private Portal. You can send them to your application to sign in, then back to the Portal with a token.

  1. Open Settings and go to Privacy & Access.
  2. In Private portal, turn on Private portal, then Require JWT.
  3. In the JWT card, enter your Login redirect URL.
  4. Click Save.

Here is the Private portal card with Require JWT turned on.

The Private portal card on the Privacy & Access page, with Private portal and Require JWT turned on

The Login redirect URL should point to the route of your application that generates the token described in step 2. AnnounceKit adds a redirect_url parameter to it, which holds your Portal's address. After generating the token, combine that redirect_url with the user_token and send the visitor there.

Was this helpful?