Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-gorillas-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@magicbell/react-headless': minor
---

Added `token` as a way to authenticate the react-headless SDK. Also improved typings to make clear which combination of parameters are expected
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ type StoreConfig = {
defaults?: Partial<Omit<INotificationStore, 'context'>>;
};

export interface MagicBellProviderProps {
apiKey: string;
userEmail?: string;
userExternalId?: string;
userKey?: string;
export type MagicBellProviderProps = {
children: React.ReactElement | React.ReactElement[];
stores?: StoreConfig[];
serverURL?: string;
disableRealtime?: boolean;
}
} & (
| { apiKey: string; userEmail: string; userKey?: string; token?: never }
| { apiKey: string; userExternalId: string; userKey?: string; token?: never }
| { token: string; apiKey?: never }
);

function setupXHR({ serverURL, ...userSettings }: Omit<MagicBellProviderProps, 'children' | 'stores'>) {
const settings = userSettings as ClientSettings;
Expand Down Expand Up @@ -54,6 +54,7 @@ function setupStores(storesConfig: StoreConfig[]) {
* @param props.userEmail Email of the user whose notifications will be displayed
* @param props.userExternalId External ID of the user whose notifications will be displayed
* @param props.userKey Computed HMAC of the user whose notifications will be displayed, compute this with the secret of the magicbell project
* @param props.token User token that can be used to authenticate instead of using the apiKey + userEmail/userExternalID combination
* @param props.stores List of stores to be created
* @param props.disableRealtime Disable realtime updates
*
Expand Down
5 changes: 4 additions & 1 deletion packages/react-headless/src/stores/clientSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type ClientSettings = {
userEmail?: string;
userExternalId?: string;
userKey?: string;
token?: string;
clientId: string;
serverURL: string;
getClient(): InstanceType<typeof UserClient>;
Expand All @@ -30,13 +31,14 @@ const clientSettings = createStore<ClientSettings>((set, get) => {
userEmail: undefined,
userExternalId: undefined,
userKey: undefined,
token: undefined,
clientId: Math.random().toString(36).substring(2) + Date.now(),
serverURL: 'https://api.magicbell.com',
appInfo: undefined,

getClient() {
const state = get();
const key = JSON.stringify([state.apiKey, state.userEmail, state.userExternalId, state.userKey]);
const key = JSON.stringify([state.apiKey, state.userEmail, state.userExternalId, state.userKey, state.token]);

if (key !== _key) {
_key = key;
Expand All @@ -45,6 +47,7 @@ const clientSettings = createStore<ClientSettings>((set, get) => {
userEmail: state.userEmail,
userHmac: state.userKey,
apiKey: state.apiKey,
token: state.token,
host: state.serverURL,
appInfo: state.appInfo || {
name: pkg.name,
Expand Down
Loading