How can I specify a guard in middleware for route? - php

I have two routes as follow:
Route::GET('admins/', 'UserController#index')->middleware('jwt.auth');
Route::GET('visitors', 'UserController#indexVisitors')->middleware('jwt.auth');
And I have guards in auth.php:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt-auth',
'provider' => 'users',
],
'visitor_api' => [
'driver' => 'jwt-auth',
'provider' => 'visitors',
],
],
I tried to specify the guard in the middleware but it doesn't work.
Route::GET('visitors', 'UserController#indexVisitors')
->middleware('jwt.auth.visitors_api');

If you would like to set a default guard through out the Route::group then you can use below syntax
Route::group(['middleware' => ['web','auth:visitor_api'], 'prefix' => 'visitor'], function() {
Route::get('/home', 'VisitorController#index')->name('home');
Route::get('/list', 'VisitorController#list')->name('list');
});
after this you can use Auth::id() instead of Auth::guard('visitor_api')->id() in your VisitorController.

I think this is what you want
Route::GET('visitors', 'UserController#indexVisitors')->middleware('auth:visitors_api');
You can specify a guard by passing it as a parameter (after the colon character)
You can refer to the laravel documentation:
https://laravel.com/docs/5.6/authentication
Under Authentication Quickstart > Protecting Routes > Specifying A Guard

Related

How to fix following error: InvalidArgumentException: Auth guard [jwt] is not defined

I have followed following guide to make an Authentication Api, and an api which uses the authentication from the first one. I have done everything exactly the same, but i still get this error:
InvalidArgumentException: Auth guard [jwt] is not defined. in file folder/to/project/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 84
The guide i have followed is: https://medium.com/swlh/build-jwt-authentication-between-multiple-api-with-laravel-fbd03352aaa3
My config/auth looks like this:
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
// 'hash' => false,
],
],
Then my app/guard/JWTGuard.php looks exactly like in the guide.
My routes/api.php look the following:
Route::middleware('auth:jwt')->resource('projects', ProjectController::class);
Does anybody has this issue before and know how to solve it?
The problem was in routes/api.php. The middleware shouldn't be "auth:jwt", but "auth:api". This did the trick for me.

laravel 8 auth Route [login] not defined

'Route [login] not defined ..'
I'm new to Laravel. I want to make a login system myself, but I still have not solved this error. I am logging in but the user appears blank on the other page, I still cannot control it. I want to use the user restrictions on other pages, how can I do this.
web.php
Route::post('login', [userLogin::class, 'loginPost'])->name('login.post');
Route::get('student/home', [studentHome::class, 'index'] )->name('student.home')->middleware('auth:students');
Route::get('/', function () {
return view('login');
userLoginController.php
public function loginPost(Request $request)
{
if(auth::guard('students')->attempt(['email'=>$request->student_mail,'password'=>$request->student_password],false))
{
//dd(Auth::guard('students')->check());
return redirect()->intended('student.home');
}
else
{
echo "yannlış";
}
}
auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'students' => [
'driver' => 'session',
'provider' => 'students',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'students' => [
'driver' => 'eloquent',
'model' => App\Models\students::class,
],
You are missing the login route which is used to display the login form. Try naming your root:
Route::get('/', function () {
return view('login');
})->name('login');
Or you can add a separate route depending on what you are looking for.
Route::get('login', [UserLogin::class, 'showLoginForm'])->name('login');
I run into this error when i was learning creating an API using laravel passport middleware, it redirects by default to the GET login route, so i had to add like the previous comment said in my web.php.
Route::get('/login', function () {
return view('login');
})->name('login');

Auth guard [:api] is not defined?

when I use auth api gaurd for logout route. I am facing with the following Exception
Auth guard [:api] is not defined
I have already implemented registration login Apis but I am facing this error with logout api which I had protected using auth::api
config.auth file
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
api.php
Route::group(['prefix'=>'auth'],function(){
Route::post('login','AuthController#login');
Route::post('signup','AuthController#signup');
Route::group(['middleware' => 'auth::api'], function () {
Route::get('logout','AuthController#logout');
Route::get('user','AuthController#user');
});
});
I should be able to logout the user
If you are using Laravel 9+ and Passport, you need to implemented this inside the guard array on config/auth.php file:
'guards' => [
...
// you need to implement this
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => true,
],
],
You have an extra colon in your code, that's why it is trying to find the guard :api.
According to the docs:
Middleware parameters may be specified when defining the route by
separating the middleware name and parameters with a :. Multiple
parameters should be delimited by commas:
Route::put('post/{id}', function ($id) {
//
})->middleware('role:editor');
So in your case it would be:
Route::group(['prefix' => 'auth'], function () {
Route::post('login','AuthController#login');
Route::post('signup','AuthController#signup');
Route::group(['middleware' => 'auth:api'], function () {
Route::get('logout','AuthController#logout');
Route::get('user','AuthController#user');
});
});
I had the same issue, it seems that i forgot to change Authentication Defaults
so in config/auth.php change this
'defaults' => [
'guard' => 'web',
....
],
into this
'defaults' => [
'guard' => 'api',
....
],

laravel passport api authentication

I'm new to api authentication (passport) in laravel.
Is it possible to guard api routes using
$this->middleware('auth:api');
even if used the laravel built in authentication
(php artisan make:auth)?
you can guard your api with auth:api, just need to change the api guard driver to passport in config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
//'driver' => 'token',
'driver' => 'passport',
'provider' => 'users',
],
],

Auth guard driver [api] is not defined

I am using laravel 5.4 and using jwt auth
jwt version is jwt-auth "tymon/jwt-auth": "0.5.*"
In auth.php i have
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
In Api.php i have
Route::post('/login','HomeController#authenticate');
Route::group(['prefix' => 'v2','middleware' => 'auth:api',], function () {
// Authentication Routes...
Route::get('/logout','HomeController#logout');
Route::get('/home','HomeController#home');
});
When i post email and password from postman it will return token .
when i try to acccess
http://localhost/demo/public/api/v2/home
and header i passed Authorization bearer token
I am getting following error in postman
Whoops, looks like something went wrong. (1/1)
InvalidArgumentException Auth guard driver [api] is not defined. in
AuthManager.php (line 99) at AuthManager->resolve('api') in
AuthManager.php (line 70) at AuthManager->guard('api') in
Authenticate.php (line 61) at Authenticate->authenticate(array('api'))
can any help me how to fix it
Also i checked follwing issue since its old one Sep 27, 2016
https://github.com/tymondesigns/jwt-auth/issues/860
try to set token:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token', // here !
'provider' => 'users',
],
],
Make sure in your kernel.php file you've declared it in
protected $routeMiddleware =
Not necessarily an answer, but this is the article I have used a couple times to install and setup JWT tokens and I have had no issues.
https://medium.com/employbl/build-authentication-into-your-laravel-api-with-json-web-tokens-jwt-cd223ace8d1a
Hopefully this helps, good luck!
I solved the problem by adding this line to the providers in config\app.php
'providers' => [
...
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
]
The error may occur if the API guard does not exist. In order to solve this issue, you have to set the API guard on
root->config->auth.php [line:38]
as follows
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token', // make sure to add token on API driver
'provider' => 'users',
],
],
the request from the default register form will have following inputs
local.DEBUG: array (
'_token' => 'J0FzfW7huNaJxcUZnjYXF9g63OOiSHaH9vZSBc73', // token is required from the request
'name' => 'mygem',
'phone' => '+1 (484) 853-6668',
'email' => 'cyvu#mailinator.com',
'password' => 'Pa$$w0rd!',
'password_confirmation' => 'Pa$$w0rd!',
)
I had the same issue. I solved it by adding
"tymon/jwt-auth": "^1.0"
in the require array of composer.json, then running composer install

Categories