Ok, before someone comes saying this is a duplicated question from here, I must say it’s not since I’m using a different library and that question has no answers at all. There are also many answers to this problem, but they are all specific for Flask, while I’m doing my app with Kivy.
The library I’m using is Pyrebase, and here it is its Github.
So, my question is: how can I keep a user logged in, even if the app was already closed and restarted, using Pyrebase without using Flask? Sure I could just save the email and password on a file in the device, but this is not safe at all.
Here’s the code I’m using to sign up and sign in:
import pyrebase
firebase = pyrebase.initialize_app({
"apiKey": "AIzaSyDfuewRkxp1q6UA-i119qAIa7OWMjonJuc",
"authDomain": "grade-blue-05.firebaseapp.com",
"projectId": "grade-blue-05",
"storageBucket": "grade-blue-05.appspot.com",
"messagingSenderId": "314137768223",
"appId": "1:314137768223:web:ded273736f5318c3f7002a",
"databaseURL": "https://grade-blue-05-default-rtdb.firebaseio.com/"
})
auth = firebase.auth()
class email:
def sign_up(login, password):
userauth = auth.create_user_with_email_and_password(login, password)
print('Ok')
return userauth
def sign_in(login, password):
userauth = auth.sign_in_with_email_and_password(login, password)
print('Ok')
return userauth
2
Answers
The Firebase front-end SDKs store the JWT/ID token that they get back from the sign-in method in platform-dependent local storage, and then restore it from there when the user restarts the app/reloads the page.
If you want your users to have the same experience, you will have to implement that persistence layer yourself. The Firebase SDK for front-end JavaScript has a documented persistence layer for that, but I don’t think Pyrebase has that – so you’ll have to figure it out on your own.
To maintain user authentication across sessions in a Kivy app using Pyrebase without resorting to storing sensitive information like passwords locally, you can use Firebase’s built-in functionality for session persistence. This allows users to remain authenticated even if they close and reopen the app. Here’s how you can do it:
Enable Session Persistence in Firebase Console: Ensure that session persistence is enabled in your Firebase project. This setting maintains the user’s authentication state across app restarts.
Update Firebase Configuration: Make sure your Firebase configuration includes the necessary settings for session persistence. Although Pyrebase doesn’t have built-in support for session persistence, Firebase settings should take care of it.
Use Firebase Auth State Listener: Implement an authentication state listener in your Kivy app. This listener triggers whenever the user’s authentication state changes, helping you manage UI transitions based on whether the user is authenticated or not.
Here’s how you can update your code to incorporate these steps:
I hope your problem is solved