[Fixed] React native login function not responding

Issue

I have created a web system that all the users of the system can log in and work. But in my similar purpose react native app should allow only for customers to log in. In another way I need to only log in for userrole == customer only. So I have implemented the following function. It works without the if condition for all users in the database. But I need to avoid other logins. So I implement the following way. But it always displays ‘cannot login’. What is the error in the function.

const SigninPage = ({navigation}) => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const {colors} = useTheme();

  const signIn = () => {
    login(email, password)
      .then((res) => {
        if (
          res.data.role === 'admin' ||
          'account-coordinator' ||
          'ceo' ||
          'project-manager' ||
          'developer'
        ) {
          throw new Error('Cannot login');
        } else {
        AsyncStorage.setItem('userData', JSON.stringify(res.data.dbResult[0]));
        AsyncStorage.setItem('userRole', res.data.role);
        AsyncStorage.setItem('userToken', res.data.token);
        console.log('successfully logged!');
        navigation.navigate('DashboardDrawer');
        }
      })
      .catch((err) => {
        console.log(err);
      });
  };

Solution

Your if statement is wrong:

if (
   res.data.role === 'admin' ||
   res.data.role === 'account-coordinator' ||
   res.data.role === 'ceo' ||
   res.data.role === 'project-manager' ||
   res.data.role === 'developer'
) {
   // ...
}

Or:

if(res.data.role !== "customer") {
   throw new Error('Cannot login');
}

Leave a Reply

(*) Required, Your email will not be published