0

I am trying to integrate a login solution to my vue.app utilising the JWT Authentication plugin.

I have a test solution working, but in my main branch, the eslint version must be newer as I am receiving "Promise executor functions should not be async no-async-promise-executor".

My code within my 'store' adn under my actions is:

login({ commit }, credentials){
  return new Promise(async (resolve, reject) => {
    try {
      const { data }  = await axios.post(`https://mywebsite.com/wp-json/jwt-auth/v1/token`, credentials)
      commit('SET_USER', data)
      resolve(data)
    }catch(e){
      reject(e)
    }
  })
},
validate({ state }) {
  return axios({
    url: `https://mywebsite.com/wp-json/jwt-auth/v1/token/validate`, 
    method: 'post',
    headers: {
      'Authorization': `Bearer ${state.user.token}`
    }
  })
},

I'm just unsure how to rewrite this to remove the error ?

Nikola Pavicevic
  • 21,952
  • 9
  • 25
  • 46
Andrew
  • 1
  • Change `async (resolve, reject)` to `(resolve, reject)`. Reference: https://eslint.org/docs/rules/no-async-promise-executor – ikhvjs Dec 20 '21 at 14:50
  • Thanks @ikhvjs, I had tried removing this previously, but when I had made the change I think go the error "Unexpected reserved word 'await' " from my line: const { data } = **await** axios.post(`https://mywebsite.com/wp-json/jwt-auth/v1/token`, credentials) – Andrew Dec 21 '21 at 10:07

1 Answers1

1

You seem to have some misunderstanding of async/await. If you want to async/await, you don't need to wrap it in a Promise object. Instead. use async keyword in the method and apply await inside.

var vm = new Vue({
    methods: {
        async login({ commit }, credentials) {
            try {
                const { data } = await axios.post(
                    `https://mywebsite.com/wp-json/jwt-auth/v1/token`,
                    credentials
                );
                commit("SET_USER", data);
            } catch (err) {
                // eslint-disable-next-line
                console.log(err);
            }
        },
        validate({ state }) {
            return axios({
                url: `https://mywebsite.com/wp-json/jwt-auth/v1/token/validate`,
                method: "post",
                headers: {
                    Authorization: `Bearer ${state.user.token}`,
                },
            });
        },
    },
});
ikhvjs
  • 5,316
  • 2
  • 13
  • 36