Skip to content

Commit 47eb53d

Browse files
authored
RID-8543: Expose getSessionToken() method (#21)
* RID-8543: expose getSessionToken * RID-8543: Update package version, update readme, write about the changes in changelog * RID-8543: Update upload-artifiact to v4, cause v3 is deprecated * RID-8543: Update vite to fix vulnerable package * RID-8543: Bump vite even higher, to fix all vulnerabilities * RID-8543: Bump node version in pipeline * RID-8543: Remove redundant spacebars * RID-8543: Switch invocation of getSessionToken to use ref
1 parent a17d63f commit 47eb53d

9 files changed

Lines changed: 602 additions & 245 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
name: CI
22

33
env:
4-
NODE_VERSION: 18.0.0
4+
NODE_VERSION: 22.14
55
RUNID_VERSION_SUFFIX: ${{ github.run_id }}.${{ github.run_attempt }}
66

77
on:
88
workflow_dispatch:
99
pull_request:
1010
push:
11-
branches: [ main ]
11+
branches: [main]
1212

1313
jobs:
1414
build:
@@ -60,16 +60,16 @@ jobs:
6060
yarn pack --filename ../transmitsecurity-riskid-reactjs-ts-v$PACKAGE_VERSION.tgz
6161
cd ..
6262
- name: Archive NPM package
63-
uses: actions/upload-artifact@v3
63+
uses: actions/upload-artifact@v4
6464
with:
6565
if-no-files-found: error
6666
retention-days: 30
6767
name: react-ts-riskid-npm-package
6868
path: package/transmitsecurity-riskid-reactjs-ts-v*.tgz
6969
- name: Archive failure data
7070
if: ${{ failure() }}
71-
uses: actions/upload-artifact@v3
71+
uses: actions/upload-artifact@v4
7272
with:
7373
name: failure-data
7474
path: |
75-
/home/runner/.npm/_logs
75+
/home/runner/.npm/_logs

.github/workflows/release.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Release Artifact
22

33
env:
4-
NODE_VERSION: 18.0.0
4+
NODE_VERSION: 22.14
55
RUNID_VERSION_SUFFIX: ${{ github.run_id }}.${{ github.run_attempt }}
66

77
on:
@@ -50,7 +50,7 @@ jobs:
5050

5151
- name: Archive failure data
5252
if: ${{ failure() }}
53-
uses: actions/upload-artifact@v3
53+
uses: actions/upload-artifact@v4
5454
with:
5555
name: failure-data
5656
path: |
@@ -67,15 +67,15 @@ jobs:
6767
uses: actions/setup-node@v3
6868
with:
6969
node-version: ${{ env.NODE_VERSION }}
70-
registry-url: 'https://npm.pkg.github.com'
70+
registry-url: "https://npm.pkg.github.com"
7171
always-auth: true
7272

7373
- name: Fetch cached workspace
7474
uses: actions/cache@v3
7575
with:
7676
path: ${{ github.workspace }}
7777
key: ${{ runner.os }}-${{ env.RUNID_VERSION_SUFFIX }}
78-
78+
7979
- name: Enable Corepack
8080
run: corepack enable
8181

@@ -90,7 +90,7 @@ jobs:
9090

9191
- name: Archive failure data
9292
if: ${{ failure() }}
93-
uses: actions/upload-artifact@v3
93+
uses: actions/upload-artifact@v4
9494
with:
9595
name: failure-data
9696
path: |
@@ -107,15 +107,15 @@ jobs:
107107
uses: actions/setup-node@v3
108108
with:
109109
node-version: ${{ env.NODE_VERSION }}
110-
registry-url: 'https://registry.npmjs.org'
110+
registry-url: "https://registry.npmjs.org"
111111
always-auth: true
112112

113113
- name: Fetch cached workspace
114114
uses: actions/cache@v3
115115
with:
116116
path: ${{ github.workspace }}
117117
key: ${{ runner.os }}-${{ env.RUNID_VERSION_SUFFIX }}
118-
118+
119119
- name: Enable Corepack
120120
run: corepack enable
121121

@@ -130,8 +130,8 @@ jobs:
130130

131131
- name: Archive failure data
132132
if: ${{ failure() }}
133-
uses: actions/upload-artifact@v3
133+
uses: actions/upload-artifact@v4
134134
with:
135135
name: failure-data
136136
path: |
137-
/home/runner/.npm/_logs
137+
/home/runner/.npm/_logs

demo/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function App() {
77
const accountProtectionOptions: DRSConfigOptions = {
88
userId: 'demo-user-id',
99
initSuccessLog: 'Detection and Response SDK successfully initialized',
10+
enableSessionToken: true,
1011
};
1112

1213
return (

demo/src/InnerComponent.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTSAccountProtection } from '@transmitsecurity/riskid-reactjs-ts';
22

33
function InnerComponent() {
4-
const { triggerActionEvent, setAuthenticatedUser, clearUser } = useTSAccountProtection();
4+
const { triggerActionEvent, setAuthenticatedUser, clearUser, getSessionToken } = useTSAccountProtection();
55

66
return (
77
<>
@@ -21,6 +21,10 @@ function InnerComponent() {
2121
style={{width: '100px', height: '100px' }}
2222
onClick={() => clearUser()}
2323
>Reset</button>
24+
<button
25+
style={{width: '100px', height: '100px' }}
26+
onClick={getSessionToken}
27+
>Get Session Token</button>
2428
</>
2529
);
2630
};

package/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
4+
## Version 1.2.0
5+
1. Expose `getSessionToken()` method from the SDK.
6+
37
## Version 1.1.1
48
1. Update react quickstart documentation link in package README
59

package/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@ import { TSAccountProtectionProvider } from '@transmitsecurity/riskid-reactjs-ts
3737

3838
## Step 4: Use the React library
3939

40-
The example below demonstrates triggering a login event from a login button, setting and clearing a user.
40+
The example below demonstrates triggering a login event from a login button, setting and clearing a user and obtaining a session token.
4141

4242
- `triggerActionEvent()` receives an action type and returns a response that includes the `actionToken`. To obtain risk recommendations for sensitive actions, your application should report these actions. To do this, add the code below to relevant user interactions (such as the Login button `click` event handler). The library allows reporting on events with the following action types: `login`, `register`, `transaction`, `password_reset`, `logout`, `checkout`, `account_details_change`, `account_auth_change`, `withdraw` or `credits_change`.
4343

4444
- `setAuthenticatedUser()` sets the user context for all subsequent events in the browser session (or until the user is explicitly cleared). It should be set only after you've fully authenticated the user (including, for example, any 2FA that was required). Receives an opaque identifier of the user in your system ([USER_ID] in the snippet), which shouldn't contain any personal info.
4545

4646
- `clearUser()` clears the user context for all subsequent events in the browser session.
47+
48+
- `getSessionToken()` retrieves the current device session token as a string, that can be used to trigger action events via backend API.
4749

4850
```js
4951
import { useTSAccountProtection } from '@transmitsecurity/riskid-reactjs-ts';
5052

5153
function InnerComponent() {
52-
const { triggerActionEvent, setAuthenticatedUser, clearUser } = useTSAccountProtection();
54+
const { triggerActionEvent, setAuthenticatedUser, clearUser, getSessionToken } = useTSAccountProtection();
5355

5456
return (
5557
<>
@@ -69,6 +71,10 @@ function InnerComponent() {
6971
style={{width: '100px', height: '100px' }}
7072
onClick={() => clearUser()}
7173
>Reset</button>
74+
<button
75+
style={{width: '100px', height: '100px' }}
76+
onClick={getSessionToken}
77+
>Get Session Token</button>
7278
</>
7379
);
7480
};

package/TSAccountProtectionProvider.tsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const SDK_LOAD_ERR = 'SDK load error';
1616
const SDK_TRIGGER_ACTION_ERR = 'Error sending action event';
1717
const SDK_AUTHENTICATE_USER_ERR = 'Error authenticating user';
1818
const SDK_CLEAR_USER_ERR = 'Error clearing user';
19+
const SDK_GET_SESSION_TOKEN_ERR = 'Error getting session token';
1920

2021
type ProviderState = DRSConfigOptions & {
2122
initialized: Promise<boolean>;
@@ -68,6 +69,12 @@ type ErrHandler = (err: any) => void;
6869
* A string to log when the sdk initialization completes. If not provided - logging will be skipped.
6970
*/
7071
initSuccessLog?: string;
72+
73+
/**
74+
* Setting that determines if session token is enabled
75+
* Default: false
76+
*/
77+
enableSessionToken?: boolean;
7178
}
7279

7380
interface QuerablePromise extends Promise<any> {
@@ -148,6 +155,7 @@ const buildProviderState = (clientId: string, options?: DRSConfigOptions): Provi
148155
return {
149156
initialized: new Promise((res) => undefined), // making default promise in pending state
150157
clientId,
158+
enableSessionToken: options?.enableSessionToken ?? false,
151159
serverUrl: options?.serverUrl ?? (options?.serverPath || 'https://api.transmitsecurity.io/risk-collect/'),
152160
sdkVersion,
153161
sdkLoadUrl: options?.sdkLoadUrl ?? generateSdkUrl(sdkVersion),
@@ -213,8 +221,8 @@ export function TSAccountProtectionProvider({
213221
const initializedPromise = makeQuerablePromise(providerState.initialized);
214222
if (initializedPromise.status != PromiseStatus.Fulfilled && !window.myTSAccountProtection) {
215223
try {
216-
const serverPath = providerState.serverUrl;
217-
window.myTSAccountProtection = new TSAccountProtection(providerState.clientId, { serverPath });
224+
const { serverUrl: serverPath, enableSessionToken } = providerState;
225+
window.myTSAccountProtection = new TSAccountProtection(providerState.clientId, { serverPath, enableSessionToken});
218226
try {
219227
await window.myTSAccountProtection.init(providerState?.userId);
220228
if (providerState.initSuccessLog) {
@@ -286,6 +294,19 @@ function getClearUserFunc(providerState: ProviderState, providerDispatch: Functi
286294
}
287295
}
288296

297+
function getSessionTokenFunc(providerState: ProviderState) {
298+
return async function getSessionToken(): Promise<string | null> {
299+
if (await providerState.initialized) {
300+
try {
301+
return await window.myTSAccountProtection?.getSessionToken();
302+
} catch (err) {
303+
(providerState.onError as ErrHandler)(buildSdkError(err, SDK_GET_SESSION_TOKEN_ERR));
304+
}
305+
}
306+
return null;
307+
}
308+
}
309+
289310
const useAccountProtectionContext = () => {
290311
const context = useContext(AccountProtectionContext);
291312
if (context === undefined) {
@@ -302,5 +323,6 @@ export const useTSAccountProtection = () => {
302323
triggerActionEvent: getTriggerActionEventFunc(state),
303324
setAuthenticatedUser: getAuthenticatedUserFunc(state, dispatch),
304325
clearUser: getClearUserFunc(state, dispatch),
326+
getSessionToken: getSessionTokenFunc(state),
305327
};
306328
};

package/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@transmitsecurity/riskid-reactjs-ts",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"license": "SEE LICENSE IN LICENSE",
55
"module": "./dist/index.es.js",
66
"main": "./dist/index.cjs.js",
@@ -52,7 +52,7 @@
5252
"react": "17.0.2",
5353
"react-error-overlay": "6.0.9",
5454
"typescript": "4.9.4",
55-
"vite": "2.9.18",
55+
"vite": "6.3.4",
5656
"vite-plugin-dts": "1.7.1",
5757
"vite-plugin-environment": "1.1.0",
5858
"vite-plugin-rewrite-all": "0.1.2",

0 commit comments

Comments
 (0)