问题
I am trying to create user authentication from back-end with python and firebase. So far creating a user and login actions worked perfectly. But for somehow i cannot find any information for signOut action. i'm using Pyrebase library. Any one has a suggestion? Here is the part it works:
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")
I can change my library if you have a better options. Thanks in advance.
回答1:
According to this line in the Pyrebase source, a secure token is being returned.
data = json.dumps({"email": email, "password": password, "returnSecureToken": True})
Most tokens are stateless, meaning the server does not store any sort of session. When Pyrebase is making requests to the server, it's sending the token along in a header with each request. As long as that token is present, valid and hasn't expired, the server will consider it authenticated.
This token, along with some other user data, is stored in auth.current_user
. To log a user out, simply set the current_user
to None
.
auth.current_user = None
Without the token, the requests will no longer be authenticated.
来源:https://stackoverflow.com/questions/54262081/how-to-signout-from-firebase-using-django-python