Laravel Passport POST oauth/token resulted in a '404 Not Found' - laravel

I'm using an apache virtualhost: 'gradez.loc', each time I try to access the /oauth/token route with cURL or Postman it returns a 404.
When I use php artisan serve and try to access to route via http://localhost:8000 there is no problem and I get back the access and refesh token.
What I have done already:
ran php artisan route:list I can see it returns all the passport routes correctly,
ran php artisan route:cache and php artisan route:clear
ran php artisan optimize
None of these seem to fix the problem unfortunately. I see a lot of people struggling with the same problem, but there's no good answer to this.
AuthServiceProvider
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
requestAccessToken method:
$response = $http->post(url('/oauth/token'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => config('services.passport.client_id'),
'client_secret' => config('services.passport.client_secret'),
'username' => $userData->username,
'password' => $userData->password,
],
]);
.env:
APP_URL=http://gradez.loc // <- oauth/token results in a 404
running php artisan serve
APP_URL=http://127.0.0.1:8000 // <- oauth/token works as expected!

Turned out my apache routes weren't setup correctly. I was using dnsmasq. I removed it then configured the vhosts myself and then it worked like a charm!

Related

How I make phpunit auth tests for laravel/passport?

I make backend API with laravel 9 (based on laravel/passport 10.3) and
I init app with command :
php artisan passport:install
which generates personal access client and client secret code
I use these codes in login request and recieve Bearer Token, which is used in all datas raeding/updating data with request.
Now I make phpunit tests for these requests and wonder in which wat these register/login requests must be tested as
usuallyin tests controllers I use
$this->actingAs($user, 'api');
Where $user is created with factory...
How that can be implemented ? Please link to example...
Thanks!
Well, you can use :
Passport::actingAs($user)
and usually we don't test third party code, so don't bother testing passport authentication routes, it's already well tested
but if you really want to do that, you can for example with $this->postJson
$this->postJson('api/oauth/token', [
'username' => 'username',
'password' => Str::random(),
'client_id' => 'client_id',
'client_secret' => 'secret',
'grant_type' => 'password',
'scope' => '*',
])->assertStatus(400)->assertJson([
'error' => 'invalid_grant',
]);

Laravel route group issue (blank page)

config: laravel 5.5
web.php
Route::group([
// 'middleware' => ['auth', 'is_admin'],
'namespace' => 'Admin',
'prefix' => 'admin',
],
function () {
Route::get('/', function(){
return 'admin';
});
Route::get('/pages', 'AdminController#pages')->name('admin_pages');
});
php artisan route:list
http://prntscr.com/moltmg
but get http://prntscr.com/moluky empty page
I don’t know what the problem is, I’m registering the path, I’ll use group, but I’m getting a blank page, no error, nothing. Other path work fine, also if I register a path in a group, for example:
admin / dashboard
everything is fine, only this one does not work.
Try running these commands.
php artisan config:clear
php artisan cache:clear
composer dump-autoload -o
if still not working.
provide 777 permission to storage and bootstrap/cache folders.
you need to add the route::get in your group.
this is an example that can help you :
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function()
{
//All the routes that you have :
Route::get('/pages', 'AdminController#pages')->name('admin_pages');
});
Hope this helps :) good luck
I apologize for the disturb, I am an idiot and created the "admin" folder in the "public" folder.

Laravel passport 500 internal server error when using subdomain

Using laravel passport on my local machine and domain http://myapp.test, I have no problem.
When I push the code to my server on my main domain https://myapp.com, again no problem.
However, I have a sudomain used for my live test (pre-production) before to push to the main domain in production. If I use https://dev.myapp.com, then I get a 500 internal server error.
Any idea how to fix it?
This is the guzzle call I'm doing:
$http = new GuzzleHttp\Client;
$response = $http->post('https://dev.myapp.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'client_id',
'client_secret' => 'client_secret_key',
'username' => 'john#example.com',
'password' => '123456',
'scope' => '',
],
]);
return $response;
If I change the url to my local or production website (changing the passport key and making sure the same user exists), it works properly.
My bad. Just forgot to run that command on the server for that specific domain as specified in the documentation. This command generates the encryption keys Passport needs in order to generate access token.
php artisan passport:keys

Laravel 5.5 socialite integration shows error formatRedirectUrl() must be of the type array, null given

I am using "laravel/socialite": "^3.0", to facebook login. But it shows
an error
Type error: Argument 1 passed to Laravel\Socialite\SocialiteManager::formatRedirectUrl() must be of the type array, null given, called in /var/www/html/mas/vendor/laravel/socialite/src/SocialiteManager.php.
It happens when I am calling the below function in my login controller
public function socialLogin($social)
{
return Socialite::driver($social)->redirect();
}
Hi you are missing to give credentials of social media put that in config/services.php
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('CALLBACK_URL_FACEBOOK'),
],
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('CALLBACK_URL_GOOGLE'),
],
'twitter' => [
'client_id' => env('TWITTER_CLIENT_ID'),
'client_secret' => env('TWITTER_CLIENT_SECRET'),
'redirect' => env('CALLBACK_URL_TWITTER'),
],
'linkedin' => [
'client_id' => env('LINKEDIN_CLIENT_ID'),
'client_secret' => env('LINKEDIN_CLIENT_SECRET'),
'redirect' => env('CALLBACK_URL_LINKEDIN'),
],
'instagram' => [
'client_id' => env('INSTAGRAM_CLIENT_ID'),
'client_secret' => env('INSTAGRAM_CLIENT_SECRET'),
'redirect' => env('CALLBACK_URL_INSTAGRAM'),
],
You must clear the configuration cache file, how? if you use php artisan you will see the command config:clear. Run it:
php artisan config:clear
and now if you access to the $config variable inside the Laravel\Socialite\SocialiteManager::createFacebookDriver()
you will get the configuration element stored in config/services.facebook (Facebook, for example) that before was not 'visible' for Socialite.
In short: run php artisan config:clear
This happened to me just recently, fixed it after reading the following post here on StackOverflow:
Why do I have to run "composer dump-autoload" command to make migration work in laravel
The solution is basically to run the following commands:
php artisan clear-compiled
composer dump-autoload
php artisan optimize
The problem is with the parameters passed to the function socialLogin($social).
Try by manually setting the 'github' or 'facebook' string in the drvier function of Socialite.
Dont forget to mention 'github' , 'facebook, etc in the route in web.php
I had same problem.
I got this errot because of copy/paste while reading Laravel doc. In my case in loginController.php changed this:
// I copied this from Laravel doc
public function redirectToProvider()
{
return Socialite::driver('github')->redirect();
}
//What I really needed
public function redirectToProvider()
{
return Socialite::driver('google')->redirect();
}
I was having the same issue even after had set up my config/services.php file and managed to solve it by clearing my cache. In your project directory run
php artisan optimize:clear
php artisan cache:clear
php artisan config:clear
php artisan config:cache
This ensures that your entire cache is cleared.
NOTE: changing core files in Laravel most of the times require you running the above commands as Laravel tends to use caching to a greater extend to improve on application speeds
Try these
Check if you have all your social media in config/services.php
On your register or login page check the url that corresponds to facebook
a href="{{ url('/login/facebook') }}"
Go to Routes and check if you have routes properly configured
Route::get('login/{social}', 'Auth\LoginController#redirectToProvider');
Route::get('login/{social}/callback', 'Auth\LoginController#handleProviderCallback');
Open Auth/LoginController and check if have
use Socialite;
public function redirectToProvider($social)
{
Socialite::driver($social)->redirect();
}
public function handleProviderCallback($social)
{
$user = Socialite::driver($social)->user();
}
As you've mentioned you are only facing the error with facebook, im pretty sure it must be in first three steps
Open vendor\laravel\socialite\src\SocialiteManager.php
Replace to
protected function formatRedirectUrl(array $config)
{
$redirect = value($config['redirect']);
return Str::startsWith($redirect, '/')
? $this->app['url']->to($redirect)
: $redirect;
}
Open vendor\laravel\socialite\src\SocialiteManager.php and
Replace
protected function createFacebookDriver()
{
$config = $this->app['config']['services.facebook'];
return $this->buildProvider(
FacebookProvider::class, $config
);
}
To
protected function createFacebookDriver()
{
$config = $this->app['config']['services.stripe.facebook'];
return $this->buildProvider(
FacebookProvider::class, $config
);
}

Laravel 5.3 upgrade - BroadcastServiceProvider error

I have tried to upgrade laravel 5.3 from 5.2 and I am getting following error while php artisan clear-compiled
Class App\Providers\BroadcastServiceProvider contains 1 abstract
method and must therefore be declared abstract or implement the
remaining methods (Illuminate\Support\ServiceProvider::register)
However, I had not facing such an issue while upgrade in my local environment.
The config/broadcasting.php is as:
<?php
return [
'default' => env('BROADCAST_DRIVER', 'log'),
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_KEY'),
'secret' => env('PUSHER_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'log' => [
'driver' => 'log',
],
],
];
The app/Providers/BroadcastServiceProvider.php is as:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;
class BroadcastServiceProvider extends ServiceProvider
{
public function boot()
{
Broadcast::routes();
Broadcast::channel('App.User.{userId}', function ($user, $userId) {
return (int) $user->id === (int) $userId;
});
}
}
And .env is as:
CACHE_DRIVER=file
SESSION_DRIVER=file
#BROADCAST_DRIVER=pusher
PUSHER_KEY=someKey
PUSHER_SECRET=SomeSecrete
PUSHER_APP_ID=SomeId
I tried setting default broadcasting driver to log, but seems not working.
Any command I am running like:
php artisan cache:clear Or php artisan config:clear Or php artisan view:clear Or php artisan clear-compiled, I am facing the same error.
I also tried using composer dump-autoload, it works fine but after that if I run php artisan clear-compiled again then also facing the same error.
Please help me.
Looks like you didn't realy update the framework, because Illuminate\Support\ServiceProvider::register method is present in 5.2 and not in 5.3
Double check your update
I have fixed this error by running following artisan commands before updating the composer to upgrade to laravel 5.3.
The commands are:
php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan clear-compiled
and then do
composer update
It will solve the said error while upgrading to laravel 5.3.

Resources