Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- this.state = {
- validating: false
- }
- render() {
- return (
- <Container>
- <Content>
- <Form>
- <Item floatingLabel>
- <Label>Email</Label>
- <Input onChangeText={(text) => this.setState({email:text})} />
- </Item>
- <Item floatingLabel last>
- <Label>Password</Label>
- <Input secureTextEntry onChangeText={(text) => this.setState({password:text})} />
- </Item>
- <Button block success style={{ marginTop: 50 }} onPress={() => {
- if( this.state.email && this.state.password ){
- this.validate();
- }
- }} >
- <Text>Authenticate</Text>
- </Button>
- </Form>
- </Content>
- </Container>
- )
- }
- <?php
- require_once('wp-load.php');
- $response = array(
- 'data' => array(),
- 'msg' => 'Invalid email or password',
- 'status' => false
- );
- /* Sanitize all received posts */
- foreach($_POST as $k => $value){
- $_POST[$k] = sanitize_text_field($value);
- }
- /**
- * Login Method
- *
- */
- if( isset( $_POST['type'] ) && $_POST['type'] == 'login' ){
- /* Get user data */
- $user = get_user_by( 'email', $_POST['email'] );
- if ( $user ){
- $password_check = wp_check_password( $_POST['password'], $user->user_pass, $user->ID );
- if ( $password_check ){
- /* Generate a unique auth token */
- $token = MY_RANDOM_CODE_GENERATOR( 30 );
- /* Store / Update auth token in the database */
- if( update_user_meta( $user->ID, 'auth_token', $token ) ){
- /* Return generated token and user ID*/
- $response['status'] = true;
- $response['data'] = array(
- 'auth_token' => $token,
- 'user_id' => $user->ID,
- 'user_login' => $user->user_login
- );
- $response['msg'] = 'Successfully Authenticated';
- }
- }
- }
- }
- validate(){
- this.setState({ validating: true });
- let formData = new FormData();
- formData.append('type', 'login');
- formData.append('email', this.state.email);
- formData.append('password', this.state.password);
- return fetch('http://example.com/authentication.php', {
- method: 'POST',
- body: formData
- })
- .then((response) => response.json())
- .then((responseJson) => {
- let data = responseJson.data;
- if (this.saveToStorage(data)){
- this.setState({
- validating: false
- });
- /* Redirect to accounts page */
- Actions.pageAccount();
- } else {
- console.log('Failed to store auth');
- }
- })
- .catch((error) => {
- console.error(error);
- });
- }
- async saveToStorage(userData){
- if (userData) {
- await AsyncStorage.setItem('user', JSON.stringify({
- isLoggedIn: true,
- authToken: userData.auth_token,
- id: userData.user_id,
- name: userData.user_login
- })
- );
- return true;
- }
- return false;
- }
- async logout(){
- await AsyncStorage.removeItem('user');
- // Add a method that will delete user_meta token of the user from the server.
- // await deleteUserMetaToken(PARAM_USER_ID);
- /* Redirect to the login page */
- Actions.pageLogin();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement