Skip to content

Commit 5c5bb8d

Browse files
authored
[SDK-2330] New tokens should be applied to existing session (#307)
1 parent 7d7d4a9 commit 5c5bb8d

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/session/get-access-token.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextApiRequest, NextApiResponse } from 'next';
33
import { ClientFactory } from '../auth0-session';
44
import { AccessTokenError } from '../utils/errors';
55
import { intersect, match } from '../utils/array';
6-
import { SessionCache, fromTokenSet, fromJson } from '../session';
6+
import { SessionCache, fromTokenSet } from '../session';
77
import { NextConfig } from '../config';
88

99
/**
@@ -114,16 +114,11 @@ export default function accessTokenFactory(
114114

115115
// Update the session.
116116
const newSession = fromTokenSet(tokenSet, config);
117-
sessionCache.set(
118-
req,
119-
res,
120-
fromJson({
121-
...session,
122-
...newSession,
123-
refreshToken: newSession.refreshToken || session.refreshToken,
124-
user: { ...session.user, ...newSession.user }
125-
})
126-
);
117+
Object.assign(session, {
118+
...newSession,
119+
refreshToken: newSession.refreshToken || session.refreshToken,
120+
user: { ...session.user, ...newSession.user }
121+
});
127122

128123
// Return the new access token.
129124
return {

tests/fixtures/setup.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export type SetupOptions = {
2929
getAccessTokenOptions?: AccessTokenRequest;
3030
discoveryOptions?: object;
3131
userInfoPayload?: object;
32+
userInfoToken?: string;
3233
};
3334

3435
export const setup = async (
@@ -42,13 +43,14 @@ export const setup = async (
4243
withPageAuthRequiredOptions,
4344
getAccessTokenOptions,
4445
discoveryOptions,
45-
userInfoPayload = {}
46+
userInfoPayload = {},
47+
userInfoToken = 'eyJz93a...k4laUWw'
4648
}: SetupOptions = {}
4749
): Promise<string> => {
4850
discovery(config, discoveryOptions);
4951
jwksEndpoint(config, jwks);
5052
codeExchange(config, makeIdToken({ iss: 'https://acme.auth0.local/', ...idTokenClaims }));
51-
userInfo(config, 'eyJz93a...k4laUWw', userInfoPayload);
53+
userInfo(config, userInfoToken, userInfoPayload);
5254
const {
5355
handleAuth,
5456
handleCallback,

tests/handlers/profile.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import nock from 'nock';
2-
import { withoutApi } from '../fixtures/default-settings';
3-
import { userInfo } from '../fixtures/oidc-nocks';
2+
import { withApi, withoutApi } from '../fixtures/default-settings';
3+
import { refreshTokenRotationExchange, userInfo } from '../fixtures/oidc-nocks';
44
import { get } from '../auth0-session/fixtures/helpers';
55
import { setup, teardown, login } from '../fixtures/setup';
66
import { Session, AfterCallback } from '../../src';
@@ -91,6 +91,28 @@ describe('profile handler', () => {
9191
);
9292
});
9393

94+
test('should refetch the user and preserve new tokens', async () => {
95+
const afterCallback: AfterCallback = (_req, _res, session: Session): Session => {
96+
session.accessTokenExpiresAt = -60;
97+
return session;
98+
};
99+
const baseUrl = await setup(withApi, {
100+
profileOptions: { refetch: true },
101+
userInfoPayload: { foo: 'bar' },
102+
callbackOptions: {
103+
afterCallback
104+
},
105+
userInfoToken: 'new-access-token'
106+
});
107+
refreshTokenRotationExchange(withApi, 'GEbRxBN...edjnXbL', {}, 'new-access-token', 'new-refresh-token');
108+
const cookieJar = await login(baseUrl);
109+
const profile = await get(baseUrl, '/api/auth/me', { cookieJar });
110+
expect(profile).toMatchObject({ foo: 'bar' });
111+
const session = await get(baseUrl, '/api/session', { cookieJar });
112+
expect(session.accessToken).toEqual('new-access-token');
113+
expect(session.refreshToken).toEqual('new-refresh-token');
114+
});
115+
94116
test('should update the session in the afterRefetch hook', async () => {
95117
const baseUrl = await setup(withoutApi, {
96118
profileOptions: {

0 commit comments

Comments
 (0)