J.Wennergren
J.Wennergren

Reputation: 145

Laravel passport unauthenticated from app not from postman

Im having some troubles with laravel passport.

Im trying to fetch some data from my api. from a react native app. it works fine when trying it out from postman, but when i do the same from my app it doesn't work. how come?

AsyncStorage.getItem('TOKEN').then(token => {
    fetch(BACKEND_URL + '/api/getdata/', {
        method: 'GET',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + token,
        },
    })
    .then(response => response.json())
    .then(responseJson => {
        console.log(responseJson);
        this.setState({
            data: responseJson.data,
            isLoading: false,
        });

    }) 
    .catch(error => {
        console.error(error);
    });
});
//DataController.php
public function fetchdataAPI()
{
    $user = auth('api')->user();

    $data = Data::all();

    return response()->json([
        'data' => $data,
    ]);
}
//API.php
Route::group(['middleware' => 'auth:api'], function(){

Route::get('getdata','dataController@fetchDataAPI');
});

When fetching data from react-native app using fetch api, the response is "unauthenticated"

Upvotes: 2

Views: 834

Answers (1)

Edrian
Edrian

Reputation: 608

Make sure that the Back-end URL's are the same including the protocols (SSL issue).

Upvotes: 1

Related Questions