Advertisement
Guest User

Untitled

a guest
Jul 14th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. this.state = {
  2. validating: false
  3. }
  4.  
  5. render() {
  6. return (
  7. <Container>
  8. <Content>
  9. <Form>
  10. <Item floatingLabel>
  11. <Label>Email</Label>
  12. <Input onChangeText={(text) => this.setState({email:text})} />
  13. </Item>
  14. <Item floatingLabel last>
  15. <Label>Password</Label>
  16. <Input secureTextEntry onChangeText={(text) => this.setState({password:text})} />
  17. </Item>
  18. <Button block success style={{ marginTop: 50 }} onPress={() => {
  19. if( this.state.email && this.state.password ){
  20. this.validate();
  21.  
  22. }
  23. }} >
  24. <Text>Authenticate</Text>
  25. </Button>
  26. </Form>
  27. </Content>
  28. </Container>
  29. )
  30. }
  31.  
  32. <?php
  33.  
  34. require_once('wp-load.php');
  35.  
  36. $response = array(
  37. 'data' => array(),
  38. 'msg' => 'Invalid email or password',
  39. 'status' => false
  40. );
  41.  
  42. /* Sanitize all received posts */
  43. foreach($_POST as $k => $value){
  44. $_POST[$k] = sanitize_text_field($value);
  45. }
  46.  
  47. /**
  48. * Login Method
  49. *
  50. */
  51. if( isset( $_POST['type'] ) && $_POST['type'] == 'login' ){
  52.  
  53. /* Get user data */
  54. $user = get_user_by( 'email', $_POST['email'] );
  55.  
  56. if ( $user ){
  57. $password_check = wp_check_password( $_POST['password'], $user->user_pass, $user->ID );
  58.  
  59. if ( $password_check ){
  60. /* Generate a unique auth token */
  61. $token = MY_RANDOM_CODE_GENERATOR( 30 );
  62.  
  63. /* Store / Update auth token in the database */
  64. if( update_user_meta( $user->ID, 'auth_token', $token ) ){
  65.  
  66. /* Return generated token and user ID*/
  67. $response['status'] = true;
  68. $response['data'] = array(
  69. 'auth_token' => $token,
  70. 'user_id' => $user->ID,
  71. 'user_login' => $user->user_login
  72. );
  73. $response['msg'] = 'Successfully Authenticated';
  74. }
  75. }
  76. }
  77. }
  78.  
  79. validate(){
  80. this.setState({ validating: true });
  81.  
  82. let formData = new FormData();
  83. formData.append('type', 'login');
  84. formData.append('email', this.state.email);
  85. formData.append('password', this.state.password);
  86.  
  87. return fetch('http://example.com/authentication.php', {
  88. method: 'POST',
  89. body: formData
  90. })
  91. .then((response) => response.json())
  92. .then((responseJson) => {
  93. let data = responseJson.data;
  94.  
  95. if (this.saveToStorage(data)){
  96. this.setState({
  97. validating: false
  98. });
  99.  
  100. /* Redirect to accounts page */
  101. Actions.pageAccount();
  102. } else {
  103. console.log('Failed to store auth');
  104. }
  105. })
  106. .catch((error) => {
  107. console.error(error);
  108. });
  109. }
  110.  
  111. async saveToStorage(userData){
  112. if (userData) {
  113. await AsyncStorage.setItem('user', JSON.stringify({
  114. isLoggedIn: true,
  115. authToken: userData.auth_token,
  116. id: userData.user_id,
  117. name: userData.user_login
  118. })
  119. );
  120. return true;
  121. }
  122.  
  123. return false;
  124. }
  125.  
  126. async logout(){
  127. await AsyncStorage.removeItem('user');
  128.  
  129. // Add a method that will delete user_meta token of the user from the server.
  130. // await deleteUserMetaToken(PARAM_USER_ID);
  131.  
  132. /* Redirect to the login page */
  133. Actions.pageLogin();
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement