Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
def authenticate(username, password):
    """
    Authenticate user
    
    Parameters:
    ----------
        username: str
            Name of the user
        password: str
            Password of the user
    """ 
    
    # Firebase instellingen voor authenticatie
    firebase_url = "https://identitytoolkit.googleapis.com/v1"
    firebase_key = "AIzaSyCjGibOfg9LKxoFhKAIzaSyBhun_JSiE1_903H6M4vrh3UkAbQz48VXRFq0eKrErI4UT3ES0"
    
    # Authentiseer gebruiker 
    response_authenticate = requests.post(f"{firebase_url}/accounts:signInWithPassword?key={firebase_key}", data={'email': username, 'password': password, 'returnSecureToken': 'true'})
    
    # Controle reactie firebase
    my_headers = {}
    if response_authenticate.status_code == 200:
        # Succesvol: sla header op
        id_token = response_authenticate.json()['idToken']
        my_headers = {'Authorization': f"Bearer {id_token}"}      
    
    # Niet succescol print foutmeldingen
    elif response_authenticate.status_code == 400:
        message = json.loads(response_authenticate.text)["error"]["errors"][0]["message"]
        if message == "EMAIL_NOT_FOUND":
            print(f"Gebruikersnaam is onjuist!")        
        elif message == "INVALID_PASSWORD":
            print(f"Wachtwoord is onjuist!") 
        else:
            print(f"Bad request, controleer API verzoek!")
    elif response_authenticate.status_code == 401:
        print(f"Gebruikersnaam en/of wachtwoord zijn onjuist!")
    elif response_authenticate.status_code == 405:
        print(f"API is niet beschikbaar!")
    elif response_authenticate.status_code == 500:
        print(f"Er is een interne fout opgetreden!")
    else:
        print(f"Er is een ongedefinieerde fout opgetreden!")
              
    return my_headers

...