below
const signInButton = document.createElement('div');
google.accounts.id.renderButton(signInButton, {type: 'standard'});
const appComponent = document.querySelector('chromedash-app');
if (appComponent) {
appComponent.insertAdjacentElement('afterbegin', signInButton); // for SPA
} else {
this.insertAdjacentElement('afterbegin', signInButton); // for MPA
}
}
initializeTestingSignIn() {
// Create DEV_MODE login button for testing
const signInTestingButton = document.createElement('button');
signInTestingButton.innerText = 'Sign in as example@chromium.org';
signInTestingButton.setAttribute('type', 'button');
signInTestingButton.setAttribute('data-testid', 'dev-mode-sign-in-button');
signInTestingButton.setAttribute(
'style',
'margin-right: 300px; z-index:1000; background: lightblue; border: 1px solid blue;'
);
signInTestingButton.addEventListener('click', () => {
// POST to '/dev/mock_login' to login as example@chromium.
fetch('/dev/mock_login', {method: 'POST'})
.then(response => {
if (!response.ok) {
throw new Error(`Sign in failed! Response: ${response}`);
}
})
.then(() => {
setTimeout(() => {
const url = window.location.href.split('?')[0];
window.location.href = url;
}, 1000);
})
.catch(error => {
console.error('Sign in failed.', error);
});
});
const signInButtonContainer = document.querySelector('chromedash-app');
if (signInButtonContainer) {
signInButtonContainer.insertAdjacentElement(
'afterbegin',
signInTestingButton
); // for SPA
} else {
this.insertAdjacentElement('afterbegin', signInTestingButton); // for MPA
}
}
handleCredentialResponse(credentialResponse) {
window.csClient
.signIn(credentialResponse)
.then(() => {
setTimeout(() => {
const url = window.location.href.split('?')[0];
window.location = url as string & Location;
}, 1000);
})
.catch(() => {
console.error('Sign in failed, so signing out to allow retry');
this.signOut();
});
}
handleSignOutClick(e) {
e.preventDefault();
this.signOut();
}
signOut() {
window.csClient.signOut().then(() => {
window.location.reload();
});
}
isCurrentPage(href) {
return this.currentPage.startsWith(href);
}
_fireEvent(eventName, detail) {
const event = new CustomEvent(eventName, {
bubbles: true,
composed: true,
detail,
});
this.dispatchEvent(event);
}
handleDrawer() {
this._fireEvent('drawer-clicked', {});
}
renderAccountMenu() {
return html`
${this.user
? html`
${this.user.can_create_feature && !this.isCurrentPage('/guide/new')
? html`
Create feature
`
: nothing}
`
: html` `}
`;
}
render() {
let accountMenu = html``;
if (!IS_MOBILE && !this.loading) {
accountMenu = html`
${this.renderAccountMenu()}
`;
}
return html`
`;
}
}