generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Chris Peyer <[email protected]> Co-authored-by: Honwai Wong <[email protected]>
- Loading branch information
1 parent
43599e7
commit f56787b
Showing
7 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const GOOGLE_SCRIPT = 'https://accounts.google.com/gsi/client'; | ||
const GOOGLE_ID = '530526366930-l874a90ipfkn26naa71r010u8epp39jt.apps.googleusercontent.com'; | ||
const PLACEHOLDER = 'feds-googleLogin'; | ||
const WRAPPER = 'feds-profile'; | ||
|
||
const onToken = async (getMetadata, data) => { | ||
let destination; | ||
try { | ||
destination = new URL(getMetadata('google-login-redirect'))?.href; | ||
} catch { | ||
// Do nothing | ||
} | ||
|
||
await window.adobeIMS.socialHeadlessSignIn({ | ||
provider_id: 'google', | ||
idp_token: data?.credential, | ||
client_id: window.adobeid?.client_id, | ||
scope: window.adobeid?.scope, | ||
}).then(() => { | ||
if (window.DISABLE_PAGE_RELOAD === true) return; | ||
// Existing account | ||
if (destination) { | ||
window.location.assign(destination); | ||
} else { | ||
window.location.reload(); | ||
} | ||
}).catch(() => { | ||
// New account | ||
window.adobeIMS.signInWithSocialProvider('google', { redirect_uri: destination || window.location.href }); | ||
}); | ||
}; | ||
|
||
export default async function initGoogleLogin(loadIms, getMetadata, loadScript) { | ||
try { | ||
await loadIms(); | ||
} catch { | ||
return; | ||
} | ||
if (window.adobeIMS?.isSignedInUser()) return; | ||
|
||
await loadScript(GOOGLE_SCRIPT); | ||
const placeholder = document.createElement('div'); | ||
placeholder.id = PLACEHOLDER; | ||
document.querySelector(`.${WRAPPER}`)?.append(placeholder); | ||
|
||
window.google?.accounts?.id?.initialize({ | ||
client_id: GOOGLE_ID, | ||
callback: (data) => onToken(getMetadata, data), | ||
prompt_parent_id: PLACEHOLDER, | ||
cancel_on_tap_outside: false, | ||
}); | ||
window.google?.accounts?.id?.prompt(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import sinon from 'sinon'; | ||
import { expect } from '@esm-bundle/chai'; | ||
import { readFile } from '@web/test-runner-commands'; | ||
import initGoogleLogin from '../../../libs/features/google-login.js'; | ||
|
||
describe('Google Login', () => { | ||
let initializeSpy; | ||
let promptSpy; | ||
beforeEach(async () => { | ||
document.body.innerHTML = await readFile({ path: './mocks/google-login.html' }); | ||
window.google = window.google || { | ||
accounts: { | ||
id: { | ||
initialize: () => {}, | ||
prompt: () => {}, | ||
}, | ||
}, | ||
}; | ||
initializeSpy = sinon.spy(window.google.accounts.id, 'initialize'); | ||
promptSpy = sinon.spy(window.google.accounts.id, 'prompt'); | ||
window.adobeid = { | ||
client_id: 'milo', | ||
scope: 'gnav', | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.innerHTML = ''; | ||
initializeSpy.restore(); | ||
promptSpy.restore(); | ||
delete window.adobeid; | ||
delete window.google; | ||
}); | ||
|
||
it('should create a placeholder to inject DOM markup', async () => { | ||
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub()); | ||
expect(document.getElementById('feds-googleLogin')).to.exist; | ||
}); | ||
|
||
it('should initialize and render the login element', async () => { | ||
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub()); | ||
expect(initializeSpy.called).to.be.true; | ||
expect(promptSpy.called).to.be.true; | ||
expect(initializeSpy.getCall(0).args[0].prompt_parent_id).to.equal('feds-googleLogin'); | ||
}); | ||
|
||
it('should exchange tokens with adobeIMS after login', async () => { | ||
window.adobeIMS = window.adobeIMS || { | ||
socialHeadlessSignIn: () => {}, | ||
isSignedInUser: () => false, | ||
signInWithSocialProvider: () => {}, | ||
}; | ||
|
||
// No account | ||
const socialHeadlessSignInStub = sinon.stub(window.adobeIMS, 'socialHeadlessSignIn') | ||
.returns(new Promise((resolve, reject) => { reject(); })); | ||
const signInWithSocialProviderSpy = sinon.spy(window.adobeIMS, 'signInWithSocialProvider'); | ||
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub()); | ||
let onToken = initializeSpy.getCall(0).args[0].callback; | ||
await onToken(sinon.stub(), sinon.stub()); | ||
expect(signInWithSocialProviderSpy.called).to.be.true; | ||
|
||
// Existing account | ||
socialHeadlessSignInStub.returns(new Promise((resolve) => { resolve(); })); | ||
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub()); | ||
onToken = initializeSpy.getCall(0).args[0].callback; | ||
window.DISABLE_PAGE_RELOAD = true; | ||
signInWithSocialProviderSpy.resetHistory(); | ||
await onToken(sinon.stub(), sinon.stub()); | ||
expect(signInWithSocialProviderSpy.called).not.to.be.true; | ||
expect(document.getElementById('feds-googleLogin')).to.exist; | ||
signInWithSocialProviderSpy.restore(); | ||
socialHeadlessSignInStub.restore(); | ||
delete window.DISABLE_PAGE_RELOAD; | ||
}); | ||
|
||
it('should not initialize if IMS is not ready or user is already logged-in', async () => { | ||
window.adobeIMS = window.adobeIMS || { isSignedInUser: () => {} }; | ||
const loggedInStub = sinon.stub(window.adobeIMS, 'isSignedInUser').returns(() => true); | ||
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub()); | ||
expect(document.getElementById('feds-googleLogin')).not.to.exist; | ||
await initGoogleLogin(Promise.reject, sinon.stub(), sinon.stub()); | ||
expect(document.getElementById('feds-googleLogin')).not.to.exist; | ||
loggedInStub.restore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div class="feds-profile"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters