0

Hello I'm using a function very similar to this one, with little changes https://play.nativescript.org/?template=play-vue&id=HdDm9M&v=786

When I login to my rest api, if I put in the wrong username or password, it always returns true (by logging me in).

It should execute the catch block when my username or password is wrong but it does not do that. I had this issue before and fixed it but now I can't figure out why this happens.

In my Login.vue

     login() {
            this.$backend
                .login(this.user)
                .then(() => {

                    // if login success then save login and password in local storage
                    appSettings.setString("username", this.user.username);
                    appSettings.setString("password", this.user.password);


                    this.$store.commit('setActualPage', 'Main');
                    this.processing = false;
                    this.$navigateTo(Main, { clearHistory: true });

                })
                .catch(() => {
                    this.processing = false;
                    this.alert(
                    "Username or password are not correct."
                    );
                });
        },

and my backend-service.js

  login(user) {

    return httpModule.request({
        url: "MYRESTAPIURL",
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "username": user.username,
            "password": user.password,
            "apikey": "814973593645gg6db8ca6983789f"
        }
    }).then((response) => {
        let result = response.content.toJSON();


        return result;

        //console.log(result.premiumdays)
    }, (e) => {
        console.log(e);
    });
}

This return result; gives the output below if it's the wrong user or password:

{ error: 'Not connected.' }

Even if I put return; to return false it logs me in.

1 Answer 1

1

from your code : when your back-end service login failed,the result JSON is send back to your page(login.vue),so maybe you should add some code to verify the return JSON:

login.vue:


login() {
            this.$backend
                .login(this.user)
                .then((resp) => {

                   // verify the return data
                   if(resp && resp.error){
                       // login failed,do sth
                        this.processing = false;
                        this.alert(
                           "Username or password are not correct."
                        );
                   }else{
                        // login success
                   }

                })
                .catch(() => {
                    this.processing = false;
                    this.alert(
                    "Username or password are not correct."
                    );
                });
        },

the catch statement will not be executed unless the HTTP response code is not 200.so the login failed is not an error request,it is just an succeed request with the error data

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

2 Comments

lol u was right but it workes 2 times and now catch is fire up. I tried postman whats going on and if I send the same post request with valid usernam, password, apikey, it returns code 200 and "no body returned for response" . before it was returning data from mysql database for particular user. do you know what is happen?
your back-end service is an async call to httpModule.request,your return statement is in the then callback function,but your login function maybe return undefined before the then callback. so you should return a Promise in login function and resolve it in the then callback

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.