GodotNuts / GodotFirebase

Implementations of Firebase for Godot using GDScript
MIT License
532 stars 76 forks source link

[BUG] Error with Save Auth #261

Closed AdrianoAla closed 2 years ago

AdrianoAla commented 2 years ago

Describe the bug save_auth and check_auth dont work, I get the following error when I try to use check_auth image

To Reproduce

func _ready():
    Firebase.Auth.check_auth_file()
    Firebase.Auth.connect("login_succeeded", self, "_on_login_succeeded")
    Firebase.Auth.connect("login_failed",self, "_on_login_failed")
    Firebase.Auth.connect("userdata_received", self, "_on_userdata_recieved")

func _on_userdata_recieved(userdata):
    print(userdata)

func _on_login_failed(err, errdesc):
    print(err)
    print(errdesc)

func _on_login_succeeded(_auth : Dictionary) -> void:
    print("Successfully logged in with oAuth2 as: {email}".format({email=_auth.email}))
    Firebase.Auth.get_user_data()
    Firebase.Auth.save_auth(_auth)

func _on_SignIn_button_up():
    var email = get_node("../Email").get_text()
    var password = get_node("../Password").get_text()

    var auth = Firebase.Auth.login_with_email_and_password(email, password)

func _on_SignUp_button_up(user):
    var email = get_node("../Email").get_text()
    var password = get_node("../Password").get_text()

    Firebase.Auth.signup_with_email_and_password(email, password)
    Firebase.Auth.save_auth(user)   
    Firebase.Auth.send_account_verification_email()

func _on_Forgot_Password_button_up():
    var email = get_node("../Email").get_text()
    print ("Resetting Password")
    Firebase.Auth.send_password_reset_email(email)

Expected behavior the autherntication data to be stored and then to be loaded once the program restarts

Environment:

BearDooks commented 2 years ago

@arpidano

Just reading through this quick. If the error is on check auth there might an an issue with the saved auth file. Can you delete it and try saving it again?

The code looks correct from my point of view.

Thanks

Chuck

BearDooks commented 2 years ago

@arpidano

Can you please change your login succeed function to this and let me know if it makes any difference

func _on_FirebaseAuth_login_succeeded(_auth) -> void:
    _print_to_console("Login with email and password has worked")
    Firebase.Auth.save_auth(_auth)

I downloaded the latest version and this code has worked perfect for me, so I am not sure what is going on with your code. Worse comes to worse I may ask you to share you code with me privately so I can look through it, (But I would ask you to scrub all parts that tie into your Firebase so I don't have access.)

Thanks

Chuck

Pukkah commented 2 years ago

It looks like manual_token_refresh() function has unneeded _base_url in request url.

# Function is called when requesting a manual token refresh
func manual_token_refresh(auth_data):
    auth = auth_data
    var refresh_token = null
    auth = get_clean_keys(auth)
    if auth.has("refreshtoken"):
        refresh_token = auth.refreshtoken
    elif auth.has("refresh_token"):
        refresh_token = auth.refresh_token
    _needs_refresh = true
    _refresh_request_body.refresh_token = refresh_token
    request(_base_url + _refresh_request_url, _headers, true, HTTPClient.METHOD_POST, JSON.print(_refresh_request_body))

Modifying last line to request(_refresh_request_url, _headers, true, HTTPClient.METHOD_POST, JSON.print(_refresh_request_body)) fixes the response error and gets refreshed token. Still, it does not return login_succeeded signal. My workaround was to connect to token_refresh_succeeded to my _on_FirebaseAuth_login_succeeded function and than disconnect it, so it does not get called when token gets refreshed automaticly.

func _ready():
    Firebase.Auth.connect("login_succeeded", self, "_on_FirebaseAuth_login_succeeded")
    Firebase.Auth.connect("token_refresh_succeeded", self, "_on_FirebaseAuth_login_succeeded")
    # For Testing purpouses sign-in with test account
    Firebase.Auth.check_auth_file()
    if Firebase.Auth.auth.empty():
        Firebase.Auth.login_with_email_and_password("test@test.lv", "testtest")

func _on_FirebaseAuth_login_succeeded(auth):
    Firebase.Auth.disconnect("token_refresh_succeeded", self, "_on_FirebaseAuth_login_succeeded")
    Firebase.Auth.save_auth(auth)
WolfgangSenff commented 2 years ago

Good call out! Can you open a PR with these changes? We'll expedite it through, @pukkah.

BearDooks commented 2 years ago

@Pukkah thanks for finding that. I have merged it into the codebase