Skip to content

Commit 820ff2d

Browse files
🔄 synced local 'src/' with remote 'src/'
1 parent aa25522 commit 820ff2d

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

‎src/index.test.ts‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,104 @@ describe('W3SSdk > Apple OAuth', () => {
312312
expect(handleLoginFailureSpy).toHaveBeenCalledTimes(1)
313313
})
314314
})
315+
316+
describe('W3SSdk > Google OAuth > selectAccountPrompt is true', () => {
317+
let sdk: W3SSdk
318+
const configs: Configs = {
319+
appSettings: {
320+
appId: 'test-app-id',
321+
},
322+
loginConfigs: {
323+
deviceToken: 'device-token',
324+
deviceEncryptionKey: 'device-encryption-key',
325+
google: {
326+
clientId: 'test-client-id',
327+
redirectUri: 'test-redirect-uri',
328+
selectAccountPrompt: true,
329+
},
330+
},
331+
}
332+
333+
beforeEach(() => {
334+
;(W3SSdk as any).instance = null
335+
jest.resetAllMocks()
336+
337+
const onLoginComplete = jest.fn()
338+
sdk = new W3SSdk(configs, onLoginComplete)
339+
})
340+
341+
it('should generate the right parameters', async () => {
342+
const performGoogleLoginSpy = jest.spyOn(sdk as any, 'performGoogleLogin')
343+
344+
const generateOauthUrlWithParamsSpy = jest.spyOn(
345+
sdk as any,
346+
'generateOauthUrlWithParams',
347+
)
348+
349+
await sdk.performLogin(SocialLoginProvider.GOOGLE)
350+
351+
expect(performGoogleLoginSpy).toHaveBeenCalled()
352+
expect(generateOauthUrlWithParamsSpy).toHaveBeenLastCalledWith(
353+
'Google',
354+
'test-client-id',
355+
'test-redirect-uri',
356+
true,
357+
)
358+
})
359+
360+
it('should update the global this location href correctly', async () => {
361+
await sdk.performLogin(SocialLoginProvider.GOOGLE)
362+
363+
expect(window.location.href).toContain('prompt=select_account')
364+
})
365+
})
366+
367+
describe('W3SSdk > Google OAuth > selectAccountPrompt is false or empty', () => {
368+
let sdk: W3SSdk
369+
const configs: Configs = {
370+
appSettings: {
371+
appId: 'test-app-id',
372+
},
373+
loginConfigs: {
374+
deviceToken: 'device-token',
375+
deviceEncryptionKey: 'device-encryption-key',
376+
google: {
377+
clientId: 'test-client-id',
378+
redirectUri: 'test-redirect-uri',
379+
},
380+
},
381+
}
382+
383+
beforeEach(() => {
384+
;(W3SSdk as any).instance = null
385+
jest.resetAllMocks()
386+
387+
const onLoginComplete = jest.fn()
388+
sdk = new W3SSdk(configs, onLoginComplete)
389+
})
390+
391+
it('should generate the right parameters', async () => {
392+
const performGoogleLoginSpy = jest.spyOn(sdk as any, 'performGoogleLogin')
393+
394+
const generateOauthUrlWithParamsSpy = jest.spyOn(
395+
sdk as any,
396+
'generateOauthUrlWithParams',
397+
)
398+
399+
await sdk.performLogin(SocialLoginProvider.GOOGLE)
400+
401+
expect(performGoogleLoginSpy).toHaveBeenCalled()
402+
expect(generateOauthUrlWithParamsSpy).toHaveBeenLastCalledWith(
403+
'Google',
404+
'test-client-id',
405+
'test-redirect-uri',
406+
undefined,
407+
)
408+
})
409+
410+
it('should update the global this location href correctly', async () => {
411+
await sdk.performLogin(SocialLoginProvider.GOOGLE)
412+
413+
expect(window.location.href).toContain('prompt=none')
414+
})
415+
})

‎src/index.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ export class W3SSdk {
460460
return
461461
}
462462

463-
const { clientId, redirectUri } = this.configs.loginConfigs.google
463+
const { clientId, redirectUri, selectAccountPrompt } =
464+
this.configs.loginConfigs.google
464465

465466
const {
466467
url = '',
@@ -470,6 +471,7 @@ export class W3SSdk {
470471
SocialLoginProvider.GOOGLE,
471472
clientId,
472473
redirectUri,
474+
selectAccountPrompt,
473475
) || {}
474476

475477
this.saveOAuthInfo(SocialLoginProvider.GOOGLE, state, nonce)
@@ -481,12 +483,14 @@ export class W3SSdk {
481483
* @param provider - Social login provider.
482484
* @param id - Client ID or Application ID.
483485
* @param redirectUri - Redirect URI.
486+
* @param selectAccountPrompt - Indicates whether the user should select the account. Default is false.
484487
* @returns OAuth URL with the necessary parameters.
485488
*/
486489
private generateOauthUrlWithParams(
487490
provider: SocialLoginProvider,
488491
id: string,
489492
redirectUri: string,
493+
selectAccountPrompt: boolean = false,
490494
):
491495
| {
492496
url: string
@@ -506,7 +510,9 @@ export class W3SSdk {
506510
return {
507511
url: `https://accounts.google.com/o/oauth2/v2/auth?client_id=${id}&redirect_uri=${encodeURIComponent(
508512
redirectUri,
509-
)}&scope=${scope}&state=${state}&response_type=${responseType}&nonce=${nonce}`,
513+
)}&scope=${scope}&state=${state}&response_type=${responseType}&nonce=${nonce}&prompt=${
514+
selectAccountPrompt ? 'select_account' : 'none'
515+
}`,
510516
state,
511517
nonce,
512518
}

‎src/types.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ export interface LoginConfigs {
194194
* Google redirect URI.
195195
*/
196196
redirectUri: string
197+
/**
198+
* Google prompt for account selection.
199+
*/
200+
selectAccountPrompt?: boolean
197201
}
198202
| undefined
199203
facebook?:

0 commit comments

Comments
 (0)