如何使用Loopback API Explorer连接React Native应用程序?

时间:2017-10-05 01:28:52

标签: android ios api react-native loopback

我创建了一个可以连接我自己的API的React Native应用程序但是如果我想连接到Loopback API,它总是遇到一些身份验证问题,我是否知道如何使用Loopback API连接React Native应用程序?以前,这是我自己的API:

var API_URL = 'http://192.168.10.130:8000/api';
exports.SIGNIN_URL = `${API_URL}/auth/token/`;
exports.TODOS_URL = (token) => `${API_URL}/workrequests/`;

但是在我更改为Loopback API后,它无法从中获取令牌,所以我也想知道如何从中获取访问令牌并将其作为身份验证令牌,谢谢。

var API_URL = 'http://localhost:3000/api';
exports.SIGNIN_URL = `${API_URL}/Users/login/`;
exports.TODOS_URL = (token) => `${API_URL}/WorkRequests/`;

// AuthAction.js

import axios from 'axios';
import Keychain from 'react-native-keychain';

import {SIGNIN_URL} from '../api';
import {addAlert} from './alertsActions';

exports.loginUser = (username, password) => {
  return function(dispatch) {
    return axios.post(SIGNIN_URL, {username, password}).then((response) => {
  var {token} = response.data;
  console.log(token);
  Keychain.setGenericPassword(username, token)
    .then(function() {
      dispatch(addAlert(token));
      dispatch(authUser(token));
    }).catch((error) => {
  console.log(error.response.data);
  console.log(error.response.status);
  console.log(error.response.headers);
    });
  }).catch((error) => {
  dispatch(addAlert("Could not sign in."));
});
  }
}

authUser = (token) => {
  return {
    type: 'AUTH_USER',
    token
  }
}

exports.unauthUser = {
  type: 'UNAUTH_USER'
}

0 个答案:

没有答案