Reputation: 2527
So I'm trying to integrate auth0 with my existing django app and sqlite database. Login is working, the problem is I can't get the usernames inputted by auth0 through request to map on to the user models already present in my db.
This is the auth0 backend and right now I've hardcoded to return eq3 and 21 respectively for username and user_id.
def get_user_details(self, response):
# Obtain JWT and the keys to validate the signature
id_token = response.get('id_token')
jwks = request.urlopen('https://' + self.setting('DOMAIN') + '/.well-known/jwks.json')
issuer = 'https://' + self.setting('DOMAIN') + '/'
audience = self.setting('KEY') # CLIENT_ID
payload = jwt.decode(id_token, jwks.read(), algorithms=['RS256'], audience=audience, issuer=issuer)
return {'username': 'eq3',
'user_id': 21}
However when I go to views.py and put a print(request.user.username)
and print(request.user.id)
it prints eq306fa23456g204e8c
and 51
. When I go into my database, I see that it has indeed created a new user in the auth_user table, even though there's already a user with eq3 and user id 21 in the same table. Furthermore, if I input a random username such as foobar
, it will still create a new user, but it won't add the nonsensical alphanumeric string to the username, just foobar
. What is going on here? How can I get django to recognize that the username eq3 is already in the database and thus use that as the user from which to retrieve data for the app??
Upvotes: 1
Views: 479
Reputation: 2527
This was due to me not having setup a python-social-auth pipeline in my settings.py I needed to add an email column to my auth_user
table and overwrite the default settings with the following:
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.social_auth.associate_by_email', #this was the important one
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
)
Upvotes: 1