How to retreive data from backend to frontend in node application

I’m quite new to node applications, but I have made a frontend in react, and a backend using node and sql. I have a UserLogin component in the frontend, which makes the call after getting user data:

  submitLoginForm(e) {
    e.preventDefault();

    const { email, password } = this.state;
    const { login } = this.props;

    const validEmail = validateEmail(email);
    login(email, password); //login function in frontend
  }

The login function is as follows:

export function login(email, password) {
    return async (dispatch) => {
        try {
            const resp = await callLoginApi(email, password);
            console.log("Response is", resp);
        } catch (err) {
            console.log("error is", err);
        }
    };
}

function callLoginApi(email, password) {
    return axios.post(API.auth.login, {
        email,
        password
    });
}

In the backend, this auth.controller makes the login call:

module.exports.login = utils.asyncRequest((req) => {
    const { body } = req;
    return authService.generateToken(dbService.User, body.email, body.password);
});

and this is what generateToken in authService looks like :

module.exports.generateToken = async (Model = dbService.User, email, password, ssoData) => {

user = await dbService.querySingle(Model, {
    [dbFields.email]: email
})
    try {
        await dbService.update(Model, user.id, {
            [dbFields.passwordAttempts]: user[dbFields.passwordAttempts] + 1
        });
        console.log("you're blocked, ", user[dbFields.passwordAttempts] )
    } catch (err) {
        logger.error(err);
    }
    throw new error.AccessDenied('Invalid login credentials');
}

My goal is to return the value of user[dbFields.passwordAttempts] to my frontend submitLoginForm function after it makes the login call.
I’m able to console log the value in the backend, but is it possible to return this value to the frontend?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.