Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

Commit ade1c6f

Browse files
authored
support not peristing tokens and last auth (#73)
* store last auth user * some comment
1 parent 3fcfd31 commit ade1c6f

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ Descope SDK is automatically refreshes the session token when it is about to exp
224224
If the Descope project settings are configured to manage tokens in cookies.
225225
you must also configure a custom domain, and set it as the `baseUrl` to the `descope` plugin. See the above [`plugin` usage](#add-descope-plugin-to-your-application) for usage example.
226226
227+
### Token Persistence
228+
229+
Descope stores two tokens: the session token and the refresh token.
230+
231+
- The refresh token is either stored in local storage or an `httpOnly` cookie. This is configurable in the Descope console.
232+
- The session token is stored in either local storage or a JS cookie. This behavior is configurable via the `sessionTokenViaCookie` prop in the Descope plugin.
233+
234+
However, for security reasons, you may choose not to store tokens in the browser. In this case, you can pass `persistTokens: false` to the Descope plugin. This prevents the SDK from storing the tokens in the browser.
235+
236+
Notes:
237+
238+
- You must configure the refresh token to be stored in an `httpOnly` cookie in the Descope console. Otherwise, the refresh token will not be stored, and when the page is refreshed, the user will be logged out.
239+
- You can still retrieve the session token using the `useSession` hook.
240+
241+
### Last User Persistence
242+
243+
Descope stores the last user information in local storage. If you wish to disable this feature, you can pass `storeLastAuthenticatedUser: false` to the Descope plugin. Please note that some features related to the last authenticated user may not function as expected if this behavior is disabled.
244+
227245
### Widgets
228246
229247
Widgets are components that allow you to expose management features for tenant-based implementation. In certain scenarios, your customers may require the capability to perform managerial actions independently, alleviating the necessity to contact you. Widgets serve as a feature enabling you to delegate these capabilities to your customers in a modular manner.

src/Descope.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
:telemetryKey.attr="telemetryKey"
1313
:redirect-url="redirectUrl"
1414
:auto-focus="autoFocus"
15+
:store-last-authenticated-user="storeLastAuthenticatedUser"
1516
:errorTransformer.prop="errorTransformer"
1617
:form.attr="formStr"
1718
:client.attr="clientStr"
@@ -88,7 +89,7 @@ const props = defineProps({
8889
}
8990
});
9091
const emit = defineEmits(['success', 'error', 'ready']);
91-
const { projectId, baseUrl } = useOptions();
92+
const { projectId, baseUrl, storeLastAuthenticatedUser } = useOptions();
9293
const sdk = useDescope();
9394

9495
const formStr = computed(() => (props.form ? JSON.stringify(props.form) : ''));

src/plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export const getSdk = () => externalSdk;
1919
export default {
2020
install: function (app: App, options: Options) {
2121
const sdk = createSdk({
22-
...options,
2322
persistTokens: true,
23+
...options,
2424
autoRefresh: true,
2525
baseHeaders
2626
});

src/sdk.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ const createSdkWrapper = <P extends Parameters<typeof createSdk>[0]>(
99
config: P
1010
) => {
1111
const sdk = createSdk({
12-
...config,
1312
persistTokens: IS_BROWSER as true,
13+
storeLastAuthenticatedUser: IS_BROWSER as true,
14+
...config,
1415
autoRefresh: IS_BROWSER as true
1516
});
1617
globalSdk = sdk;

src/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import type { Ref } from 'vue';
44
export type Options = {
55
projectId: string;
66
baseUrl?: string;
7+
// If true, tokens will be stored on local storage
8+
persistTokens?: boolean;
79
sessionTokenViaCookie?: boolean;
10+
// If true, last authenticated user will be stored on local storage and can accessed with getUser function
11+
storeLastAuthenticatedUser?: boolean;
812
};
913

1014
export type Sdk = ReturnType<typeof createSdk>;

tests/plugin.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ describe('plugin', () => {
2323

2424
plugin.install(app, options);
2525

26+
expect(createSdk).toHaveBeenCalledWith(
27+
expect.objectContaining({
28+
persistTokens: true,
29+
autoRefresh: true,
30+
storeLastAuthenticatedUser: true,
31+
...options
32+
})
33+
);
34+
});
35+
36+
it('should create sdk instance with the custom config', () => {
37+
const provide = jest.fn();
38+
const app = { provide } as any;
39+
const options = {
40+
projectId: 'pid',
41+
baseUrl: 'burl',
42+
sessionTokenViaCookie: true,
43+
persistTokens: false,
44+
storeLastAuthenticatedUser: true
45+
};
46+
47+
plugin.install(app, options);
48+
2649
expect(createSdk).toHaveBeenCalledWith(expect.objectContaining(options));
2750
});
2851

0 commit comments

Comments
 (0)