WoolDoughnut310 / micropython-firebase-auth

Firebase Auth implementation for Micropython

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

micropython-firebase-auth

Firebase implementation based on REST API, based on micropython-firebase-realtime-database from ckoever.

Installation

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 :

Commands that are implemented

- sign_in
- sign_out
- sign_up

Required modules

ujson, urequests, time, sys

Connect to Wifi

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()

Create a FirebaseAuth instance

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

Functions

sign_in

auth.sign_in(email, password)

Authenticate a user with email and password

sign_up

auth.sign_up(email=None, password=None)

Registers an account with the given email and password

  • leave email and password empty to sign in anonymously

    auth.sign_up(None, None) # Sign in as guest

    Example:

auth.sign_up("email", "password")
print("Hello, " + auth.user.display_name)

sign_out

auth.sign_out()

Clears authentication session and stored user data

Attributes

user

auth.user: dict()

Returns the user data for the currently authenticated user

  • Properties

    uid
    email
    display_name - Optional
    photo_url - Optional

session

auth.session: AuthSession

The AuthSession object used to handle requests to the backend

AuthSession

request

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)

access_token

session.access_token

Gets the access token for use within other Firebase services such as RTDB, Firestore, etc.

AuthSession.load_credentials

AuthSession.load_credentials()

Reads a stored version of session credentials from a credentials.json file

AuthSession.save_credentials

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)

About

Firebase Auth implementation for Micropython

License:MIT License


Languages

Language:Python 100.0%