5

I have a really strange problem with Laravel 5.5 Passport API Authentication.

I need to permit an external site to authenticate via 'Implicit Grant Token' method and get data from the API.

I'm stuck on authentication. JavaScript sends an AJAX request to the API, but all it gets in return is an 401 (Unauthorized) Error (instead of a token).

The setup is by the book (https://laravel.com/docs/5.5/passport#implicit-grant-tokens)

  1. Fresh Laravel 5.5 install

  2. Laravel CORS added https://github.com/barryvdh/laravel-cors

  3. Passport package install composer require laravel/passport

  4. Migration php artisan migrate

  5. Passport install php artisan passport:install

  6. App\User model adjusted

  7. AuthServiceProvider adjusted

  8. config/auth.php adjusted

  9. Sample client created with php artisan passport:client

  10. Passport::enableImplicitGrant(); added to AuthServiceProvider

The JS looks like this:

var serialize = function(obj) {
    var str = [];
    for (var key in obj)
        if (obj.hasOwnProperty(key)) {
            str.push(encodeURIComponent(key) + "=" + encodeURIComponent(obj[key]));
        }
    return str.join("&");
}

var request = new XMLHttpRequest();
var data = {
    'client_id': '1',
    'response_type': 'token',
    'redirect_uri': 'http://localhost',
    'scope': ''
}
request.onreadystatechange = function(res) {
    if (request.readyState == XMLHttpRequest.DONE) {
        //console.log(res.responseText);
    }
}
request.open('GET', '//localhost/oauth/authorize?' + serialize(data), true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.setRequestHeader('Accept', 'application/json');

request.send();

Unfortunately, 401 ERROR is all it GETs.

All the files are available on: https://github.com/michalduda/laravel-passport.git

Do you have any idea what is wrong?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
michalduda
  • 738
  • 5
  • 7
  • I've had a similar-ish issue with passport. [Have a look at my question, maybe it will help you](https://stackoverflow.com/questions/39525968/laravels-5-3-passport-and-api-routes) – Andrei Sep 28 '17 at 11:01

1 Answers1

0

You missing post your content from:

route\api.php
Wisdom
  • 121
  • 1
  • 1
  • 13