Skip to content
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
22 changes: 14 additions & 8 deletions assets/blocks/reader-registration/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,27 @@ function domReady( callback ) {

form.endLoginFlow = ( message, status, data ) => {
let messageNode;
if ( message ) {
messageNode = document.createElement( 'div' );
messageNode.textContent = message;
}

if ( data?.existing_user ) {
successElement = document.querySelector( '.newspack-login__success' );
}

if ( message ) {
messageNode = document.createElement( 'p' );
messageNode.classList.add( 'has-text-align-center' );
messageNode.textContent = message;

const defaultMessage = successElement.querySelector( 'p' );
if ( defaultMessage && data?.sso ) {
defaultMessage.replaceWith( messageNode );
}
}

const isSuccess = status === 200;
container.classList.add( `newspack-registration--${ isSuccess ? 'success' : 'error' }` );
if ( isSuccess ) {
if ( messageNode ) {
successElement.classList.remove( 'newspack-registration--hidden' );
form.remove();
}
successElement.classList.remove( 'newspack-registration--hidden' );
form.remove();
if ( data?.email ) {
readerActivation.setReaderEmail( data.email );
// Set authenticated only if email is set, otherwise an error will be thrown.
Expand Down
45 changes: 29 additions & 16 deletions assets/reader-activation/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ const convertFormDataToObject = ( formData, includedFields = [] ) =>
}

allContainers.forEach( container => {
const form = container.querySelector( 'form' );
const emailInput = container.querySelector( 'input[name="email"]' );
const redirectInput = container.querySelector( 'input[name="redirect"]' );
const reader = readerActivation.getReader();
let redirectAfterLogin = redirectInput && !! redirectInput.value;

if ( emailInput ) {
emailInput.value = reader?.email || '';
}
Expand All @@ -105,7 +106,7 @@ const convertFormDataToObject = ( formData, includedFields = [] ) =>
/** If there's a pre-auth, signing in redirects to the reader account. */
if ( reader?.email && ! reader?.authenticated ) {
link.setAttribute( 'data-redirect', link.getAttribute( 'href' ) );
redirectAfterLogin = true;
redirectInput.value = link.getAttribute( 'href' );
} else {
link.removeAttribute( 'data-redirect' );
}
Expand All @@ -116,8 +117,13 @@ const convertFormDataToObject = ( formData, includedFields = [] ) =>
} catch {}
} );
}
if ( reader?.authenticated && ! redirectAfterLogin ) {
container.style.display = 'none';
if ( reader?.authenticated ) {
const messageContentElement = container.querySelector(
'.newspack-reader__auth-form__response__content'
);
if ( messageContentElement ) {
form.replaceWith( messageContentElement.parentNode );
}
}
} );
}
Expand Down Expand Up @@ -150,15 +156,19 @@ const convertFormDataToObject = ( formData, includedFields = [] ) =>
const passwordInput = container.querySelector( 'input[name="password"]' );
const actionInput = container.querySelector( 'input[name="action"]' );

if ( readerActivation.hasAuthLink() ) {
authLinkMessage.style.display = 'block';
} else {
authLinkMessage.style.display = 'none';
if ( authLinkMessage ) {
if ( readerActivation.hasAuthLink() ) {
authLinkMessage.style.display = 'flex';
} else {
authLinkMessage.style.display = 'none';
}
}

emailInput.value = reader?.email || '';
if ( emailInput ) {
emailInput.value = reader?.email || '';
}

if ( ev.target.getAttribute( 'data-redirect' ) ) {
if ( redirectInput && ev.target.getAttribute( 'data-redirect' ) ) {
redirectInput.value = ev.target.getAttribute( 'data-redirect' );
}

Expand All @@ -170,7 +180,7 @@ const convertFormDataToObject = ( formData, includedFields = [] ) =>
);
hideCurrentlyOpenOverlayPrompts();

if ( emailInput.value && 'pwd' === actionInput.value ) {
if ( passwordInput && emailInput?.value && 'pwd' === actionInput?.value ) {
passwordInput.focus();
} else {
emailInput.focus();
Expand Down Expand Up @@ -255,7 +265,7 @@ const convertFormDataToObject = ( formData, includedFields = [] ) =>
form.style.opacity = 0.5;
};

form.endLoginFlow = ( message, status, data, redirect ) => {
form.endLoginFlow = ( message = null, status = 500, data = null, redirect ) => {
if ( message ) {
const messageNode = document.createElement( 'p' );
messageNode.textContent = message;
Expand All @@ -273,6 +283,7 @@ const convertFormDataToObject = ( formData, includedFields = [] ) =>
form.replaceWith( messageContentElement.parentNode );
}
}
container.setAttribute( 'data-form-status', status );
form.style.opacity = 1;
submitButtons.forEach( button => {
button.disabled = false;
Expand Down Expand Up @@ -324,7 +335,7 @@ const convertFormDataToObject = ( formData, includedFields = [] ) =>
const googleLoginElements = document.querySelectorAll( '.newspack-reader__logins__google' );
googleLoginElements.forEach( googleLoginElement => {
const googleLoginForm = googleLoginElement.closest( 'form' );

const redirectInput = googleLoginForm.querySelector( 'input[name="redirect"]' );
const checkLoginStatus = metadata => {
fetch(
`/wp-json/newspack/v1/login/google/register?metadata=${ JSON.stringify( metadata ) }`
Expand All @@ -333,13 +344,14 @@ const convertFormDataToObject = ( formData, includedFields = [] ) =>
res
.json()
.then( ( { message, data } ) => {
const redirect = redirectInput?.value || null;
if ( googleLoginForm?.endLoginFlow ) {
googleLoginForm.endLoginFlow( message, res.status, data );
googleLoginForm.endLoginFlow( message, res.status, data, redirect );
}
} )
.catch( error => {
if ( googleLoginForm?.endLoginFlow ) {
googleLoginForm.endLoginFlow( error?.message );
googleLoginForm.endLoginFlow( error?.message, res.status );
}
} );
} )
Expand Down Expand Up @@ -387,8 +399,9 @@ const convertFormDataToObject = ( formData, includedFields = [] ) =>
}
} )
.catch( error => {
console.log( error );
if ( googleLoginForm?.endLoginFlow ) {
googleLoginForm.endLoginFlow( error?.message );
googleLoginForm.endLoginFlow( error?.message, 400 );
}
if ( authWindow ) {
authWindow.close();
Expand Down
3 changes: 2 additions & 1 deletion assets/reader-activation/auth.scss
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
}
}

&[data-form-status='400'] {
&[data-form-status='400'],
&[data-form-status='500'] {
.newspack-reader__auth-form {
&__response {
width: 100%;
Expand Down
6 changes: 3 additions & 3 deletions includes/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public static function render_auth_form( $is_inline = false ) {
}
$terms_text = self::get_setting( 'terms_text' );
$terms_url = self::get_setting( 'terms_url' );
$is_account_page = \get_the_ID() === \wc_get_page_id( 'myaccount' );
$is_account_page = function_exists( '\wc_get_page_id' ) ? \get_the_ID() === \wc_get_page_id( 'myaccount' ) : false;
$redirect = $is_account_page ? \wc_get_account_endpoint_url( 'dashboard' ) : '';
?>
<div class="<?php echo \esc_attr( implode( ' ', $classnames ) ); ?>" data-labels="<?php echo \esc_attr( htmlspecialchars( \wp_json_encode( $labels ), ENT_QUOTES, 'UTF-8' ) ); ?>">
Expand Down Expand Up @@ -685,7 +685,7 @@ public static function render_auth_form( $is_inline = false ) {
private static function send_auth_form_response( $data = [], $message = false, $redirect_url = false ) {
$is_error = \is_wp_error( $data );
if ( empty( $message ) ) {
$message = $is_error ? $data->get_error_message() : __( 'You are authenticated!', 'newspack' );
$message = $is_error ? $data->get_error_message() : __( 'Login successful!', 'newspack' );
}
if ( \wp_is_json_request() ) {
\wp_send_json( compact( 'message', 'data' ), \is_wp_error( $data ) ? 400 : 200 );
Expand Down Expand Up @@ -904,7 +904,7 @@ public static function process_auth_form() {
if ( true !== $sent ) {
return self::send_auth_form_response( new \WP_Error( 'unauthorized', __( 'Invalid account.', 'newspack' ) ) );
}
return self::send_auth_form_response( $payload, __( 'An account was already registered with this email. Please check your inbox for an authentication link.', 'newspack' ), $redirect );
return self::send_auth_form_response( $payload, __( 'Please check your inbox for an authentication link.', 'newspack' ), $redirect );
case 'register':
$metadata = [];
if ( ! empty( $lists ) ) {
Expand Down
12 changes: 8 additions & 4 deletions includes/oauth/class-google-login.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ public static function api_google_login_register( $request ) {
if ( $email ) {
$existing_user = \get_user_by( 'email', $email );
$message = __( 'Thank you for registering!', 'newspack' );
$data = [
'email' => $email,
'authenticated' => true,
'sso' => true,
'existing_user' => $existing_user ? true : false,
];

if ( $existing_user ) {
// Log the user in.
$result = Reader_Activation::set_current_reader( $existing_user->ID );
Expand All @@ -185,10 +192,7 @@ public static function api_google_login_register( $request ) {

return \rest_ensure_response(
[
'data' => [
'email' => $email,
'authenticated' => true,
],
'data' => $data,
'message' => $message,
]
);
Expand Down