fix: defer mini program phone auth until login

This commit is contained in:
kdletters
2026-05-26 19:59:14 +08:00
parent b388b124da
commit 296a7fced9
10 changed files with 520 additions and 19 deletions

View File

@@ -20,8 +20,8 @@ import { clearStoredAccessToken, getStoredAccessToken } from './apiClient';
import {
authEntry,
bindWechatPhone,
changePhoneNumber,
changePassword,
changePhoneNumber,
consumeAuthCallbackResult,
getAuthAuditLogs,
getAuthLoginOptions,
@@ -34,6 +34,7 @@ import {
loginWithPhoneCode,
logoutAllAuthSessions,
redeemRegistrationInviteCode,
requestWechatMiniProgramPhoneLogin,
revokeAuthSession,
revokeAuthSessions,
sendPhoneLoginCode,
@@ -408,6 +409,84 @@ describe('authService', () => {
);
});
it('requests mini program phone login by opening the native auth page', async () => {
const navigateTo = vi.fn((options: { url: string; success?: () => void }) => {
options.success?.();
});
vi.stubGlobal(
'window',
createWindowMock({
location: {
pathname: '/',
hash: '',
search: '?clientRuntime=wechat_mini_program',
assign: vi.fn(),
},
wx: {
miniProgram: {
navigateTo,
},
},
}),
);
const result = await requestWechatMiniProgramPhoneLogin();
expect(result).toBe(true);
expect(navigateTo).toHaveBeenCalledWith({
url: '/pages/web-view/index?authAction=login&returnTo=previous',
success: expect.any(Function),
fail: expect.any(Function),
});
});
it('waits for an existing WeChat JS SDK script before opening the native auth page', async () => {
const navigateTo = vi.fn((options: { url: string; success?: () => void }) => {
options.success?.();
});
const scriptListeners = new Map<string, EventListener>();
const existingScript = {
addEventListener: vi.fn(
(type: string, listener: EventListener) => {
scriptListeners.set(type, listener);
},
),
};
vi.stubGlobal(
'window',
createWindowMock({
location: {
pathname: '/',
hash: '',
search: '?clientRuntime=wechat_mini_program',
assign: vi.fn(),
},
}),
);
vi.stubGlobal('document', {
querySelector: vi.fn(() => existingScript),
head: {
appendChild: vi.fn(),
},
createElement: vi.fn(),
});
const request = requestWechatMiniProgramPhoneLogin();
window.wx = {
miniProgram: {
navigateTo,
},
};
scriptListeners.get('load')?.(new Event('load'));
await expect(request).resolves.toBe(true);
expect(navigateTo).toHaveBeenCalledWith({
url: '/pages/web-view/index?authAction=login&returnTo=previous',
success: expect.any(Function),
fail: expect.any(Function),
});
});
it('loads available login methods for the unauthenticated login screen', async () => {
apiClientMocks.requestJson.mockResolvedValue({
availableLoginMethods: ['phone', 'wechat'],