Add filter for Google auth error redirect - #394
Conversation
There was a problem hiding this comment.
Pull request overview
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Adds a new WordPress filter to allow redirecting users to a custom URL when Google authentication fails, instead of always returning a WP_Error and re-rendering wp-login.php.
Changes:
- Introduces
rtcamp.google_login_failed_redirectfilter inLogin::authenticate()and performs a safe redirect when it returns a non-empty URL. - Adds a unit test to verify redirect behavior when the filter returns a URL.
- Documents the new filter in
README.md.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/Modules/Login.php |
Applies new failed-login redirect filter and redirects on non-empty URL. |
tests/php/Unit/Modules/LoginTest.php |
Adds unit test asserting redirect occurs when filter returns a URL. |
README.md |
Documents the new filter and its parameters. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $redirect_to = apply_filters( 'rtcamp.google_login_failed_redirect', '', $error ); | ||
|
|
||
| if ( is_string( $redirect_to ) && '' !== $redirect_to ) { | ||
| wp_safe_redirect( $redirect_to, 302, 'Login with Google' ); | ||
| exit; | ||
| } |
| WP_Mock::onFilter( 'rtcamp.google_login_failed_redirect' ) | ||
| ->withAnyArgs() | ||
| ->reply( 'https://example.com/custom-error' ); |
| ->with( 'abc' ) | ||
| ->willThrowException( new Exception( 'Exception for test' ) ); | ||
|
|
||
| Mockery::mock( 'WP_Error' ); |
|
Thanks @copilot — went through all three: 1. Validate redirect URL before use ( 2. Tighten the filter-arg assertion ( 3. |
What & why
Adds a filter,
rtcamp.google_login_failed_redirect, so sites can control where a user lands when Google authentication fails. CurrentlyLogin::authenticate()hardcodes aWP_Errorreturn on failure (src/Modules/Login.php), and WordPress re-renderswp-login.phpwith that error — there is no way to send the user to a custom/friendly error page.Closes #173
Approach
In the
catch ( Throwable $e )block, after building theWP_Error, apply a new filter passing the default ('') and theWP_Error. If a consumer returns a non-empty string, it is run throughwp_validate_redirect()(empty fallback); only when a validated URL survives do wewp_safe_redirect()there andexit. Otherwise the originalWP_Erroris returned. Fully backward-compatible — default behavior is unchanged, and invalid/off-site filter values fall through to the default error rather than silently redirecting towp-admin.Testing
Run the added unit test and the module suite:
testAuthenticationExceptionRedirectsWhenFilterReturnsUrlpasses (PHP 8.3 and 8.5)phpcsclean on changed filesWP_Errorstill returned when no filter is set (testAuthenticationCapturesExceptionsstill green)WP_Errorinstead of redirecting (viawp_validate_redirect)README.md