How to connect the React Native app with Loopback API Explorer?

Viewed 1099

I have created a React Native app which can connect my own API but if I want to connect to the Loopback API, it always got some problem with the authentication, may I know how to connect the React Native app with the Loopback API? Previously, this is my own 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/`;

But after I change to the Loopback API it cannot get the token from it so I also want to know how to get the access token from it and act it as an authentication token, thank you.

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 Answers
Related