Social Authentication, widely known as social sign-in, uses social networking platforms’ information to facilitate our applications’ logins. This process is designed in a way to simplify login and registration experiences. It provides a better and convenient alternative to create a mandatory account creation.
Social authentication is a smooth process to accessing sites and apps for users, one that doesn’t require various credentials and a lengthy registration process.
It is a very attractive feature for developers that can help streamline user verification and provide reliable user data for personalization.
Here I will discuss how to use the Firebase authentication system as your REST API’s identity provider using the Django Rest Framework.
Prerequisites:
This blog will cover how to set up Firebase admin SDK into the Django rest framework and use it. The steps are as follows-
Generate secret key from Firebase:
pip install Firebase-admin
import Firebase_admin from Firebase_admin import credentials from Firebase_admin import auth cred = credentials.Certificate({ "type": "service_account", "project_id": os.getenv('FIREBASE_PROJECT_ID'), "private_key_id": os.environ.get('FIREBASE_PRIVATE_KEY_ID'), "private_key": os.environ.get('FIREBASE_PRIVATE_KEY'), "client_email": os.environ.get('FIREBASE_CLIENT_EMAIL'), "client_id": os.environ.get('FIREBASE_CLIENT_ID'), "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": os.environ.get('FIREBASE_CLIENT_CERT_URL') })
default_app = Firebase_admin.initialize_app(cred)
def Firebase_validation(id_token): """ This function receives id token sent by Firebase and validate the id token then check if the user exist on Firebase or not if exist it returns True else False """ try: decoded_token = auth.verify_id_token(id_token) uid = decoded_token['uid'] provider = decoded_token['firebase']['sign_in_provider'] image = None name = None if "name" in decoded_token: name = decoded_token['name'] if "picture" in decoded_token: image = decoded_token['picture'] try: user = auth.get_user(uid) email = user.email if user: return { "status": True, "uid": uid, "email": email, "name": name, "provider": provider, "image": image } else: return False except UserNotFoundError: print("user not exist") except ExpiredIdTokenError: print("invalid token")
class SocialSignupAPIView(GenericAPIView): """ api for creating user from social logins """ def post(self, request): auth_header = request.META.get('HTTP_AUTHORIZATION') if auth_header: id_token = auth_header.split(" ").pop() validate = Firebase_validation(id_token) if validate: user = CustomUser.objects.filter(uid = validate["uid"]).first() if user: data = { "id": user.id, "email": user.email, "name": user.name, "image": user.image, "type": "existing_user", "provider": validate['provider'] } return Response({“data”: data, “message”: “Login Successful” }) else: user = CustomUser(email = validate['email'], name = validate['name'], uid = validate['uid'], image = validate['image'] ) user.save() data = { "id": user.id, "email": obj.email, "name": obj.name, "image": obj.image, "type": "new_user", "provider": validate['provider'] } return Response({“data”: data, “message”: “User Created Successfully” }) else: return Response({“message”: “invalid token”}) else: return Response({“message”: “token not provided”})
from Django.urls import path from .views import SocialSignupAPIView urlpatterns = [ path('socialSignup', SocialSignupAPIView.as_view(), name= social-signup), ]
That’s it!! All set to apply your Firebase authentication with your Django rest framework. Now you can signup and log in your users through social platform accounts with the help of Firebase.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowMindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and MindBowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Our CISO was extremely impressed by Mindbowser’s work. It is pretty rare to see this kind of clean security report so early in the company’s journey. Huge Thank you for the disciplined approach here.
Founder, TrestleIQ
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
The flexibility and capacity of the Mindbower staff has been impressive.
CEO, ProofPilot
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.
Founder, Courtyardly
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork
We cannot express enough how pleased we are with the entire Mindbowser team. From our first call, they focused on our vision and ensured things were handled as per requirement. Communication was so easy and flexible to our schedule. I’m...
Founder, SafeZone Safety Systems