skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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:

    import pyrebase
    from kivy.app import App
    from kivy.uix.label import Label
    
    firebase_config = {
        "apiKey": "YOUR_API_KEY",
        "authDomain": "YOUR_AUTH_DOMAIN",
        "projectId": "YOUR_PROJECT_ID",
        "storageBucket": "YOUR_STORAGE_BUCKET",
        "messagingSenderId": "YOUR_MESSAGING_SENDER_ID",
        "appId": "YOUR_APP_ID",
        "databaseURL": "YOUR_DATABASE_URL"
    }
    
    firebase = pyrebase.initialize_app(firebase_config)
    auth = firebase.auth()
    
    class MyApp(App):
        def build(self):
            self.label = Label(text="Not logged in")
            auth.add_auth_state_listener(self.auth_state_listener)
            return self.label
    
        def auth_state_listener(self, auth, user):
            if user:
                self.label.text = f"Logged in as {user['email']}"
            else:
                self.label.text = "Not logged in"
    
        def on_stop(self):
            auth.remove_auth_state_listener(self.auth_state_listener)
    
        def sign_up(self, login, password):
            auth.create_user_with_email_and_password(login, password)
    
        def sign_in(self, login, password):
            auth.sign_in_with_email_and_password(login, password)
    
    if __name__ == "__main__":
        MyApp().run()
    

    I hope your problem is solved

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search