Press ESC to close

38 3

Python Django with Google Firebase – Authentication

In this part of Python Django with Google Firebase tutorial we’ll extend Firebase Authentication and explore how we can create user SignUp Form and maintain each user data in database with additional functionality of Invalid  SignUp alert (existing user, invalid email/ password format).

Check out this video, code below follows the video to help

If you like videos like this consider donating $1, or simply turn off AdBlocker. Either helps me to continue making tutorials.
 
 
Transcript / Cheat Sheet :
 
 
Templates:
First we need to edit our ‘SignIn.html’ template in which we will add a button for SignUp which will be redirected to ‘SignUp.html’, 
 
SignIn.html
 
{% if messg %}
<script>
alert({{ messg }});
</script>
{% endif %}
<html lang=en>
<head>
<meta charset=UTF-8>
<title>Sign</title>
</head>
<body>
<form action=/postsign/ method=post>
{% csrf_token %}
Email :
<input type=email name=email><br><br>
Password:
<input type=password name=pass><br><br>
<input type=submit value=SignIn>
<button type=button onclick=location.href='{% url ‘signup’ %}’>SignUp</button>
</form>
</body>
</html>

SignUp.html 

 

 

 

A simple Signup form for creating user account, using POST operation sends user info to views.py which creates user account using pyrebase library, here if condition is added for getting invalid SignUp alert.
 
{% if messg %}
<script>
alert({{ messg }});
</script>
{% endif %}
<html lang=en>
<head>
<meta charset=UTF-8>
<title>Sign</title>
</head>
<body>
<form action=/postsignup/ method=post>
{% csrf_token %}
Name:
<input type=text name=name><br><br>
Email :
<input type=email name=email><br><br>
Password:
<input type=password name=pass><br><br>
<input type=submit value=SignUp>
</form>
</body>
</html>
g

Views.py:



























































import pyrebase
from django.shortcuts import render
from django.contrib import auth
config = {
apiKey: AIzaSyB0Il0NLQPxxDyMgoE0fOMd4pYUkbkZVvI,
authDomain: cpanel-5e873.firebaseapp.com,
databaseURL: https://cpanel-5e873.firebaseio.com,
projectId: cpanel-5e873,
storageBucket: cpanel-5e873.appspot.com,
messagingSenderId: 579985583952
}
firebase = pyrebase.initialize_app(config)
authe = firebase.auth()
database=firebase.database()
def signIn(request):
return render(request, signIn.html)
def postsign(request):
email=request.POST.get(email)
passw = request.POST.get(pass)
try:
user = authe.sign_in_with_email_and_password(email,passw)
except:
message=invalid credentials
return render(request,signIn.html,{messg:message})
print(user[idToken])
session_id=user[idToken]
request.session[uid]=str(session_id)
return render(request, welcome.html,{e:email})
def logout(request):
auth.logout(request)
return render(request,signIn.html)
def signUp(request):
 
return render(request,signup.html)
def postsignup(request):
 
name=request.POST.get(name)
email=request.POST.get(email)
passw=request.POST.get(pass)
try:
user=authe.create_user_with_email_and_password(email,passw)
uid = user[localId]
data={name:name,status:1}
database.child(users).child(uid).child(details).set(data)
except:
message=Unable to create account try again
return render(request,signup.html,{messg:message})
 
return render(request,signIn.html)

Urls.py :












from django.contrib import admin
from django.conf.urls import url
from . import views
urlpatterns = [
url(r^admin/, admin.site.urls),
url(r^$,views.signIn),
url(r^postsign/,views.postsign),
url(r^logout/,views.logout,name=log),
url(r^signup/,views.signUp,name=signup),
url(r^postsignup/,views.postsignup,name=postsignup),
]
That’s end up Firebase Authentication with Python Django for more functionality refer Pyrebase library page. In next tutorial we will cover Python django with Firebase database if you guys facing some issue comment out 🙂

Whole Project Source Code here   


3 Comments

  1. Anonymous on January 16, 2019

    Hello, I got an OPERATION_NOT_ALLOWED from firebase with create_user_with_email_and_password(email,passw). Any idea to fix this issue? I think it could be related with the csrf token maybe. Thanks!

    Reply
  2. Unknown on January 17, 2019

    Is it possible to authenticate using GoogleAuthProvider and FacebookAuthProvide?

    Reply
  3. Go Lagu on May 18, 2020

    This will be the correct weblog for everyone who is wishes to learn about this topic. You already know much its almost challenging to argue along with you (not too I actually would want…HaHa). You certainly put a different spin for a topic thats been discussing for a long time. Great stuff, just wonderful! https://royalcbd.com/product/cbd-oil-250mg/

    Reply

Leave a Reply