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
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.
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
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!
Is it possible to authenticate using GoogleAuthProvider and FacebookAuthProvide?