Skip to content

Commit

Permalink
Merge pull request #737 from google/update/724-disconnect-splash
Browse files Browse the repository at this point in the history
Update setup screen and disconnect handling
  • Loading branch information
felixarntz authored Oct 29, 2019
2 parents 58658e9 + 952c115 commit 050d7cf
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 60 deletions.
39 changes: 30 additions & 9 deletions assets/js/components/setup/setup-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ import Optin from 'GoogleComponents/optin';
import { sendAnalyticsTrackingEvent } from 'GoogleUtil';
import { getSiteKitAdminURL } from 'SiteKitCore/util';

const { __ } = wp.i18n;
const { __, sprintf } = wp.i18n;
const { Component, Fragment } = wp.element;
const { getQueryArg } = wp.url;
const { delay } = lodash;

class SetupUsingProxy extends Component {
constructor( props ) {
super( props );

const { proxySetupURL } = googlesitekit.admin;
const { proxySetupURL, siteURL } = googlesitekit.admin;
const { isSiteKitConnected } = googlesitekit.setup;
const { canSetup } = googlesitekit.permissions;

Expand All @@ -43,6 +44,8 @@ class SetupUsingProxy extends Component {
isSiteKitConnected,
completeSetup: false,
proxySetupURL,
context: getQueryArg( location.href, 'googlesitekit_context' ),
siteHostname: ( new URL( siteURL ) ).hostname,
};
}

Expand All @@ -69,7 +72,8 @@ class SetupUsingProxy extends Component {
}, 500, 'later' );
}

const { proxySetupURL } = this.state;
const { context, proxySetupURL, siteHostname } = this.state;
const isRevoked = 'revoked' === context;

return (
<Fragment>
Expand All @@ -90,12 +94,29 @@ class SetupUsingProxy extends Component {
mdc-layout-grid__cell
mdc-layout-grid__cell--span-12
">
<h1 className="googlesitekit-setup__title">
{ __( 'The Site Kit plugin is active but requires setup', 'google-site-kit' ) }
</h1>
<p className="googlesitekit-setup__description">
{ __( 'Site Kit Service will guide you through 3 simple setup steps.', 'google-site-kit' ) }
</p>
{ isRevoked ? (
<Fragment>
<h1 className="googlesitekit-setup__title">
{ sprintf(
/* translators: %s is the site's hostname. (e.g. example.com) */
__( 'You revoked access to Site Kit for %s', 'google-site-kit' ),
siteHostname
) }
</h1>
<p className="googlesitekit-setup__description">
{ __( 'Site Kit will no longer have access to your account. If you’d like to reconnect Site Kit, click "Start Setup" below to generate new credentials.', 'google-site-kit' ) }
</p>
</Fragment>
) : (
<Fragment>
<h1 className="googlesitekit-setup__title">
{ __( 'The Site Kit plugin is active but requires setup', 'google-site-kit' ) }
</h1>
<p className="googlesitekit-setup__description">
{ __( 'Site Kit Service will guide you through 3 simple setup steps.', 'google-site-kit' ) }
</p>
</Fragment>
) }
<Button
href={ proxySetupURL }
onClick={ () => {
Expand Down
18 changes: 7 additions & 11 deletions assets/js/components/user-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ import Dialog from 'GoogleComponents/dialog';
import Button from 'GoogleComponents/button';
import Menu from 'GoogleComponents/menu';
import { clearAppLocalStorage } from 'GoogleUtil';
import data, { TYPE_CORE } from 'GoogleComponents/data';
import { getSiteKitAdminURL } from 'SiteKitCore/util';

const { Component, Fragment, createRef } = wp.element;
const { __ } = wp.i18n;
const { addQueryArgs } = wp.url;

class UserMenu extends Component {
constructor( props ) {
Expand Down Expand Up @@ -120,9 +119,6 @@ class UserMenu extends Component {

// Log the user out if they confirm the dialog.
async handleUnlinkConfirm() {
// Disconnect the user.
await data.set( TYPE_CORE, 'user', 'disconnect' );

// Close the modal.
this.setState( {
dialogActive: false,
Expand All @@ -131,13 +127,13 @@ class UserMenu extends Component {
// Clear caches.
clearAppLocalStorage();

// Return to the Site Kit Dashboard.
const { adminRoot } = googlesitekit.admin;

document.location = addQueryArgs( adminRoot.replace( 'admin.php', '' ),
// Navigate back to the splash screen to reconnect.
document.location = getSiteKitAdminURL(
'googlesitekit-splash',
{
notification: 'googlesitekit_user_disconnected',
} );
googlesitekit_context: 'revoked',
}
);
}

render() {
Expand Down
12 changes: 12 additions & 0 deletions includes/Core/Admin/Screens.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Google\Site_Kit\Core\Admin;

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Authentication\Authentication;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\Assets\Assets;

Expand Down Expand Up @@ -307,6 +308,17 @@ private function get_screens() {

// This callback will redirect to the dashboard on successful authentication.
'initialize_callback' => function( Context $context ) {
$splash_context = filter_input( INPUT_GET, 'googlesitekit_context' );
$authentication = new Authentication( $context );

// If the user is authenticated, redirect them to the disconnect URL and then send them back here.
if ( empty( $_GET['googlesitekit_reset_session'] ) && 'revoked' === $splash_context && $authentication->is_authenticated() ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
$authentication->disconnect();

wp_safe_redirect( add_query_arg( array( 'googlesitekit_reset_session' => 1 ) ) );
exit;
}

$notification = filter_input( INPUT_GET, 'notification' );
$error = filter_input( INPUT_GET, 'error' );

Expand Down
36 changes: 1 addition & 35 deletions includes/Core/Authentication/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ public function get_oauth_client() {
* @since 1.0.0
*/
public function disconnect() {
$auth_client = $this->get_oauth_client();

$auth_client->revoke_token();
$this->get_oauth_client()->revoke_token();

$this->user_options->delete( Clients\OAuth_Client::OPTION_ACCESS_TOKEN );
$this->user_options->delete( Clients\OAuth_Client::OPTION_ACCESS_TOKEN_EXPIRES_IN );
Expand Down Expand Up @@ -607,42 +605,10 @@ private function authentication_admin_notices( $notices ) {

$notices[] = $this->get_reauthentication_needed_notice();
$notices[] = $this->get_authentication_oauth_error_notice();
$notices[] = $this->get_disconnected_user_notice();

return $notices;
}

/**
* Gets disconnected user notice.
*
* @since 1.0.0
*
* @return Notice Notice object.
*/
private function get_disconnected_user_notice() {
return new Notice(
'googlesitekit_user_disconnected',
array(
'content' => function() {
ob_start();
?>
<p>
<?php esc_html_e( 'Successfully disconnected from Site Kit by Google.', 'google-site-kit' ); ?>
</p>
<?php
return ob_get_clean();
},
'type' => Notice::TYPE_SUCCESS,
'active_callback' => function() {
if ( isset( $_GET['notification'] ) && 'googlesitekit_user_disconnected' === $_GET['notification'] ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
return true;
}
return false;
},
)
);
}

/**
* Gets re-authentication notice.
*
Expand Down
9 changes: 5 additions & 4 deletions tests/e2e/specs/auth-flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function stubGoogleSignIn( request ) {
}
}

const signOut = async () => {
const disconnectFromSiteKit = async () => {
await page.waitForSelector( 'button[aria-controls="user-menu"]' );
await page.click( 'button[aria-controls="user-menu"]' );

Expand Down Expand Up @@ -83,11 +83,12 @@ describe( 'Site Kit set up flow for the first time', () => {
await setSearchConsoleProperty();
await visitAdminPage( 'admin.php', 'page=googlesitekit-dashboard' );

await signOut();
await disconnectFromSiteKit();

// Ensure the user is on step one of the setup wizard.
await expect( page ).toMatchElement(
'.notice-success',
{ text: /Successfully disconnected from Site Kit by Google./i }
'.googlesitekit-wizard-progress-step__number-text--inprogress',
{ text: '1' }
);
} );
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ function ( $notice ) {
);
$this->assertEqualSets(
array(
'googlesitekit_user_disconnected',
'needs_reauthentication',
'oauth_error',
),
Expand Down

0 comments on commit 050d7cf

Please sign in to comment.