Guest User

Untitled

a guest
Apr 2nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. storeToken(responseData){
  2. AsyncStorage.setItem('access_token', responseData, (err)=> {
  3. if(err){
  4. console.log("an error");
  5. throw err;
  6. }
  7. console.log("storage token was a success");
  8. }).catch((err)=> {
  9. console.log("error is: " + err);
  10. });
  11. }
  12. async onLoginPressed() {
  13. this.setState({showProgress: true})
  14. username = this.state.username;
  15. password = this.state.password;
  16. try {
  17. let response = await fetch('https://roomies-backend-prithajnath.c9users.io/get_auth_token/', {
  18. method: 'POST',
  19. headers: {
  20. 'Accept': 'application/json',
  21. 'Content-Type': 'application/json',
  22. },
  23. body: JSON.stringify({
  24. username: username,
  25. password: password,
  26. })
  27. });
  28. let res = await response.text();
  29. if (response.status >= 200 && response.status < 300) {
  30. //Handle success
  31. let accessToken = res;
  32. console.log(accessToken);
  33. //On success we will store the access_token in the AsyncStorage
  34. this.storeToken(accessToken);
  35. this.redirect('Profile');
  36. } else {
  37. //Handle error
  38. let error = res;
  39. throw error;
  40. }
  41. } catch(error) {
  42. this.setState({error: error});
  43. console.log("error " + error);
  44. //this.setState({showProgress: false});
  45. }
  46. }
Add Comment
Please, Sign In to add comment