reactjssimple-oauth2

await is a reserved word error in ReactJS


I'm new to ReactJS and I have used simple-oauth to connect to a test API. I have added the client id, client secret, username and password as well as the oauth token url. I'm getting syntax error await is a reserved word (40:21)

Below is my current code which is a sample from simple-oauth:-

const credentials = {
  client: {
    id: "CLIENT_ID",
    secret: "CLIENT_SECRET"
  },
  auth: {
    tokenHost: "http://localhost/oauth/token"
  }
};

const oauth2 = require('simple-oauth2').create(credentials);

const tokenConfig = {
  username: "USERNAME",
  password: "PASSWORD",
  scope: '<scope>',
};

try {
  const result = await oauth2.ownerPassword.getToken(tokenConfig);
  const accessToken = oauth2.accessToken.create(result);
} catch (error) {
  console.log('Access Token Error', error.message);
}

I also tried async function. Though the error gone, the console log isn't being triggered. Here's the async function code:-

async () => {
  const result = oauth2.ownerPassword.getToken(tokenConfig);
  const accessToken = oauth2.accessToken.create(result);
  // no console.log in the debugger
  console.log(result);
};

What could be wrong in the code? please help.


Solution

  • I also tried async function. Though the error gone, the console log isn't being triggered.

    Because you didn't call your function. What you need is a Self Invoking Function:

    (async () => {
        const result = oauth2.ownerPassword.getToken(tokenConfig);
        const accessToken = oauth2.accessToken.create(result);
        // no console.log in the debugger
        console.log(result);
      })();