Why do you need this?
While working on a side-project(shhh!!), which had a mobile application as a frontend and django as backend, I wanted to incorporate social logins as well(who wants to create a new account today? ).
Now I wanted a
User with some extra properties than the existing
User model that
I had 2 options to do this:
- Extend the existing
- Create a new
Userand had a
I went with the later approach. Reasons for that I’ll explain in some other blog post
After doing this, I started finding ways to authenticate user using their social media access tokens. While doing that I faced 2 problems:
- Most of the
Django’s existing libraries for social login, authenticates the
Usermodel and not a
- Every library I could find was actually providing a lot of abstraction layers(which is a good thing) that I didn’t want. I wanted full control over my user model.
What I did?
While I was exploring with Facebook Devs account, as to how to get user’s info using the access token. I stumbled upon Facebook’s GraphQL API. You simply hit the API with the access token and it fetches the data for you(That’s what APIs do, duh!!).
It’s that simple. So I thought maybe I could do this for
I used python’s wonderful package
requests and started hitting the social media’s APIs for user’s info. I used the fetched fields as needed for my
User model and discarded the rest of them.
For achieving this I created:
Classfor every social media that I wanted to authenticate with. Those classes would extend an
Abstract Classthat has method
get_user_infowhich is needed to be overridden.
APIServicewhich takes in
auth_sourceand delegate the work to the appropriate service based on
The code for this was initally embedded as service in my backend application. Later, I realised, why not extract it out in a pip package for all of the world to use?
What does the package do?
I created a
social-user-info. All you need is to give the social-media you want to login with, and the access token of the user you want information of.
Currently it supports the following social media:
Issues opened for:
How to use this package?
Install the package into your application using
For a more detailed guide, visit the project on github
Follow me on Github
Feel free to reach out to me on my website