andpor / react-native-sqlite-storage

Full featured SQLite3 Native Plugin for React Native (Android and iOS)
MIT License
2.75k stars 521 forks source link

Stranger behavior during user login #542

Open emekaokoli opened 1 year ago

emekaokoli commented 1 year ago

Having an issue with login, Can anyone help with this, please?

I am building a react native app where I am creating a user account and authenticating the user via registration if the user does not exist and logs in after successfully creating a user account, so far I have a user registration screen and a login screen. I am able to create a new user but upon login, the user does not exist.

Screenshot 2022-11-21 164424

StackOverflow entry

"react-native-sqlite-storage": "6.0.1",

INIT DB

useEffect(() => {
    db.transaction(
      txn => {
        txn.executeSql(
          'CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY AUTOINCREMENT, email VARCHAR(50), password VARCHAR(50))',
          [],
          function (tx, res) {
            console.log({res});
          },
        );
      },
      e => {
        console.log(e);
      },
      null,
    );
  }, []);

Register

import { openDatabase } from 'react-native-sqlite-storage';
import Button from '../../common/Button';
const db = openDatabase({name:'user.db'});

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

  const onRegister = useCallback(() => {
    db.transaction(
      (tx) => {
        tx.executeSql(
          'INSERT INTO users (email, password) VALUES (?,?)',
          [email, password],
          (tx, results) => {
            console.info({'Register Results': results});
            if (results.rowsAffected > 0) {
              Alert.alert(
                'Success',
                'You are Registered Successfully',
                [
                  {
                    text: 'Ok',
                    onPress: () => navigation.navigate('Login'),
                  },
                ],
                { cancelable: false }
              );
            } else alert('Registration Failed');
          }
        );
      },
      (e) => {
        console.log(e);
      },
      null
    );
  }, []);

LOGIN

import {openDatabase} from 'react-native-sqlite-storage';
import Button from './../../common/Button';
const db = openDatabase({name: 'user.db'});

const Login = ({navigation}) => {
  const dispatch = useDispatch();
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  const onLogin = () => {
    if (email === '' || password === '') {
      alert('Please enter your username and password!');
      return;
    }

    db.transaction(
      tx => {
        tx.executeSql(
          'SELECT * FROM users WHERE email= ?',
          [email],
          (tx, results) => {
            console.info({outside: results});

            if (results.rows.length === 0) {
              Alert.alert('User does not exist!');
            } else {
              console.info({results});
              const row = results.rows.item;
              if (password === row.password) {
                navigation.navigate('Home');
                dispatch(authed(results.rows));
                return;
              }
              Alert.alert('Authentication failed!');
            }
          },
        );
      },
      e => {
        Alert.alert(
          'Error',
          'Login failed ' + e.message,
          [
            {
              text: 'Ok',
            },
          ],
          {cancelable: false},
        );
      },
    );
  };
emekaokoli commented 1 year ago

Still need help with this

MacKenzieHnC commented 1 year ago

What platform are you running this on?

MacKenzieHnC commented 1 year ago

Also, should 'SELECT * FROM users WHERE email= ?' be `SELECT * FROM users WHERE email= ${email}`?