Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Google OAuth2 authentication flow #935

Merged
merged 8 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ If you have a patch or have stumbled upon an issue with the Newspack plugin/them
Some features require environment variables to be set (e.g. in `wp-config.php`):

```php
// support
define('NEWSPACK_SUPPORT_API_URL', 'https://super-tech-support.zendesk.com/api/v2');
define('NEWSPACK_SUPPORT_EMAIL', '[email protected]');
define('NEWSPACK_WPCOM_CLIENT_ID', '12345');
// optional
define('NEWSPACK_SUPPORT_IS_PRE_LAUNCH', true);
// Support
define( 'NEWSPACK_SUPPORT_API_URL', 'https://super-tech-support.zendesk.com/api/v2' );
define( 'NEWSPACK_SUPPORT_EMAIL', '[email protected]' );
define( 'NEWSPACK_WPCOM_CLIENT_ID', '12345' );

// OAuth2
define( 'NEWSPACK_GOOGLE_OAUTH_CLIENT_ID', 'abc' );
define( 'NEWSPACK_GOOGLE_OAUTH_CLIENT_SECRET', 'abc' );

// Optional
define( 'NEWSPACK_SUPPORT_IS_PRE_LAUNCH', true );
```

## News Consumer Insights integration
Expand Down
9 changes: 6 additions & 3 deletions assets/components/src/global-notices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ const GlobalNotices = () => {
if ( ! notice ) {
return null;
}
return notice
.split( ',' )
.map( ( text, i ) => <Notice isSuccess noticeText={ text } key={ i } /> );
return notice.split( ',' ).map( ( text, i ) => {
if ( text.indexOf( '_error_' ) === 0 ) {
return <Notice isError noticeText={ text.replace( '_error_', '' ) } key={ i } />;
}
return <Notice isSuccess noticeText={ text } key={ i } />;
} );
};

export default GlobalNotices;
153 changes: 122 additions & 31 deletions assets/wizards/dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,139 @@

import '../../shared/js/public-path';

/**
* External dependencies.
*/
import qs from 'qs';

/**
* WordPress dependencies.
*/
import { Component, Fragment, render } from '@wordpress/element';
import { useEffect, useState, Fragment, render } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';
import { Icon, plugins } from '@wordpress/icons';

/**
* Internal dependencies.
*/
import { GlobalNotices, Footer, Grid, NewspackLogo } from '../../components/src';
import {
GlobalNotices,
Button,
Card,
Waiting,
Footer,
Grid,
NewspackLogo,
} from '../../components/src';
import DashboardCard from './views/dashboardCard';
import './style.scss';

/**
* Newspack Dashboard.
* External dependencies.
*/
class Dashboard extends Component {
/**
* Render.
*/
render() {
const { items } = this.props;

return (
<Fragment>
<div className="newspack-wizard__header">
<div className="newspack-wizard__header__inner">
<NewspackLogo centered height={ 72 } />
</div>
</div>
<div className="mw6 mr-auto ml-auto">
<GlobalNotices />
</div>
<div className="newspack-wizard newspack-wizard__content">
<Grid columns={ 3 } gutter={ 32 }>
{ items.map( card => (
<DashboardCard { ...card } key={ card.slug } />
) ) }
</Grid>
import classnames from 'classnames';

const Dashboard = ( { items } ) => {
const params = qs.parse( window.location.search );
const authCode = params.code;
const [ authState, setAuthState ] = useState( {} );

const userBasicInfo = authState.user_basic_info;
const canUseOauth = authState.can_google_auth;

const displayAuth = canUseOauth && ! authCode;

useEffect( () => {
apiFetch( { path: '/newspack/v1/oauth/google' } ).then( setAuthState );
}, [] );

useEffect( () => {
if ( canUseOauth && authCode ) {
apiFetch( {
path: '/newspack/v1/oauth/google',
method: 'POST',
data: {
auth_code: authCode,
state: params.state,
},
} )
.then( () => {
window.location =
'/wp-admin/admin.php?' +
qs.stringify( {
page: 'newspack',
'newspack-notice': __( 'Successfully authenticated with Google.', 'newspack' ),
} );
} )
.catch( e => {
window.location =
'/wp-admin/admin.php?' +
qs.stringify( {
page: 'newspack',
'newspack-notice':
'_error_' +
( e.message ||
__( 'Something went wrong during authentication with Google.', 'newspack' ) ),
} );
} );
}
}, [ canUseOauth ] );

const goToAuthPage = () => {
apiFetch( {
path: '/newspack/v1/oauth/google/get-url',
} ).then( url => ( window.location = url ) );
};

return (
<Fragment>
<GlobalNotices />
<div className="newspack-wizard__header">
<div className="newspack-wizard__header__inner">
<NewspackLogo centered height={ 72 } />
</div>
<Footer />
</Fragment>
);
}
}
</div>
<div className="newspack-wizard newspack-wizard__content">
<Grid columns={ 3 } gutter={ 32 }>
{ items.map( card => (
<DashboardCard { ...card } key={ card.slug } />
) ) }
{ authCode && (
<div className="flex justify-around items-center">
<Waiting />
</div>
) }
{ displayAuth ? (
<Card className={ classnames( 'newspack-dashboard-card', 'google-oauth2' ) }>
{ userBasicInfo ? (
<div className="newspack-dashboard-card__contents">
<Icon icon={ plugins } />
<div className="newspack-dashboard-card__header">
<h2>{ __( 'Google OAuth2' ) }</h2>
<p>
{ __( 'Authorized Google as', 'newspack' ) }{' '}
<strong>{ userBasicInfo.email }</strong>
</p>
</div>
</div>
) : (
<Button onClick={ goToAuthPage }>
<div className="newspack-dashboard-card__contents">
<Icon icon={ plugins } />
<div className="newspack-dashboard-card__header">
<h2>{ __( 'Google OAuth2' ) }</h2>
<p>{ __( 'Authorize Newspack with Google', 'newspack' ) }</p>
</div>
</div>
</Button>
) }
</Card>
) : null }
</Grid>
</div>
<Footer />
</Fragment>
);
};
render( <Dashboard items={ newspack_dashboard } />, document.getElementById( 'newspack' ) );
11 changes: 10 additions & 1 deletion assets/wizards/dashboard/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
}
}

a {
a,
body[class*='admin-color-'] & .newspack-button {
align-items: center;
border-radius: inherit;
color: inherit;
display: flex;
font-weight: inherit;
height: 100%;
justify-content: space-between;
padding: 0;
text-align: inherit;
text-decoration: none;
transition: background-color 125ms ease-in-out, box-shadow 125ms ease-in-out;

Expand All @@ -31,6 +35,7 @@
&:hover {
background-color: $light-gray-100;
box-shadow: 0 4px 8px rgba( black, 0.08 );
color: $primary-600;

h2 {
color: inherit;
Expand Down Expand Up @@ -81,3 +86,7 @@
}
}
}

.newspack-notice {
margin: 0;
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"issues": "https://github.com/Automattic/newspack-plugin/issues"
},
"require" : {
"composer/installers": "~1.6",
"joshtronic/php-loremipsum": "^1.0"
"composer/installers": "~1.6",
"joshtronic/php-loremipsum": "^1.0",
"google/auth": "^1.15"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.6",
Expand Down
Loading