Skip to content

Commit 6acdc48

Browse files
committed
add login redirect if no account is found
1 parent 79f1fe7 commit 6acdc48

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/modules/authentication/AuthenticationWrapper.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,14 @@ describe('AuthenticationWrapper should', () => {
150150
const token = await authenticationWrapper.getToken();
151151
expect(token.account!.homeAccountId).toBe('homeAccountId');
152152
});
153+
154+
it('redirects to login if no account is found in cache or memory', async () => {
155+
const { msalApplication } = require('./msal-app.ts');
156+
msalApplication.getAllAccounts.mockReturnValueOnce([]);
157+
158+
msalApplication.loginRedirect = jest.fn(() => Promise.resolve());
159+
160+
await expect(authenticationWrapper.getToken()).rejects.toThrow('Login redirect initiated');
161+
expect(msalApplication.loginRedirect).toHaveBeenCalled();
162+
});
153163
})

src/modules/authentication/AuthenticationWrapper.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ export class AuthenticationWrapper implements IAuthenticationWrapper {
169169
throw new Error(`Silent token acquisition failed for cached account: ${error}`);
170170
}
171171
} else {
172-
throw new Error('No active or cached account found. User login required.');
172+
await msalApplication.loginRedirect({
173+
scopes: defaultScopes,
174+
redirectUri: getCurrentUri()
175+
});
176+
throw new Error('Login redirect initiated');
173177
}
174178
}
175179

0 commit comments

Comments
 (0)