Support » Plugin: Tim's Nextcloud SSO OAuth2 » Return to same page after login

  • Resolved gnomeore

    (@gnomeore)


    I use the WP Download Manager plugin. If users are not logged in the Download button on the page that they are on turns into a log in button. So when users click on this and login using Nextcloud they get returned to the ‘On login redirect user to’ URL in the settings. I tried to leave this setting blank but logging in just sent me to my site’s main page.

    Is there a way for the user to return to the page that they were on when they logged in? Maybe saved in the session or cookie?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Tim Oxendale

    (@timssolutions)

    Hey, make sure you are on version 1.9 or later (I have added some new filter hooks in this version). Then if you add the following into your theme functions.php file, that should do it!

    add_filter('tims_nso_nextcloud_login_button_url', 'add_redirect_to_query_string');
    function add_redirect_to_query_string($login_button_url){
    	if(get_the_ID()){
    		$login_button_url = $login_button_url.'&redirect_to='.urlencode(esc_url_raw(get_permalink(get_the_ID())));
    	}
    	return $login_button_url;
    }
    
    add_filter('tims_nso_authorize_url', 'add_redirect_to_query_string_to_session');
    function add_redirect_to_query_string_to_session($authorize_url){
    	if(session_status() === PHP_SESSION_NONE) {
    	    session_start();
    	}
    	if(isset($_GET['redirect_to'])){
    		$_SESSION['original_page'] = esc_url_raw(urldecode($_GET['redirect_to']));
    	}
    	return $authorize_url;
    }
    
    add_filter('tims_nso_successful_login_redirect', 'redirect_user_back_to_original_page');
    function redirect_user_back_to_original_page($redirect_url){
    	if(session_status() === PHP_SESSION_NONE) {
    	    session_start();
    	}
    	if(isset($_SESSION['original_page']) && filter_var($_SESSION['original_page'], FILTER_VALIDATE_URL)){
    		$redirect_url = esc_url_raw($_SESSION['original_page']);
    	}
    	return $redirect_url;
    }

    so the first filter gets the page URL from the page ID and adds it to the Nextcloud login button redirect_to=…

    the second checks the redirect_to=… parameter and adds it to the session just before the user goes off to Nextcloud.

    Then the final one checks the session for the redirect and sets the redirect URL to that page.

    Thread Starter gnomeore

    (@gnomeore)

    Thanks again for the great support!
    I created fresh wordpress and Nextcloud sites (latest versions).
    I installed version 1.9 of your plugin.

    I created a (Sydney) Child Theme, changed to it, and used the Code Snippets plugin to insert your code into that theme’s functions.php file.
    I use the url from your button (https://site.xxxx.net/wp-login.php?nc-sso=redirect) both in my login menu item and to replace my Download button with a ‘Please login to download’ link when a member is not logged in.
    I have left the ‘On login redirect user to’ in your Settings empty.

    While logged off, I went to the page with the download on it and clicked on my ‘Please login to download’ link.
    It logged in, but went to the main page not the Download page. I also went to another page (while logged off) and clicked on my ‘Log in’ in the menu bar and it also went back to the main page.

    I tried this with the ‘Temp Key Storage Type’ set to both ‘Session’ and ‘Cookie’.
    I also disabled the ‘Code Snippets’ and inserted your php code directly into the Child’s functions.php file. The results were the same.

    When I enter a url into the ‘On login redirect user to’ field it uses that as its redirect and goes to that page.

    Any suggestions or am I doing something wrong?
    I can send the log file or give you access to the sites if you wish.

    [Using with Firefox 100.0.2 (64 bit) and Safari 15.1 on macOS 12.0.1]

    Plugin Author Tim Oxendale

    (@timssolutions)

    Hey, sorry for the slow reply, if you’re doing it that way you would need to add the redirect URL to the login link, e.g. https://site.xxxx.net/wp-login.php?nc-sso=redirect&redirect_to=ULR_to_go_back_to

    Plugin Author Tim Oxendale

    (@timssolutions)

    Hey, just a follow-up. Version 2.0.0 will manage all of this for you now!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.