Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when calling credentials::username_password #112

Closed
adamski opened this issue Nov 6, 2023 · 8 comments
Closed

Exception when calling credentials::username_password #112

adamski opened this issue Nov 6, 2023 · 8 comments

Comments

@adamski
Copy link

adamski commented Nov 6, 2023

I am trying out username/password authentication.

I am getting the following exception (not caught by the try/catch block):

ERROR: App: log_in_with_credentials failed: 401 message: http error code considered fatal: {"error":"invalid username/password","error_code":"InvalidPassword","link":"https://realm.mongodb.com/groups/6545ff69c5df044009a5bfd9/apps/654601d24b6daef7f4f26da9/logs?co_id=6548c5bf2e4e293eaac6f424"}. Client Error: 401
libc++abi: terminating due to uncaught exception of type realm::app_error

Here is my code:

    auto app = realm::App (realmConfig);
    realm::user user;
    try
    {
        std::future_status status;
        auto userFuture = app.login (realm::App::credentials::username_password (
            "[email protected]", "password1"));
        do
        {
            switch (status = userFuture.wait_for (50ms); status)
            {
                case std::future_status::deferred:
                    std::cout << "deferred\n";
                    break;
                case std::future_status::timeout:
                    std::cout << "timeout\n";
                    break;
                case std::future_status::ready:
                    std::cout << "ready!\n";
                    break;
            }
        } while (status != std::future_status::ready);

        user = userFuture.get();
        std::cout << "result is " << user.identifier() << '\n';
    }
    catch (std::exception& e)
    {
        std::cout << "Error logging in: " << e.what() << std::endl;
    }

I'm not sure what's going wrong.

@leemaguire
Copy link
Contributor

Hi @adamski have you created this user from the Device Sync UI or registered this user before the login call?

@adamski
Copy link
Author

adamski commented Nov 6, 2023

@leemaguire Your question helped me to figure out part of what was going wrong. I wanted to call a custom authentication function, and was calling the wrong method.

Now I am calling the correct function, and getting the expected response, however on failure (if the credentials are not found) the exception seems to be "considered fatal" and is not able to be caught:

ERROR: App: log_in_with_credentials failed: 401 message: http error code considered fatal: {"error":"error executing auth function: {\"message\":\"Authentication failed with reason: Invalid email or password\",\"name\":\"Error\"}","error_code":"AuthError","link":"https://realm.mongodb.com/groups/6545ff69c5df044009a5bfd9/apps/654601d24b6daef7f4f26da9/logs?co_id=65490b3d41f94827ca89e962"}. Client Error: 401
libc++abi: terminating due to uncaught exception of type realm::app_error

(Here the string "Authentication failed with reason: Invalid email or password" comes from the custom function)

Note: it works perfectly when supplied with the correct credentials.

This is the updated code section:

   auto app = realm::App (realmConfig);
    realm::user user;
    try
    {
        std::future_status status;
        realm::bson::BsonDocument credentials = {
            {"username", "[email protected]"},
            {"password", "password1"}};

        auto userFuture = app.login (realm::App::credentials::function(credentials));
        do
        {
            switch (status = userFuture.wait_for (50ms); status)
            {
                case std::future_status::deferred:
                    std::cout << "deferred\n";
                    break;
                case std::future_status::timeout:
                    std::cout << "timeout\n";
                    break;
                case std::future_status::ready:
                    std::cout << "ready!\n";
                    break;
            }
        } while (status != std::future_status::ready);

        user = userFuture.get();
        std::cout << "result is " << user.identifier() << '\n';
    }
    catch (std::exception& e)
    {
        std::cout << "Error logging in: " << e.what() << std::endl;
    }

@adamski
Copy link
Author

adamski commented Nov 7, 2023

Is it because it's an async function that I'm not able to catch the exception? I'm not familiar with futures and exceptions

@leemaguire
Copy link
Contributor

@adamski what version are you using? I can capture the error in the catch block using your code.

@adamski
Copy link
Author

adamski commented Nov 8, 2023

I found the cause of the issue: The exception type needs to be realm::app_error.

catch (const realm::app_error& e)

@adamski adamski closed this as completed Nov 8, 2023
@adamski
Copy link
Author

adamski commented Nov 8, 2023

@adamski what version are you using? I can capture the error in the catch block using your code.

0.4.0

@jsflax
Copy link
Collaborator

jsflax commented Nov 8, 2023

@adamski we still have to normalize our exceptions. In the meantime I would recommend catching with the (...) syntax and reading from the last exception ptr from the std library.

@vtharmalingam
Copy link

vtharmalingam commented Nov 30, 2023

Hi Team,

I used exactly the same code above and in my case, I get the below error irrespective of what kind of authentication method I use, such as anonymous, username/password, api key, etc.

`
catching "realm::app_error& e", and printing e.message() reads like this:

Error logging in: non-zero custom status code considered fatal
`

I think I spent lots of time going back and forth to sync settings, and nothing worked. Any help on this will be greatly appreciated.

Thanks,
Tharma

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants