Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The login page is not redirecting to the access controlled page? #199

Closed
bobrocke opened this issue Feb 3, 2019 · 10 comments
Closed

The login page is not redirecting to the access controlled page? #199

bobrocke opened this issue Feb 3, 2019 · 10 comments

Comments

@bobrocke
Copy link

bobrocke commented Feb 3, 2019

I have a site with an access controlled page at addpage-blog-post which redirects to the login page, authenticate, as expected. But once logged in, the user is not redirected back to the addpage-blog-post as the default behavior suggests.

authenticate.md has login_redirect_here: false set. And the plug-in YAML configuration is default with redirect_after_login: .

What have I misconfigured?

@otahirs
Copy link

otahirs commented Feb 10, 2019

I am experiencing the same issue, can not get redirection after login get to work as expected.

In short, is there a simple way to have a button/link navigating to a login page, and after successful login get redirected back to the page on which the login prompt was started?

(I am displaying additional content on a page when user is logged in, so no place basic for access: site.login: true)

@otahirs
Copy link

otahirs commented Feb 11, 2019

For anyone interested, I got it working by using custom login page "/login" and custom php twig extension, called from a template user/plugins/login/templates/login.html.twig

login.html.twig

{% extends 'partials/base.html.twig' %}

{% block content %}
    {{phpLoginRedirect()}}
    {% include 'partials/messages.html.twig' %}
    {% include 'partials/login-form.html.twig' %}
{% endblock %}

the function

function LoginRedirect(){
        // placed in "user/plugins/login/templates/login.html.twig" - {{phpLoginRedirect()}}
        
        if($_SESSION["user"]["authenticated"]){
            $authenticated = True;
        }

        if(isset($_SERVER['HTTP_REFERER'])) {
            if(!$authenticated){
                // if user is not logged in, save refferer
                $_SESSION['ref'] = $_SERVER['HTTP_REFERER'];
            }
            else{
                $protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';

                $login_path = $protocol . "://" . $_SERVER['HTTP_HOST'] . "/login";
                $refferer_path = $_SERVER['HTTP_REFERER'];
 
                // referrer is a login page and we successfully logged in -> redirect to last page before login page
                if(isset($_SESSION['ref']) && $login_path == $refferer_path){
                    if($_SESSION['ref'] != $login_path){
                        header('Location: ' . $_SESSION['ref']);
                        exit();
                    }
                }
            }          
        }
    }

EDIT: few tweaks and https compatible

@bobrocke
Copy link
Author

@otahirs Is this a universal fix? Is it worth offering as a pull request?

@otahirs
Copy link

otahirs commented Feb 11, 2019

@bobrocke It is just a dirty workaround as it is not using the plugin default files and rely on caling separate php code within a template and the /login path is absolute to my setup. I made it after failing trying to understand the plugin own php code as I am not that experienced programmer.

However, the idea behind it works and with a little bit of php knowledge could be easily implemented into the existing plugin.

@bobrocke
Copy link
Author

I'm still having the problem after updating to Grav v1.6.0-rc.2 and Login v3.0.0-rc.2.

@rhukster rhukster added the bug label Feb 20, 2019
@lauroguedes
Copy link
Contributor

Any news about resolving this bug? I have been having this problem since version 1.5. When logging in, it does not return to the page that was previously requested as it should be the default behavior of the plugin. Help us

@Heraes-git
Copy link

Same for me.

@rhukster
Copy link
Member

Can you guys please test the commit above?

@lauroguedes
Copy link
Contributor

Now, it worked perfectly, thank you.

@buzatuAda
Copy link

buzatuAda commented Jul 1, 2019

do you have an approximate date when this fix will go in the stable release (3.0.3)?

@mahagr mahagr closed this as completed Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants