Failure and success message not working properly

I created a custom login lightbox that is connected to a database to record each time a user logs in. Everything seems to be working working fine. I added a failure and success message to the submit button, but the failure message always pops up even if the user enters the correct info. It still logs them in, but always gives them a failure message. The success message never appears. How do I fix this? My code for the page is below.

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;

$w.onReady( function () {
$w(‘#submit’).onClick( function () {
let email = $w(‘#email’).value;
let password = $w(‘#password’).value;
wixUsers.login(email, password)
.then(() => {

            wixLocation.to('https://www.therapaw.com/logged-in'); 
        }) 
}) 

})
{ let isUserLoggedIn = wixUsers.loggedIn; // true
//Add your code for this event here:
}

Well, you’ve got some conflicts…

The Submit button is linked to navigate to the LoggedIn page:

However, your code also redirects to the LoggedIn page if the user is logged in:

        wixUsers.login(email, password)
            .then(() => {
                wixLocation.to('https://www.therapaw.com/logged-in');
            })

OK, so I’m not really sure who wins that contest, but I would suggest not linking the button.

Also, you have some “extraneous” code appearing. Not sure if this causes any problems, but it is an error and can’t be good.

Another thing, @sapirh pointed out to me that Submit is only valid when you are submitting data to be saved to the collection. As the documentation states: “You can only add form submit messages if your connected dataset is set to either Write-only or Read & Write.”

Instead of Submit, you should not connect the button.

Rather, just use the onClick() event handler and handle the login, including redirection, and success/failure messages yourself. If you redirect to a LoggedIn page, then you don’t really need a success message. If login fails, you can use a popup box/Lightbox.

You might also just consider using the Wix Login Bar for convenience: