Skip to content
Open
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
1 change: 1 addition & 0 deletions packages/libs/sdk-mixins/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export * from './mixins/cookieConfigMixin';
export * from './mixins/injectNpmLibMixin';
export * from './mixins/injectStyleMixin';
export * from './mixins/cspNonceMixin';
export * from './mixins/storagePrefixMixin';
13 changes: 13 additions & 0 deletions packages/libs/sdk-mixins/src/mixins/storagePrefixMixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createSingletonMixin, compose } from '@descope/sdk-helpers';
import { missingAttrValidator } from './createValidateAttributesMixin/commonValidators';
import { createValidateAttributesMixin } from './createValidateAttributesMixin';

export const storagePrefixMixin = createSingletonMixin(
<T extends CustomElementConstructor>(superclass: T) => {
return class ProjectIdMixinClass extends superclass {
get storagePrefix() {
return this.getAttribute('storage-prefix') || '';
}
};
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const AccessKeyManagementWC = lazy(async () => {
const AccessKeyManagement = React.forwardRef<
HTMLElement,
AccessKeyManagementProps
>(({ logger, tenant, theme, debug, widgetId, styleId }, ref) => {
>(({ logger, tenant, theme, debug, widgetId, styleId, storagePrefix }, ref) => {
const [innerRef, setInnerRef] = useState(null);

useImperativeHandle(ref, () => innerRef);
Expand All @@ -37,6 +37,7 @@ const AccessKeyManagement = React.forwardRef<
baseStaticUrl={baseStaticUrl}
baseCdnUrl={baseCdnUrl}
innerRef={setInnerRef}
storagePrefix={storagePrefix}
{...{
// attributes
'theme.attr': theme,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ApplicationsPortalWC = lazy(async () => {
const ApplicationsPortal = React.forwardRef<
HTMLElement,
ApplicationsPortalProps
>(({ logger, theme, debug, widgetId, styleId }, ref) => {
>(({ logger, theme, debug, widgetId, styleId, storagePrefix }, ref) => {
const [innerRef, setInnerRef] = useState(null);

useImperativeHandle(ref, () => innerRef);
Expand All @@ -36,6 +36,7 @@ const ApplicationsPortal = React.forwardRef<
baseStaticUrl={baseStaticUrl}
baseCdnUrl={baseCdnUrl}
innerRef={setInnerRef}
storagePrefix={storagePrefix}
{...{
// attributes
'theme.attr': theme,
Expand Down
3 changes: 2 additions & 1 deletion packages/sdks/react-sdk/src/components/AuditManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const AuditManagementWC = lazy(async () => {
});

const AuditManagement = React.forwardRef<HTMLElement, AuditManagementProps>(
({ logger, tenant, theme, debug, widgetId, styleId }, ref) => {
({ logger, tenant, theme, debug, widgetId, styleId, storagePrefix }, ref) => {
const [innerRef, setInnerRef] = useState(null);

useImperativeHandle(ref, () => innerRef);
Expand All @@ -35,6 +35,7 @@ const AuditManagement = React.forwardRef<HTMLElement, AuditManagementProps>(
baseStaticUrl={baseStaticUrl}
baseCdnUrl={baseCdnUrl}
innerRef={setInnerRef}
storagePrefix={storagePrefix}
{...{
// attributes
'theme.attr': theme,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ interface IAuthProviderProps {
// Use this option if the authentication is done via cookie, and configured with a different name
// Currently, this is done using Descope Flows
refreshCookieName?: string;
// A prefix to add to the key of the local storage when persisting tokens
storagePrefix?: string;
// Function to get external token, for seamless migration from external system
getExternalToken?: () => Promise<string>;
children?: React.ReactNode;
Expand All @@ -58,6 +60,7 @@ const AuthProvider: FC<IAuthProviderProps> = ({
refreshCookieName = '',
getExternalToken = undefined,
children = undefined,
storagePrefix = undefined,
}) => {
const [user, setUser] = useState<User>();
const [session, setSession] = useState<string>();
Expand All @@ -82,6 +85,7 @@ const AuthProvider: FC<IAuthProviderProps> = ({
keepLastAuthenticatedUserAfterLogout,
refreshCookieName,
getExternalToken,
storagePrefix,
});

useEffect(() => {
Expand Down
4 changes: 4 additions & 0 deletions packages/sdks/react-sdk/src/components/AuthProvider/useSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config = Pick<
| 'keepLastAuthenticatedUserAfterLogout'
| 'refreshCookieName'
| 'getExternalToken'
| 'storagePrefix'
>;

export default ({
Expand All @@ -27,6 +28,7 @@ export default ({
storeLastAuthenticatedUser,
keepLastAuthenticatedUserAfterLogout,
getExternalToken,
storagePrefix,
}: Config): ReturnType<typeof createSdk> =>
useMemo(() => {
if (!projectId) {
Expand All @@ -44,6 +46,7 @@ export default ({
storeLastAuthenticatedUser,
keepLastAuthenticatedUserAfterLogout,
getExternalToken,
storagePrefix,
});
}, [
projectId,
Expand All @@ -56,4 +59,5 @@ export default ({
// See: https://github.com/descope/etc/issues/11965
JSON.stringify(sessionTokenViaCookie),
getExternalToken,
storagePrefix,
]);
3 changes: 2 additions & 1 deletion packages/sdks/react-sdk/src/components/RoleManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const RoleManagementWC = lazy(async () => {
});

const RoleManagement = React.forwardRef<HTMLElement, RoleManagementProps>(
({ logger, tenant, theme, debug, widgetId, styleId }, ref) => {
({ logger, tenant, theme, debug, widgetId, styleId, storagePrefix }, ref) => {
const [innerRef, setInnerRef] = useState(null);

useImperativeHandle(ref, () => innerRef);
Expand All @@ -35,6 +35,7 @@ const RoleManagement = React.forwardRef<HTMLElement, RoleManagementProps>(
baseStaticUrl={baseStaticUrl}
baseCdnUrl={baseCdnUrl}
innerRef={setInnerRef}
storagePrefix={storagePrefix}
{...{
// attributes
'theme.attr': theme,
Expand Down
3 changes: 2 additions & 1 deletion packages/sdks/react-sdk/src/components/TenantProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const TenantProfileWC = lazy(async () => {
});

const TenantProfile = React.forwardRef<HTMLElement, TenantProfileProps>(
({ logger, theme, debug, widgetId, styleId, tenant }, ref) => {
({ logger, theme, debug, widgetId, styleId, tenant, storagePrefix }, ref) => {
const [innerRef, setInnerRef] = useState(null);

useImperativeHandle(ref, () => innerRef);
Expand All @@ -34,6 +34,7 @@ const TenantProfile = React.forwardRef<HTMLElement, TenantProfileProps>(
baseCdnUrl={baseCdnUrl}
styleId={styleId}
tenant={tenant}
storagePrefix={storagePrefix}
ref={setInnerRef}
{...{
'theme.attr': theme,
Expand Down
3 changes: 2 additions & 1 deletion packages/sdks/react-sdk/src/components/UserManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const UserManagementWC = lazy(async () => {
});

const UserManagement = React.forwardRef<HTMLElement, UserManagementProps>(
({ logger, tenant, theme, debug, widgetId, styleId }, ref) => {
({ logger, tenant, theme, debug, widgetId, styleId, storagePrefix }, ref) => {
const [innerRef, setInnerRef] = useState(null);

useImperativeHandle(ref, () => innerRef);
Expand All @@ -34,6 +34,7 @@ const UserManagement = React.forwardRef<HTMLElement, UserManagementProps>(
baseUrl={baseUrl}
baseStaticUrl={baseStaticUrl}
baseCdnUrl={baseCdnUrl}
storagePrefix={storagePrefix}
ref={setInnerRef}
{...{
// attributes
Expand Down
6 changes: 5 additions & 1 deletion packages/sdks/react-sdk/src/components/UserProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ const UserProfileWC = lazy(async () => {
});

const UserProfile = React.forwardRef<HTMLElement, UserProfileProps>(
({ logger, theme, debug, widgetId, onLogout, styleId }, ref) => {
(
{ logger, theme, debug, widgetId, onLogout, styleId, storagePrefix },
ref,
) => {
const [innerRef, setInnerRef] = useState(null);

useImperativeHandle(ref, () => innerRef);
Expand Down Expand Up @@ -71,6 +74,7 @@ const UserProfile = React.forwardRef<HTMLElement, UserProfileProps>(
baseCdnUrl={baseCdnUrl}
styleId={styleId}
ref={setInnerRef}
storagePrefix={storagePrefix}
{...{
// attributes
'theme.attr': theme,
Expand Down
1 change: 1 addition & 0 deletions packages/sdks/react-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type WidgetProps = {
theme?: ThemeOptions;
debug?: boolean;
styleId?: string;
storagePrefix?: string;
};

type FlowResponse = Awaited<ReturnType<Sdk['flow']['next']>>;
Expand Down
17 changes: 17 additions & 0 deletions packages/sdks/react-sdk/test/components/AuthProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,21 @@ describe('AuthProvider', () => {
expect(createSdk).toHaveBeenCalledTimes(1);
});
});

it('Should init sdk config with customized storage prefix option', async () => {
render(
<AuthProvider projectId="pr1" storagePrefix="APP_">
<div>hello</div>
</AuthProvider>,
);

await waitFor(() => {
expect(createSdk).toHaveBeenCalledWith(
expect.objectContaining({
projectId: 'pr1',
storagePrefix: 'APP_',
}),
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
loggerMixin,
observeAttributesMixin,
projectIdMixin,
storagePrefixMixin,
} from '@descope/sdk-mixins';
import { Sdk, createSdk } from '../api/sdk';

Expand All @@ -20,6 +21,7 @@ export const apiMixin = createSingletonMixin(
createValidateAttributesMixin({
tenant: createValidateAttributesMixin.missingAttrValidator,
}),
storagePrefixMixin,
)(superclass);

return class ApiMixinClass extends BaseClass {
Expand All @@ -32,6 +34,7 @@ export const apiMixin = createSingletonMixin(
projectId: this.projectId,
baseUrl: this.baseUrl,
refreshCookieName: this.refreshCookieName,
storagePrefix: this.storagePrefix,
},
this.tenantId,
this.mock === 'true',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
loggerMixin,
observeAttributesMixin,
projectIdMixin,
storagePrefixMixin,
} from '@descope/sdk-mixins';
import { Sdk, createSdk } from '../api/sdk';

Expand All @@ -16,6 +17,7 @@ export const apiMixin = createSingletonMixin(
loggerMixin,
cookieConfigMixin,
baseUrlMixin,
storagePrefixMixin,
)(superclass);

return class ApiMixinClass extends BaseClass {
Expand All @@ -28,6 +30,7 @@ export const apiMixin = createSingletonMixin(
projectId: this.projectId,
baseUrl: this.baseUrl,
refreshCookieName: this.refreshCookieName,
storagePrefix: this.storagePrefix,
},
this.mock === 'true',
this.widgetId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
loggerMixin,
observeAttributesMixin,
projectIdMixin,
storagePrefixMixin,
} from '@descope/sdk-mixins';
import { Sdk, createSdk } from '../api/sdk';

Expand All @@ -20,6 +21,7 @@ export const apiMixin = createSingletonMixin(
createValidateAttributesMixin({
tenant: createValidateAttributesMixin.missingAttrValidator,
}),
storagePrefixMixin,
)(superclass);

return class ApiMixinClass extends BaseClass {
Expand All @@ -32,6 +34,7 @@ export const apiMixin = createSingletonMixin(
projectId: this.projectId,
baseUrl: this.baseUrl,
refreshCookieName: this.refreshCookieName,
storagePrefix: this.storagePrefix,
},
this.tenantId,
this.mock === 'true',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
loggerMixin,
observeAttributesMixin,
projectIdMixin,
storagePrefixMixin,
} from '@descope/sdk-mixins';
import { Sdk, createSdk } from '../api/sdk';

Expand All @@ -20,6 +21,7 @@ export const apiMixin = createSingletonMixin(
createValidateAttributesMixin({
tenant: createValidateAttributesMixin.missingAttrValidator,
}),
storagePrefixMixin,
)(superclass);

return class ApiMixinClass extends BaseClass {
Expand All @@ -32,6 +34,7 @@ export const apiMixin = createSingletonMixin(
projectId: this.projectId,
baseUrl: this.baseUrl,
refreshCookieName: this.refreshCookieName,
storagePrefix: this.storagePrefix,
},
this.tenantId,
this.mock === 'true',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
loggerMixin,
observeAttributesMixin,
projectIdMixin,
storagePrefixMixin,
} from '@descope/sdk-mixins';
import { Sdk, createSdk } from '../api/sdk';

Expand All @@ -16,6 +17,7 @@ export const apiMixin = createSingletonMixin(
loggerMixin,
baseUrlMixin,
cookieConfigMixin,
storagePrefixMixin,
)(superclass);

return class ApiMixinClass extends BaseClass {
Expand All @@ -28,6 +30,7 @@ export const apiMixin = createSingletonMixin(
projectId: this.projectId,
baseUrl: this.baseUrl,
refreshCookieName: this.refreshCookieName,
storagePrefix: this.storagePrefix,
},
this.tenantId,
this.mock === 'true',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
loggerMixin,
observeAttributesMixin,
projectIdMixin,
storagePrefixMixin,
} from '@descope/sdk-mixins';
import { Sdk, createSdk } from '../api/sdk';

Expand All @@ -20,6 +21,7 @@ export const apiMixin = createSingletonMixin(
createValidateAttributesMixin({
tenant: createValidateAttributesMixin.missingAttrValidator,
}),
storagePrefixMixin,
)(superclass);

return class ApiMixinClass extends BaseClass {
Expand All @@ -32,6 +34,7 @@ export const apiMixin = createSingletonMixin(
projectId: this.projectId,
baseUrl: this.baseUrl,
refreshCookieName: this.refreshCookieName,
storagePrefix: this.storagePrefix,
},
this.tenantId,
this.mock === 'true',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
loggerMixin,
observeAttributesMixin,
projectIdMixin,
storagePrefixMixin,
} from '@descope/sdk-mixins';
import { Sdk, createSdk } from '../api/sdk';

Expand All @@ -16,6 +17,7 @@ export const apiMixin = createSingletonMixin(
loggerMixin,
baseUrlMixin,
cookieConfigMixin,
storagePrefixMixin,
)(superclass);

return class ApiMixinClass extends BaseClass {
Expand All @@ -27,7 +29,8 @@ export const apiMixin = createSingletonMixin(
{
projectId: this.projectId,
baseUrl: this.baseUrl,
refreshCookieName: this.refreshCookieName,
refreshCookieName: this.accessKey,
storagePrefix: this.storagePrefix,
},
this.mock === 'true',
this.widgetId,
Expand Down
Loading