带有 Firebase 的 Django 身份验证项目
Django 是一个基于 Python 的 web 框架,可以让你快速创建高效的 web 应用程序。 当我们构建任何网站时,我们都需要一组组件:如何处理用户身份验证(注册、登录、退出),用于管理我们的网站、如何上传文件等的管理面板。Django 为我们提供了现成的组件,可以轻松使用它。
If you are new to Django then you can refer to Django Introduction and Installation.
To check how to create a new project with django and firebase, check – How to create a new project in Django using Firebase Database?
在这里,我们将学习如何使用 Firebase 作为数据库在 Django 中创建登录和注册。在 Firebase Authentication 中,我们需要启用登录方法
在 Firebase 仪表板上授予权限 –
第 1 步:转到身份验证 -> 登录方法 -> 电子邮件/密码。
第 2 步:启用电子邮件/密码并单击“保存”。
现在,我希望您已经在 Django 中创建了一个项目。如果没有,请参阅如何在 Django 中使用 MVT 创建基本项目?
创建 Django 身份验证项目 –
创建 URL,以映射urls.py中的请求
Python3
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
# Here we are assigning the path of our url
path('', views.signIn),
path('postsignIn/', views.postsignIn),
path('signUp/', views.signUp, name="signup"),
path('logout/', views.logout, name="log"),
path('postsignUp/', views.postsignUp),
]
Python3
from django.shortcuts import render
import pyrebase
config={
apiKey: "Use Your Api Key Here",
authDomain: "Use Your authDomain Here",
databaseURL: "Use Your databaseURL Here",
projectId: "Use Your projectId Here",
storageBucket: "Use Your storageBucket Here",
messagingSenderId: "Use Your messagingSenderId Here",
appId: "Use Your appId Here"
}
# Initialising database,auth and firebase for further use
firebase=pyrebase.initialize_app(config)
authe = firebase.auth()
database=firebase.database()
def signIn(request):
return render(request,"Login.html")
def home(request):
return render(request,"Home.html")
def postsignIn(request):
email=request.POST.get('email')
pasw=request.POST.get('pass')
try:
# if there is no error then signin the user with given email and password
user=authe.sign_in_with_email_and_password(email,pasw)
except:
message="Invalid Credentials!!Please ChecK your Data"
return render(request,"Login.html",{"message":message})
session_id=user['idToken']
request.session['uid']=str(session_id)
return render(request,"Home.html",{"email":email})
def logout(request):
try:
del request.session['uid']
except:
pass
return render(request,"Login.html")
def signUp(request):
return render(request,"Registration.html")
def postsignUp(request):
email = request.POST.get('email')
passs = request.POST.get('pass')
name = request.POST.get('name')
try:
# creating a user with the given email and password
user=authe.create_user_with_email_and_password(email,passs)
uid = user['localId']
idtoken = request.session['uid']
print(uid)
except:
return render(request, "Registration.html")
return render(request,"Login.html")
HTML
{% if message %}
{% endif %}
Sign In
HTML
Sign Up
HTML
Welcome
Python3
path('reset/', views.reset),
path('postReset/', views.postReset),
Python3
def reset(request):
return render(request, "Reset.html")
def postReset(request):
email = request.POST.get('email')
try:
authe.send_password_reset_email(email)
message = "A email to reset password is successfully sent"
return render(request, "Reset.html", {"msg":message})
except:
message = "Something went wrong, Please check the email you provided is registered or not"
return render(request, "Reset.html", {"msg":message})
HTML
{% if message %}
{% endif %}
Sign In
HTML
{% load static %}
{%if msg%}
{% endif%}
Document
Reset Your Password
视图.py
在这里,我们将使用我们的 firebase 凭据进行身份验证。
蟒蛇3
from django.shortcuts import render
import pyrebase
config={
apiKey: "Use Your Api Key Here",
authDomain: "Use Your authDomain Here",
databaseURL: "Use Your databaseURL Here",
projectId: "Use Your projectId Here",
storageBucket: "Use Your storageBucket Here",
messagingSenderId: "Use Your messagingSenderId Here",
appId: "Use Your appId Here"
}
# Initialising database,auth and firebase for further use
firebase=pyrebase.initialize_app(config)
authe = firebase.auth()
database=firebase.database()
def signIn(request):
return render(request,"Login.html")
def home(request):
return render(request,"Home.html")
def postsignIn(request):
email=request.POST.get('email')
pasw=request.POST.get('pass')
try:
# if there is no error then signin the user with given email and password
user=authe.sign_in_with_email_and_password(email,pasw)
except:
message="Invalid Credentials!!Please ChecK your Data"
return render(request,"Login.html",{"message":message})
session_id=user['idToken']
request.session['uid']=str(session_id)
return render(request,"Home.html",{"email":email})
def logout(request):
try:
del request.session['uid']
except:
pass
return render(request,"Login.html")
def signUp(request):
return render(request,"Registration.html")
def postsignUp(request):
email = request.POST.get('email')
passs = request.POST.get('pass')
name = request.POST.get('name')
try:
# creating a user with the given email and password
user=authe.create_user_with_email_and_password(email,passs)
uid = user['localId']
idtoken = request.session['uid']
print(uid)
except:
return render(request, "Registration.html")
return render(request,"Login.html")
登录.html
HTML
{% if message %}
{% endif %}
Sign In
注册.html
HTML
Sign Up
主页.html
HTML
Welcome
现在移动到您的项目目录并使用给定的命令运行我们的项目:
python manage.py runserver
登录页面
注册页面
主页
实施重置密码功能 -
在这里,我们将学习如何使用数据库作为 Firebase 在 Django 中重置密码。大多数情况下,您会忘记密码并想重置密码。所以在这篇文章中,我们将学习如何在 Django 中做到这一点。
网址.py
蟒蛇3
path('reset/', views.reset),
path('postReset/', views.postReset),
视图.py
在这里,我们基本上渲染到 Reset.html 页面,用户将在其中输入他/她注册的电子邮件 ID,并会收到一封电子邮件以重置密码。 send_password_reset_email是 firebase 重置密码的预定义方法。
蟒蛇3
def reset(request):
return render(request, "Reset.html")
def postReset(request):
email = request.POST.get('email')
try:
authe.send_password_reset_email(email)
message = "A email to reset password is successfully sent"
return render(request, "Reset.html", {"msg":message})
except:
message = "Something went wrong, Please check the email you provided is registered or not"
return render(request, "Reset.html", {"msg":message})
登录.html
HTML
{% if message %}
{% endif %}
Sign In
重置.html
HTML
{% load static %}
{%if msg%}
{% endif%}
Document
Reset Your Password
现在移动到您的项目目录并使用给定的命令运行我们的项目:
python manage.py runserver
单击单击此处重置,然后您将被重定向到另一个页面。
输入您的电子邮件并单击发送重置链接,然后您将在电子邮件中收到一个链接以更改密码。
将出现一个警告框,确认已发送邮件。
现在,您将收到这样的邮件
.单击它以查看邮件的描述。
在这里,单击给定的链接以更改您的密码。
在这里,输入一个新密码并点击Save 。
现在,您可以使用新密码登录。