Skip to content

Commit 1a47754

Browse files
authored
fix: clear traits when identifying over previous identity (#229)
* Clear traits on Initialise over old identity * Fix tests * bump version
1 parent b1aa273 commit 1a47754

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

flagsmith-core.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ const Flagsmith = class {
506506
}
507507

508508
identify(userId: string, traits?: ITraits) {
509+
if(this.identity && this.identity !== userId) {
510+
// clear out old traits when switching identity
511+
this.withTraits = {}
512+
}
509513
this.identity = userId;
510514
this.log("Identify: " + this.identity)
511515

lib/flagsmith-es/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith-es",
3-
"version": "4.0.1",
3+
"version": "4.0.2",
44
"description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.",
55
"main": "./index.js",
66
"type": "module",

lib/flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "4.0.1",
3+
"version": "4.0.2",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

lib/react-native-flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-flagsmith",
3-
"version": "4.0.1",
3+
"version": "4.0.2",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

test/init.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,19 @@ describe('Flagsmith.init', () => {
7070
const {flagsmith,initConfig} = getFlagsmith({onChange, environmentID:"bad"})
7171
await expect(flagsmith.init(initConfig)).rejects.toThrow(Error);
7272
});
73+
test('identifying with new identity should not carry over previous traits for different identity', async () => {
74+
const onChange = jest.fn()
75+
const now = Date.now();
76+
const identityA = `test_identity_a_${now}`
77+
const identityB = `test_identity_b_${now}`
78+
const {flagsmith,initConfig} = getFlagsmith({onChange, identity:identityA, traits: {a:`example`}})
79+
await flagsmith.init(initConfig);
80+
expect(flagsmith.getTrait("a")).toEqual(`example`)
81+
await flagsmith.identify(identityB)
82+
expect(flagsmith.getTrait("a")).toEqual(undefined)
83+
await flagsmith.identify(identityA)
84+
expect(flagsmith.getTrait("a")).toEqual(`example`)
85+
await flagsmith.identify(identityB)
86+
expect(flagsmith.getTrait("a")).toEqual(undefined)
87+
});
7388
});

0 commit comments

Comments
 (0)