简体   繁体   中英

How to add query parameters to django-allauth LOGIN_REDIRECT_URL?

  1. My JS file is embedded on a third party page
  2. the JS file talks to my server using a jsonp request which returns a value which gets stored inside variable x
  3. now i want to authenticate this user but to do that correctly(for my app logic to work) i need the value of x stored inside the JS specific to the third party page
  4. I use facebook login through allauth

window.open("http://localhost:8000/accounts/facebook/login/?method=oauth2", "AskAFriend Facebook Login","menubar=0,toolbar=0;scrollbars=0;dialog=0;resizable=1,width=480px,height=320px");

  1. the code above opens a new window for the facebook login process, which when completed redirects to a view i have designed in my django app, i have achieved this by providing the url to this view in the LOGIN_REDIRECT_URL setting in settings.py

  2. now i need a way to add a parameter to this request which can reach my view after the login completes, how can i achieve this?

  3. i have tried adding it to the url i used in my JS like window.open("http://localhost:8000/accounts/facebook/login/?method=oauth2&*valueofx=' + x* ... (did not work)(ignore the *)

  4. I have read about overriding the get_login_redirect_url() function by using a custom adapter but i can't figure out how to do it for my specific case

You could change LOGIN_REDIRECT_URL to something like /redirect and this will in turn be an 'intermediary' view that controls where the user gets redirected to.

That view could look something like this (untested):

from django.views.generic import View
from django.http import HttpResponseRedirect

class LoginRedirectIntermediary(View):
    """
    Decides where a newly logged in user should go based on the request.
    """
    def get(self, request, *args, **kwargs):
        if request.user.is_superuser:
            url = '/admin-panel/{}/'
        else:
            url = '/user-panel/{}/'

        return HttpResponseRedirect(url.format(request.user.username))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM