I am adding a custom authentication system via guards , models and providers.
After trying to read some documentation and articles, I am bit confused about the different way of invoking guards and middleware
Question 1:
So, if I want to invoke (default) authentication in a particular route (as defined in auth.php as "default => [ 'guard' => 'web' ... , is it
'middleware' => ['auth']]
Does this mean the default auth ? Or the middleware "auth" - which is included in the Kernel.php as below
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
if i want the default auth as defined in "web" of config/auth.php
should it be
'middleware' => ['auth']]
or
'middleware' => ['web']]
or
'middleware' => ['auth:web']]
Question 2: Now there is a middleware group called "web" which has useful things like StartSession, EncryptCookies, CSRF Protection, - so if I have a custom guard "abcguard" - is session management, cookie encryption done through some mechanism or I should explicitly add that middleware "web" in my routes /route-group?
Question 3: what if I have a custom guard called "abcguard" defined in auth.php as such and I have a middleware group called abcguard (just for illustration purpose) -- how do I make sure that that a route dashboard is available after the abcguard authentication and passing though middleware abcguard
Is this the right way?
'middleware' => ['auth:abcguard', abcguard]]
How does Laravel know which one is authentication and which one is just a middleware? Or is "guard" just a name for another middleware - the only specific behaviour is that a guard determines how users are authenticated and a middleware might just check if an user is authenticated?
Q4 - In the statement below, how does laravel find out which abcguard is a guard and which is a middlewar group?
'middleware' => ['auth:abcguard', abcguard]]
First, you seem to confuse
web
middleware group (defined in Kernel) withweb
guard (authentication mechanism using session) for auth middleware, and they are even not related TBH. Guard is used to authenticate users, and middleware group just holds different middlewares for your web routes. If in yourauth.php
config, you have default guardweb
, the'middleware' => 'auth'
will end up usingweb
guard. In case you want to use other guard, you will need to use'middleware' => 'auth:other_guard'
Again,
web
middleware group only holds middlewares for ALL of your web routes. You should not addauth
to all of them, for sure.The right way will be
'middleware' => ['auth:abcguard', 'abcguard']
, yea. In this case, you haveauth
middleware usingabcguard
guard andabcguard
middleware group.When you write
auth:abcguard
you are callingauth
middleware and passingabcguard
as argument to it's handle function. Same if it wasweb
- default session guard, orapi
- default auth token, orsanctum
- sanctum's session cookie. Middleware groups are defined in yourApp\Http\Kernel::class
. In case you haveabcguard
middleware group there, you will be using all middlewares defined in that group.