rtCamp / login-with-google

Minimal plugin which allows WordPress user to login with google.
https://wordpress.org/plugins/login-with-google/
GNU General Public License v2.0
63 stars 17 forks source link

When authenticating from One Tap Login how to redirect user to same page. #95

Closed git-bhanu closed 2 years ago

git-bhanu commented 2 years ago

First of all, great plugin. It works as it is supposed to work out of the box.

I am in need of some custom changes. I am interested in redirecting user to the same page from where he/she actually tried to authenticate himself/herself.

I found the concerned filter.

function google_redirect_after_authentication() {
    return home_url() . "/product/XXXXXXXXXXXXX/";
}

add_filter( 'rtcamp.google_default_redirect', __NAMESPACE__.'\\google_redirect_after_authentication' );

Right now, I have hardcoded the URL, but I intend to make it dynamic.

The issue being how do I identify what page the user logged in? Is this data passed when logged in? I would be very thankful if you could help me out in setting this up.

Thanks again.

git-bhanu commented 2 years ago

I took some time to look into the login for OneTapLogin.php.

It seems that here on (https://github.com/rtCamp/login-with-google/blob/master/src/Modules/OneTapLogin.php#L185-L198) $decoded_states redirect_to value is the url which it is getting redirected to.

$redirect_to   = apply_filters( 'rtcamp.google_default_redirect', admin_url() );
$state         = Helper::filter_input( INPUT_POST, 'state', FILTER_SANITIZE_STRING );
$decoded_state = $state ? (array) ( json_decode( base64_decode( $state ) ) ) : null;

if ( is_array( $decoded_state ) && ! empty( $decoded_state['provider'] ) && 'google' === $decoded_state['provider'] ) {
    $redirect_to = $decoded_state['redirect_to'] ?? $redirect_to;
}

wp_send_json_success(
    [
        'redirect' => $redirect_to,
    ]
);
die;

How can that be changed to the present page where the oneTap UI is shown ?

ned-bs commented 2 years ago

need help for same issue. users should stay in same page.

git-bhanu commented 2 years ago

need help for same issue. users should stay in same page.

Hey @bsahins, I made a fork of the plugin to fix this. But its private right now. I don't think its possible without making an edit to the possible.

ned-bs commented 2 years ago

Hi @bhanu-krenovate, If your version is working it is OK for me :) Can you share the updated file?

vishalkakadiya commented 2 years ago

@bhanu-krenovate @bsahins You can use rtcamp.google_login_state filter to set the current URL to maintain the redirect URL in the state. I am sharing the below code as an example, which can help you to achieve this.

function lwg_google_login_state( $args ) {

    $request_uri = filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING );

    $args['redirect_to'] = home_url( $request_uri );

    return $args;
}
add_filter( 'rtcamp.google_login_state', 'lwg_google_login_state', 100 );

Let me know how it goes, thanks!

ned-bs commented 2 years ago

Will try asap. But I suggest to put a small settings about this. So admin can choose where to go. thanks

aviral-mittal commented 2 years ago

Solution has already been shared by Vishal, and no changes are required in the plugin for the same.