1

The problem is that I am getting "undefined" when I use error.code in JavaScript when I want error.code to spit out meaningful Supabase Auth error codes.

I tried error.message, error.name, and error.status and they all gave me meaningful results. What's notable is that error.code exists although it just returns "undefined".

Here is some code and output to further diagnose the problem and see all the available properties of the error object:

code:

async function signIn() {
    const email = document.querySelector('.sign-in-popup-sign-in-email-input').value;
    const password = document.querySelector('.sign-in-popup-sign-in-password-input').value;
    const { user, session, error } = await supabaseClient.auth.signInWithPassword({
        email: email,
        password: password
    });

    if (error && error.message === 'Invalid login credentials') {
        console.error(error);

        // Get all property names of the error object
        let properties = Object.getOwnPropertyNames(error);

        // Create an array to store property-value pairs
        let propertyStrings = [];

        // Iterate over each property and build the output string
        properties.forEach(function (prop) {
            console.log(prop, error[prop]);  // Log each property and its value
            propertyStrings.push(prop + ': ' + error[prop]);
        });

        // Join the property strings with a newline for readability
        let alertMessage = propertyStrings.join('\n');

        // Alert all properties
        alert(alertMessage);

        async function signUp() {
            const { user, error } = await supabaseClient.auth.signUp({
                email: email,
                password: password
            });
        }

        signUp();
    }
}

output:

message: Invalid login credentials
__isAuthError: true
name: AuthApiError
status: 400
code: undefined
line: 7
column: 4034
sourceURL: https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2
stack: w@https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2:7:4034
T@https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2:7:4221
@https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2:7:6534

Some code in console log after I click sign in with no email or password entered:

[Error] Failed to load resource: the server responded with a status of 400 () (token, line 0)
[Error] AuthApiError: Invalid login credentials

    (anonymous function) (index.html:68)
3
  • 4
    It looks like error.code is coming back from the call as undefined for a 401 response Nothing you can do. Try console logging error, nor error.message to reveal this. Commented Jul 17, 2024 at 7:13
  • I don't understand what you mean by "Try console logging error, nor error.message to reveal this." Should I do something more? Commented Jul 17, 2024 at 7:21
  • 3
    Change console.log(error.message) in your code to console.log(error) and update the question with what gets logged in the browser console. Commented Jul 17, 2024 at 7:28

1 Answer 1

1

This is a known issue with the Supabase auth error codes. It is being tracked on the Supabase Auth GitHub repo https://github.com/supabase/auth/issues/1631

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.