首页 > 解决方案 > 如何使用 django python 从 firebase 注销

问题描述

我正在尝试使用 python 和 firebase 从后端创建用户身份验证。到目前为止,创建用户和登录操作效果很好。但不知何故,我找不到任何有关 signOut 操作的信息。我正在使用 Pyrebase 库。有人有建议吗?这是它起作用的部分:

 import pyrebase
config = {
    #my private config informations
  }

firebase = pyrebase.initialize_app(config)
auth = firebase.auth()

def createAccount(request):
    if request.method == "POST":
        email=request.POST.get("email")
        password = request.POST.get("password")

        auth.create_user_with_email_and_password(email, password)
        return render(request, 'beforeLogin/landingPage.html')

def verifyLogin(request):
    if request.method == 'POST':
        email=request.POST.get("email")
        password = request.POST.get("password")
        try:
            auth.sign_in_with_email_and_password(email, password)
        except:
            messages.error(request,'Email or password is not correct.')
            return render(request, 'beforeLogin/landingPage.html')
        return redirect(settings.LOGIN_REDIRECT_URL)
    return HttpResponse("why y r here")

如果您有更好的选择,我可以更改我的图书馆。提前致谢。

标签: djangopython-3.xfirebase

解决方案


根据Pyrebase 源代码中的这一行,正在返回一个安全令牌。

data = json.dumps({"email": email, "password": password, "returnSecureToken": True})

大多数令牌是无状态的,这意味着服务器不存储任何类型的会话。当 Pyrebase 向服务器发出请求时,它会在每个请求的标头中发送令牌。只要该令牌存在、有效且未过期,服务器就会认为它已通过身份验证。

此令牌与其他一些用户数据一起存储在auth.current_user. 要注销用户,只需将 设置current_userNone

auth.current_user = None

没有令牌,请求将不再被验证。


推荐阅读