|
| 1 | +import firebase_admin # pip install firebase-admin |
| 2 | +from firebase_admin import credentials, db |
| 3 | +import pyrebase # pip install pyrebase4 |
| 4 | + |
| 5 | + |
| 6 | +class Firebase: |
| 7 | + def __init__(self): |
| 8 | + ##################################################################### |
| 9 | + # Use your own information for your Firebase project below. # |
| 10 | + ##################################################################### |
| 11 | + # You need to download the credentials file from your Firebase # |
| 12 | + # project settings. Click on the "Generate new private key" button # |
| 13 | + # and save the file as "credentials.json" in the same directory. # |
| 14 | + CREDENTIALS_FILE = "credentials.json" |
| 15 | + # You can find your database URL in your Firebase project settings. # |
| 16 | + # The format is "https://<project_id>.firebasedatabase.app/". # |
| 17 | + DATABASE_URL = "" |
| 18 | + # You can find your API key in your Firebase project settings. # |
| 19 | + # The format is "AIza...". # |
| 20 | + # If you can't find your API key, create a new web app in your # |
| 21 | + # Firebase project, then you will see the API key. # |
| 22 | + API_KEY = "" |
| 23 | + # You can find your project ID in your Firebase project settings. # |
| 24 | + PROJECT_ID = "" |
| 25 | + ##################################################################### |
| 26 | + # You can find these information in your Firebase project settings. # |
| 27 | + # You don't need to change the following code. # |
| 28 | + ##################################################################### |
| 29 | + self.cred = credentials.Certificate(CREDENTIALS_FILE) |
| 30 | + firebase_admin.initialize_app( |
| 31 | + self.cred, |
| 32 | + { |
| 33 | + "databaseURL": DATABASE_URL, |
| 34 | + }, |
| 35 | + ) |
| 36 | + self.ref = db.reference("/") |
| 37 | + self.config = { |
| 38 | + "apiKey": API_KEY, |
| 39 | + "authDomain": f"{PROJECT_ID}.firebaseapp.com", |
| 40 | + "databaseURL": DATABASE_URL, |
| 41 | + "storageBucket": f"{PROJECT_ID}.appspot.com", |
| 42 | + } |
| 43 | + self.firebase = pyrebase.initialize_app(self.config) |
| 44 | + self.auth = self.firebase.auth() |
| 45 | + |
| 46 | + def login(self, email, password): |
| 47 | + user = self.auth.sign_in_with_email_and_password(email, password) |
| 48 | + return user |
| 49 | + |
| 50 | + def register(self, email, password): |
| 51 | + user = self.auth.create_user_with_email_and_password(email, password) |
| 52 | + return user |
| 53 | + |
| 54 | + def get_current_user(self): |
| 55 | + return self.auth.current_user |
| 56 | + |
| 57 | + def get_user_id(self): |
| 58 | + return self.auth.current_user["localId"] |
| 59 | + |
| 60 | + def get_user_email(self): |
| 61 | + return self.auth.current_user["email"] |
| 62 | + |
| 63 | + def logout(self): |
| 64 | + self.auth.current_user = None |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + f = Firebase() |
| 69 | + print(f.ref.get()) |
| 70 | + f.ref.set({"name": "Bora Canbula"}) |
| 71 | + print(f.ref.get()) |
| 72 | + try: |
| 73 | + f.login("prof@university.edu", "password") |
| 74 | + except Exception as e: |
| 75 | + f.register("prof@university.edu", "password") |
| 76 | + print(f.get_current_user()) |
0 commit comments