Skip to content

Commit

Permalink
Move logged-in user redirection to an earlier hook
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Nov 9, 2018
1 parent c9d09ac commit 7324a03
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions WP_Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,18 @@ public function render_auth0_login_css() {
return;
}

// If the user has a WP session, determine where they should end up and redirect.
if ( is_user_logged_in() ) {
$login_redirect = empty( $_REQUEST['redirect_to'] ) ?
$this->a0_options->get( 'default_login_redirection' ) :
filter_var( $_REQUEST['redirect_to'], FILTER_SANITIZE_URL );

// Add a cache buster to avoid an infinite redirect loop on pages that check for auth.
$login_redirect = add_query_arg( time(), '', $login_redirect );
wp_safe_redirect( $login_redirect );
exit;
}

wp_enqueue_style( 'auth0', WPA0_PLUGIN_CSS_URL . 'login.css', false, WPA0_VERSION );
}

Expand All @@ -397,18 +409,6 @@ public function render_form( $html ) {
return $html;
}

// If the user has a WP session, determine where they should end up and redirect.
if ( is_user_logged_in() ) {
$login_redirect = empty( $_REQUEST['redirect_to'] ) ?
$this->a0_options->get( 'default_login_redirection' ) :
filter_var( $_REQUEST['redirect_to'], FILTER_SANITIZE_URL );

// Add a cache buster to avoid an infinite redirect loop on pages that check for auth.
$login_redirect = add_query_arg( time(), '', $login_redirect );
wp_safe_redirect( $login_redirect );
exit;
}

ob_start();
require_once WPA0_PLUGIN_DIR . 'templates/login-form.php';
renderAuth0Form();
Expand Down
4 changes: 2 additions & 2 deletions tests/testRenderForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function testThatLoggedInUserIsRedirected() {
// Use the default login redirection.
$caught_exception = false;
try {
self::$wp_auth0->render_form( self::$html );
self::$wp_auth0->render_auth0_login_css();
} catch ( Exception $e ) {
$err_msg = unserialize( $e->getMessage() );
$caught_exception = 0 === strpos( $err_msg['location'], 'http://example.org' ) && 302 === $err_msg['status'];
Expand All @@ -118,7 +118,7 @@ public function testThatLoggedInUserIsRedirected() {

$caught_exception = false;
try {
self::$wp_auth0->render_form( self::$html );
self::$wp_auth0->render_auth0_login_css();
} catch ( Exception $e ) {
$err_msg = unserialize( $e->getMessage() );
$caught_exception = 0 === strpos( $err_msg['location'], $_REQUEST['redirect_to'] ) && 302 === $err_msg['status'];
Expand Down

0 comments on commit 7324a03

Please sign in to comment.