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.
Nihar is a full stack developer with around 1 year of experience. He is an expert in building python Integrated web applications, creating REST API’s with well designed, testable and efficient code. He is always eager to learn new programming languages and technologies and looking for new ways to optimize the development process.
Get the latest updates by sharing your email.
Flexible Engagement Model | Secure & Scalable Apps | First Time Right Process
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
We had very close go live timeline and MindBowser team got us live a month before.
They were a very responsive team! Extremely easy to communicate and work with!
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Mindbowser was very helpful with explaining the development process and started quickly on the project.
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.
Mindbowser is professional, efficient and thorough.
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
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.
They're very tech-savvy, yet humble.
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
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.
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.
We are a small non-profit on a budget and they were able to deliver their work at our prescribed budgets. Their team always met their objectives and I'm very happy with the end result. Thank you, Mindbowser team!!