Search code examples
pythondjangopython-3.xngrok

Django request.user become AnonymousUser after third party redirect


test.html:

<a href="https://auth.ebay.com/oauth2/authorize?    ...">authorize</a>

views.py:

from django.contrib.auth.decorators import login_required

@login_required
def myview(req):
    user = req.user

   return render(req, 'test.html')

For ebay's oauth process, you have to provide users with a link to ebay's server, which asks the user if they want to give credentials to you. If they accept, ebay redirects the user to a given url with a querystring containing the access key.

The problem is, when I authorize my app with ebay, the user gets redirected to my login page (despite already being logged in). If I remove the @login_required decorator, req.user returns AnonymousUser instead. This is a problem since I don't know which user to assign the access token to.

What am I missing here?

Note that I am using ngrok to tunnel my server, and I have no problems
rendering myview other than the fact that the user is Anonymous.


Solution

  • The problem was that when I logged the user in initially, I was using the domain localhost:8000 instead of my ngrok instance.

    Logging my user in using my ngrok address fixed the problem.