James L.
James L.

Reputation: 1153

redirect to last page not working on python social auth

I'm using python social auth to use social logins but I'm unable to redirect to the last page after successful login.

For instance if I'm on the following page http://localhost:8000/docprofile/14/and click the login button, instead of redirecting me to the last page http://localhost:8000/docprofile/14/ it redirects me to the login page.

If I put this:

<a href="{% url 'social:begin' 'facebook' %}?next={{ request.path }}"> | Login with Facebook</a>

It redirects to the login page and the url ends with strange characters:

http://localhost:8000/login/#_=_

I also tried this:

<a href="{% url 'social:begin' 'facebook' %}?next={{ request.get_full_path }}"> | Login with Facebook</a>

This time it does take the path of the /docprofile/14 but still doesn't redirect me back and takes me to the login page with the url http://localhost:8000/login/?next=/docprofile/14/#_=_

Upvotes: 6

Views: 1091

Answers (3)

Barney Szabolcs
Barney Szabolcs

Reputation: 12514

Solution:

<a class="fb-login-placeholder" href="{% url 'social:begin' 'facebook' %}?next={{ request.GET.next }}">| Login with Facebook</a>

that is instead of request.path you need request.GET.next.
And indeed, to get rid of the funny characters use @AshishGupta's answer.

Upvotes: 0

ThrowsException
ThrowsException

Reputation: 2636

What I had to do was strip off the next parameter from the query string in my GET and modify the html to include it in the template like this

def home(request):
    c = context()
    if request.GET.get('next'):
        c = context(next=request.GET['next'])

    return render_to_response('home.html',
                              context_instance=RequestContext(request, c))

def context(**extra):
    return dict({
        'available_backends': load_backends(settings.AUTHENTICATION_BACKENDS)
    }, **extra)

And my template now gets the next parameter from the context and when i log in the redirect works.

<a class="col-md-2 btn btn-default" name="{{ backend|backend_class }}" href="{% url "social:begin" backend=name %}?next={{next}}">

Upvotes: 1

Ashish Gupta
Ashish Gupta

Reputation: 2614

You can include this in head of your template of the redirected page -

<script type="text/javascript">
   if (window.location.hash == '#_=_') {
      window.location.hash = '';
   }
</script>`

It will do the trick!

Upvotes: 0

Related Questions