Skip to content

Commit 748822c

Browse files
authored
feat(react-headless): expose token as client setting (#465)
The v1 user client supports token auth ([code](https://github.com/magicbell/magicbell-js/blob/794f5220f485415863e414bf500d8d982ff66663/packages/magicbell/src/user-client.ts/#L14)), but that was not available in react-headless ([code](https://github.com/magicbell/magicbell-js/blob/794f5220f485415863e414bf500d8d982ff66663/packages/react-headless/src/stores/clientSettings.ts/#L6-L15)). This PR allows to pass the `token` through the `MagicBellProvide` to the `UserClient` where it was already accepted. I've tested this in magicbell/mobile-inbox#21 where I used the preview package and can confirm that notifications load successfully when using a v2 JWT token.
1 parent 794f522 commit 748822c

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

.changeset/tidy-gorillas-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@magicbell/react-headless': minor
3+
---
4+
5+
Added `token` as a way to authenticate the react-headless SDK. Also improved typings to make clear which combination of parameters are expected

packages/react-headless/src/components/MagicBellProvider/MagicBellProvider.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ type StoreConfig = {
1414
defaults?: Partial<Omit<INotificationStore, 'context'>>;
1515
};
1616

17-
export interface MagicBellProviderProps {
18-
apiKey: string;
19-
userEmail?: string;
20-
userExternalId?: string;
21-
userKey?: string;
17+
export type MagicBellProviderProps = {
2218
children: React.ReactElement | React.ReactElement[];
2319
stores?: StoreConfig[];
2420
serverURL?: string;
2521
disableRealtime?: boolean;
26-
}
22+
} & (
23+
| { apiKey: string; userEmail: string; userKey?: string; token?: never }
24+
| { apiKey: string; userExternalId: string; userKey?: string; token?: never }
25+
| { token: string; apiKey?: never }
26+
);
2727

2828
function setupXHR({ serverURL, ...userSettings }: Omit<MagicBellProviderProps, 'children' | 'stores'>) {
2929
const settings = userSettings as ClientSettings;
@@ -54,6 +54,7 @@ function setupStores(storesConfig: StoreConfig[]) {
5454
* @param props.userEmail Email of the user whose notifications will be displayed
5555
* @param props.userExternalId External ID of the user whose notifications will be displayed
5656
* @param props.userKey Computed HMAC of the user whose notifications will be displayed, compute this with the secret of the magicbell project
57+
* @param props.token User token that can be used to authenticate instead of using the apiKey + userEmail/userExternalID combination
5758
* @param props.stores List of stores to be created
5859
* @param props.disableRealtime Disable realtime updates
5960
*

packages/react-headless/src/stores/clientSettings.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type ClientSettings = {
88
userEmail?: string;
99
userExternalId?: string;
1010
userKey?: string;
11+
token?: string;
1112
clientId: string;
1213
serverURL: string;
1314
getClient(): InstanceType<typeof UserClient>;
@@ -30,13 +31,14 @@ const clientSettings = createStore<ClientSettings>((set, get) => {
3031
userEmail: undefined,
3132
userExternalId: undefined,
3233
userKey: undefined,
34+
token: undefined,
3335
clientId: Math.random().toString(36).substring(2) + Date.now(),
3436
serverURL: 'https://api.magicbell.com',
3537
appInfo: undefined,
3638

3739
getClient() {
3840
const state = get();
39-
const key = JSON.stringify([state.apiKey, state.userEmail, state.userExternalId, state.userKey]);
41+
const key = JSON.stringify([state.apiKey, state.userEmail, state.userExternalId, state.userKey, state.token]);
4042

4143
if (key !== _key) {
4244
_key = key;
@@ -45,6 +47,7 @@ const clientSettings = createStore<ClientSettings>((set, get) => {
4547
userEmail: state.userEmail,
4648
userHmac: state.userKey,
4749
apiKey: state.apiKey,
50+
token: state.token,
4851
host: state.serverURL,
4952
appInfo: state.appInfo || {
5053
name: pkg.name,

0 commit comments

Comments
 (0)