makeDEVeasy
Published in

makeDEVeasy

Authenticate the user using python flask and firestore

krishna ojha

I saw so many articles during my project to do sigin & signup with python flask and firestore, but no where i was not able to understand well to proceed futher.

Atlast after seeing somany youtube tutorials and reading some articles, i got the clear picture to proceed with flask and firestore. As a MDE (makeDEVeasy) initiative, today i was brought this information to you.

In this article, we are using pyrebase also, it is python wrapper to firestore/firebase. From this article, you will get more detailed understanding about backend and constructing the json api’s towards signup and signin.

Code:-

import os
from flask import Flask, request, jsonify
from firebase_admin import credentials, firestore, initialize_app
import firebase_admin
import pyrebase
from flask_cors import CORS
import io
import json
auth = firebase.auth()
from firebase_admin import auth
app=Flask(__name__)
cred = credentials.Certificate("key.json")
default_app=firebase_admin.initialize_app(cred)
CORS(app)
db = firestore.client()
todo_ref=db.collection('todos') #sample collections
pb = pyrebase.initialize_app(json.load(open('fbconfig.json')))
@app.route("/") #basic api
def home():
return "hello all"

Sign-up Api:-

@app.route('/signup',methods=['POST'])   #signup api
def signup():
email=request.json['email'] #get the email from json
password=request.json['password'] #get the password from json
if email is None or password is None:
return jsonify({'message':'username and password must not in blank'}),400
try:
user = auth.create_user(
email=email,
password=password
)
user = pb.auth().sign_in_with_email_and_password(email, password)
#pb.auth().send_email_verification(user['idToken'])
return jsonify({'message': f'Successfully created user and send verification link please activate your account '}),200
except:
if email:
emailexists=auth.get_user_by_email(email)
if(emailexists.uid):
return jsonify({'message': 'user is already exists '}),400-
else:
return jsonify({'message': 'error creating in user'}),400

Sign-in Api:-

@app.route('/signin',methods=['POST'])  #signin api
def signin():
email=request.json['email']
password=request.json['password']
if email is None or password is None:
return jsonify({'message':'username and password must not to be empty'})
try:
user = pb.auth().sign_in_with_email_and_password(email, password)
print(user)
arr=''

for x in user:
if x == 'localId':
arr=(user[x])

user1= auth.get_user(arr)
user3=user1.email_verified
print(user3)
if user3:
return user
else:
return jsonify({'message':'please verify your account with your mailId'}),400
except:
return jsonify({'message':'invalid crendentails please enter with valid credentials'}),400

If you necessary to do *email verification then please mention it below line code in signup.

pb.auth().send_email_verification(user['idToken'])

*=No need to setup any extra email template, (firebase) it have the default email template it will send to your respective email address during signup.

If you need custom email template go to console.firebase.com, select firebase project -> authentication->Templates.

I hope you understand and stay tuned for upcoming articles….

Also If you want to learn more about it, You can explore different official coding communities 👇…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
makeDEVeasy

makeDEVeasy

Initiative to make the every developer easy, when they look into complex problems and make them to learn more simpler as easy……