Add tokenRefresh event (#244)
* Add tokenRefresh event * Create slow-oranges-serve.md * update docs
This commit is contained in:
parent
d4169dc35a
commit
f809368e4f
6 changed files with 74 additions and 1 deletions
12
.changeset/slow-oranges-serve.md
Normal file
12
.changeset/slow-oranges-serve.md
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
---
|
||||||
|
"@saleor/app-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Added `tokenRefresh` event to AppBridge.
|
||||||
|
|
||||||
|
It's meant to be triggered by dashboard, when token is refreshed.
|
||||||
|
Apps that use new AppBridge will receive fresh token.
|
||||||
|
|
||||||
|
This fixes [this issue](https://github.com/saleor/saleor-app-sdk/issues/222)
|
||||||
|
|
||||||
|
For Saleor Cloud where token lives for 24h it was rare, but Saleor can be set to have any token duration, causing app to fail fast.
|
|
@ -128,6 +128,7 @@ appBridge.unsubscribeAll();
|
||||||
| `redirect` | Fired when Dashboard changes a subpath within the app path |
|
| `redirect` | Fired when Dashboard changes a subpath within the app path |
|
||||||
| `theme` | Fired when Dashboard changes the theme |
|
| `theme` | Fired when Dashboard changes the theme |
|
||||||
| `localeChanged` | Fired when Dashboard changes locale (and passes locale code in payload) |
|
| `localeChanged` | Fired when Dashboard changes locale (and passes locale code in payload) |
|
||||||
|
| `tokenRefresh` | Fired when Dashboard receives a new auth token and passes it to the app |
|
||||||
|
|
||||||
See [source code for detailed payload](./src/app-bridge/events.ts)
|
See [source code for detailed payload](./src/app-bridge/events.ts)
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import {
|
||||||
actions,
|
actions,
|
||||||
ActionType,
|
ActionType,
|
||||||
AppBridge,
|
AppBridge,
|
||||||
|
DashboardEventFactory,
|
||||||
DispatchResponseEvent,
|
DispatchResponseEvent,
|
||||||
HandshakeEvent,
|
HandshakeEvent,
|
||||||
ThemeEvent,
|
ThemeEvent,
|
||||||
|
@ -286,4 +287,30 @@ describe("AppBridge", () => {
|
||||||
|
|
||||||
appBridge = new AppBridge({ autoNotifyReady: true });
|
appBridge = new AppBridge({ autoNotifyReady: true });
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
it("Overwrites token after tokenRefresh action is triggered", () => {
|
||||||
|
const tokenRefreshEvent = DashboardEventFactory.createTokenRefreshEvent("new-token");
|
||||||
|
|
||||||
|
expect(appBridge.getState().token).toBeUndefined();
|
||||||
|
|
||||||
|
fireEvent(
|
||||||
|
window,
|
||||||
|
new MessageEvent("message", {
|
||||||
|
data: handshakeEvent,
|
||||||
|
origin,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(appBridge.getState().token).toEqual(handshakeEvent.payload.token);
|
||||||
|
|
||||||
|
fireEvent(
|
||||||
|
window,
|
||||||
|
new MessageEvent("message", {
|
||||||
|
data: tokenRefreshEvent,
|
||||||
|
origin,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(appBridge.getState().token).toEqual(tokenRefreshEvent.payload.token);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -52,6 +52,12 @@ function eventStateReducer(state: AppBridgeState, event: Events) {
|
||||||
locale: event.payload.locale,
|
locale: event.payload.locale,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
case EventType.tokenRefresh: {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
token: event.payload.token,
|
||||||
|
};
|
||||||
|
}
|
||||||
case EventType.response: {
|
case EventType.response: {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
@ -72,6 +78,7 @@ const createEmptySubscribeMap = (): SubscribeMap => ({
|
||||||
redirect: {},
|
redirect: {},
|
||||||
theme: {},
|
theme: {},
|
||||||
localeChanged: {},
|
localeChanged: {},
|
||||||
|
tokenRefresh: {},
|
||||||
});
|
});
|
||||||
|
|
||||||
export type AppBridgeOptions = {
|
export type AppBridgeOptions = {
|
||||||
|
|
|
@ -49,4 +49,13 @@ describe("DashboardEventFactory", () => {
|
||||||
type: "localeChanged",
|
type: "localeChanged",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Creates token refresh event", () => {
|
||||||
|
expect(DashboardEventFactory.createTokenRefreshEvent("TOKEN")).toEqual({
|
||||||
|
payload: {
|
||||||
|
token: "TOKEN",
|
||||||
|
},
|
||||||
|
type: "tokenRefresh",
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,6 +9,7 @@ export const EventType = {
|
||||||
redirect: "redirect",
|
redirect: "redirect",
|
||||||
theme: "theme",
|
theme: "theme",
|
||||||
localeChanged: "localeChanged",
|
localeChanged: "localeChanged",
|
||||||
|
tokenRefresh: "tokenRefresh",
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export type EventType = Values<typeof EventType>;
|
export type EventType = Values<typeof EventType>;
|
||||||
|
@ -56,12 +57,20 @@ export type LocaleChangedEvent = Event<
|
||||||
}
|
}
|
||||||
>;
|
>;
|
||||||
|
|
||||||
|
export type TokenRefreshEvent = Event<
|
||||||
|
"tokenRefresh",
|
||||||
|
{
|
||||||
|
token: string;
|
||||||
|
}
|
||||||
|
>;
|
||||||
|
|
||||||
export type Events =
|
export type Events =
|
||||||
| HandshakeEvent
|
| HandshakeEvent
|
||||||
| DispatchResponseEvent
|
| DispatchResponseEvent
|
||||||
| RedirectEvent
|
| RedirectEvent
|
||||||
| ThemeEvent
|
| ThemeEvent
|
||||||
| LocaleChangedEvent;
|
| LocaleChangedEvent
|
||||||
|
| TokenRefreshEvent;
|
||||||
|
|
||||||
export type PayloadOfEvent<
|
export type PayloadOfEvent<
|
||||||
TEventType extends EventType,
|
TEventType extends EventType,
|
||||||
|
@ -112,4 +121,12 @@ export const DashboardEventFactory = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
createTokenRefreshEvent(newToken: string): TokenRefreshEvent {
|
||||||
|
return {
|
||||||
|
type: "tokenRefresh",
|
||||||
|
payload: {
|
||||||
|
token: newToken,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue