Skip to content

Commit

Permalink
Merge pull request #46 from Bloc/fix-api-auth
Browse files Browse the repository at this point in the history
Adds axios post and fixes the login error
  • Loading branch information
bdougie committed Apr 20, 2016
2 parents 359e62b + 59ef6c8 commit d831495
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions App/Components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default class Login extends Component {
} else {
this.handleError(res.message);
}

})
.catch((error) => {
this.handleError(error);
Expand All @@ -126,7 +126,7 @@ export default class Login extends Component {
error: error
});
}

render() {
const _handleSubmit = this.handleSubmit.bind(this);
const _handleEmailChange = this.handleEmailChange.bind(this);
Expand Down
29 changes: 17 additions & 12 deletions App/Lib/Api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import store from 'react-native-simple-store';
import {find} from 'lodash';
import squish from './Squish';
import axios from 'axios';

// use localhost:3000 or http://staging.bloc.io/ for testing purposes
// const apiRoot = 'http://localhost:3000';
Expand All @@ -16,8 +17,8 @@ const api = {
method: 'POST'
};

return fetch(url, params).then((res) => {
const resBody = JSON.parse(res._bodyText);
return axios.post(url).then((res) => {
const resBody = res.data;

if (res.status < 400) {
store.save('session', {
Expand All @@ -26,13 +27,17 @@ const api = {
});
}

if (res.status > 400) {
throw new Error(`Status ${res.status}: ${res.message}`)
}

return resBody;
})
.catch((error) => console.log('token creation failure'));
.catch((error) => console.log(`error: ${error.message}`));
},

sendMessage(id = null, token, text) {
store.get('session').then((session) => { //TODO: refactor a performAuthorizedAction function
store.get('session').then((session) => {
const authToken = session.token;
const user = session.current_user;
const params = {
Expand All @@ -46,15 +51,15 @@ const api = {

fetch(url, params)
.then((res) => {
if (res.status >= 400) { //TODO refactor a generic error handler to display to the user
if (res.status >= 400) {
console.log(res);
} else {
console.log("Message Sent. Response on next line.");
console.log(res);
}
})
.catch((error) => {
console.log(`Send Message error: ${error}`);
console.log(`Send Message error: ${error.message}`);
})
});

Expand All @@ -71,13 +76,13 @@ const api = {
}
};

return fetch(url, init)
return axios.get(url, init)
.then((res) => {
const items = JSON.parse(res._bodyText).items;
const items = res.data.items;
const messages = find(items, {id: id}).messages;
return messages;
})
.catch((error) => console.log(`API messages error: ${error}`));
.catch((error) => console.log(`API messages error: ${error.message}`));
});
},

Expand All @@ -92,12 +97,12 @@ const api = {
}
};

return fetch(url, init)
return axios.get(url, init)
.then((res) => {
const resBody = JSON.parse(res._bodyText);
const resBody = res.data;
return resBody.items;
})
.catch((error) => console.log(`API threads error: ${error}`));
.catch((error) => console.log(`API threads error: ${error.message}`));
});
}
};
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
"build": "curl http://localhost:8081/index.ios.bundle -o main.jsbundle"
},
"babel": {
"presets": ["react-native"]
"presets": [
"react-native"
]
},
"dependencies": {
"axios": "^0.9.1",
"babel-eslint": "^6.0.0",
"eslint": "^2.3.0",
"eslint-config-airbnb": "^6.0.2",
Expand Down

0 comments on commit d831495

Please sign in to comment.