首页 > 解决方案 > authProvider is finishing its Promise after the Dashboard being mounted

问题描述

标签: reactjsadmin-on-restreact-admin

解决方案


Try using async/await, it's bit easier to follow the flow:

export default async (type, userID) => {
  if (type === AUTH_LOGIN) {
    try  {
      const res =  await fetch('https://localhost:8080/users/auth', {
        method: 'POST',
        body: JSON.stringify({ userID })
        headers: new Headers({ 'Content-Type': 'application/json' }),
      }) // fetch API data

      const { user: { token, name, email, activePage } } = await res.json(); // convert to JSON, from the JSON's user prop, pull out token, name, email, activePage

      localStorage.setItem('token', token);
      localStorage.setItem('name', name);
      localStorage.setItem('email', email);
      localStorage.setItem('activePage', activePage);

    } catch(err) { console.error(err); }
  }
}

推荐阅读