Firebase implementation based on REST API, based on micropython-firebase-realtime-database from ckoever.
You can use uPip to install library from PyPi
import upip
upip.install("micropython-firebase-auth")
or you can just upload firebase_auth/firebase_auth.py
to your microcontroller:
python pyboard.py -d PORT -f cp firebase_auth.py :
- sign_in
- sign_out
- sign_up
ujson, urequests, time, sys
import time
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
wlan.connect("ssid", "pass")
print("Waiting for Wi-Fi connection", end="...")
while not wlan.isconnected():
print(".", end="")
time.sleep(1)
print()
from firebase_auth import FirebaseAuth
auth = FirebaseAuth("API_KEY")
The API key is required, read about it here
or you can find it at Project Settings > General
in your project's console
auth.sign_in(email, password)
Authenticate a user with email and password
auth.sign_up(email=None, password=None)
Registers an account with the given email and password
-
leave
email
andpassword
empty to sign in anonymouslyauth.sign_up(None, None) # Sign in as guest
Example:
auth.sign_up("email", "password")
print("Hello, " + auth.user.display_name)
auth.sign_out()
Clears authentication session and stored user data
auth.user: dict()
Returns the user data for the currently authenticated user
-
uid
email
display_name
- Optional
photo_url
- Optional
auth.session: AuthSession
The AuthSession
object used to handle requests to the backend
session.request(method, endpoint, data=None, method=None, **kwargs)
Make a request to an auth endpoint requiring an idToken (the accessToken) (if method is not None, else an endpoint that returns tokens)
session.access_token
Gets the access token for use within other Firebase services such as RTDB, Firestore, etc.
AuthSession.load_credentials()
Reads a stored version of session credentials from a credentials.json
file
AuthSession.save_credentials(creds)
Stores a session credentials object into credentials.json
file
Example:
# Storing data on system before going to sleep for 10s
import machine
creds = auth.session.credentials
AuthSession.save_credentials(creds)
machine.deepsleep(10000)