AM creating an offline react native app where users can register and login(Users info are saved in async storage).
I was able to create the Registration component successfully(Users info are being saved inside the asyncstorage) but am unable to retrieve it for loggin users in.
I will only include the necessary code here.
import React, { Component } from 'react';
import { Text } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
export default class StudentLogin extends Component {
constructor() {
super();
this.state = {
username: '',
password: '',
};
}
getStudent = async () => {
const student = {
'password': this.state.password,
'username': this.state.username,
};
//function to get the value from AsyncStorage
const existingStudent = await AsyncStorage.getItem('student');
let studentLogin= JSON.parse(existingStudent);
if( studentLogin ){
studentLogin = []
}else{
alert('Student info not found in the database.');
}
studentLogin.push( student );
let students = studentLogin.filter(s => s.username === this.state.username)[0];// here I want to include the password as well
if(students){
this.props.navigation.navigate('NewAttendance', {data: students});
}else{
alert('Email/password combination not correct');
}
};
render() {
return (
<Container style={styles.contentStyle}>
<Item inlineLabel style={styles.itemStyle}>
<MaterialCommunityIcons
name="Username"
style={styles.iconStyle}
/>
<Label style={styles.labelStyle}>Username</Label>
<Input
style={styles.inputStyle}
autoCorrect={false}
placeholder="Username"
icon={<Icon name="user" />}
onChangeText={data => this.setState({ username: data })}
label='Username'
/>
</Item>
<Item inlineLabel style={styles.itemStyle}>
<MaterialCommunityIcons
name="lock"
style={styles.iconStyle}
/>
<Label style={styles.labelStyle}>Password</Label>
<Input
style={styles.inputStyle}
placeholder="Password"
secureTextEntry
onChangeText={data => this.setState({ password: data })}
label='Password'
/>
</Item>
<Button full onPress={this.getStudent} style={styles.buttonStyle}>
<Text style={styles.buttonTextStyle}>Login</Text>
</Button>
</Container>
);
}
}
Where am I going to run in this code?