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