redirect to last page not working on python social auth
Asked Answered
C

3

6

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/#_=_

Carley answered 15/11, 2014 at 18:34 Comment(3)
Beside the redirect issue, the authentication is working fine?Binns
@Binns Yes, the authentication is working fine. It's just the redirection is the issue!Carley
Did you find any solution for this issue?Estaestablish
B
1

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}}">
Brucine answered 17/7, 2015 at 23:46 Comment(0)
J
0

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!

Julietajulietta answered 28/1, 2015 at 0:48 Comment(0)
F
0

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.

Frothy answered 4/11, 2018 at 1:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.