Skip to content

Commit b383022

Browse files
committed
fix(core): Take user from current scope when starting a session
1 parent 840a307 commit b383022

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
release: '0.1',
8+
initialScope: {
9+
user: {
10+
id: '1337',
11+
12+
username: 'user1337',
13+
},
14+
tags: {
15+
test: 'abc',
16+
},
17+
},
18+
debug: true,
19+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<a id='navigate' href="foo">Navigate</button>
8+
</body>
9+
</html>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { Route } from '@playwright/test';
2+
import { expect } from '@playwright/test';
3+
import type { SessionContext } from '@sentry/types';
4+
5+
import { sentryTest } from '../../../utils/fixtures';
6+
import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers';
7+
8+
sentryTest('should start a new session on pageload.', async ({ getLocalTestPath, page }) => {
9+
const url = await getLocalTestPath({ testDir: __dirname });
10+
const session = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);
11+
12+
expect(session).toBeDefined();
13+
expect(session.init).toBe(true);
14+
expect(session.errors).toBe(0);
15+
expect(session.status).toBe('ok');
16+
expect(session.did).toBe('dsdd');
17+
});
18+
19+
sentryTest('should start a new session with navigation.', async ({ getLocalTestPath, page, browserName }) => {
20+
// Navigations get CORS error on Firefox and WebKit as we're using `file://` protocol.
21+
if (browserName !== 'chromium') {
22+
sentryTest.skip();
23+
}
24+
25+
const url = await getLocalTestPath({ testDir: __dirname });
26+
await page.route('**/foo', (route: Route) => route.fulfill({ path: `${__dirname}/dist/index.html` }));
27+
28+
const initSession = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);
29+
30+
await page.click('#navigate');
31+
32+
const newSession = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);
33+
34+
expect(newSession).toBeDefined();
35+
expect(newSession.init).toBe(true);
36+
expect(newSession.errors).toBe(0);
37+
expect(newSession.status).toBe('ok');
38+
expect(newSession.sid).toBeDefined();
39+
expect(initSession.sid).not.toBe(newSession.sid);
40+
});

packages/core/src/exports.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ export function startSession(context?: SessionContext): Session {
383383
const session = makeSession({
384384
release,
385385
environment,
386-
user: isolationScope.getUser(),
386+
user: currentScope.getUser() || isolationScope.getUser(),
387387
...(userAgent && { userAgent }),
388388
...context,
389389
});
@@ -413,7 +413,7 @@ export function endSession(): void {
413413
const isolationScope = getIsolationScope();
414414
const currentScope = getCurrentScope();
415415

416-
const session = isolationScope.getSession();
416+
const session = currentScope.getSession() || isolationScope.getSession();
417417
if (session) {
418418
closeSession(session);
419419
}

0 commit comments

Comments
 (0)