Laravel Multiple Auth::attempt - laravel

I have two models in my laravel v5.4 project, user and admin.
In config/auth.php i added admin to guards and providers as below :
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
],
Now in AdminController class i want to use Auth::attempt function but by default it uses the users table. I can change defaults in config/auth.php as below and it works but in this case i can not use Auth::attempt for users.
'defaults' => [
'guard' => 'admin',
'passwords' => 'users',
],
I want to set user as default but use Auth::attempt function for admin with a method like Auth::attempt('admin',[credentials]). How can i use Auth::attempt for Admin model?

You call the guard directly, like this:
Auth::guard('admin')->attempt($credentials);
Or with the helper:
auth()->guard('admin')->attempt($credentials);
Or, even shorter with the helper:
auth('admin')->attempt($credentials);

Related

Laravel Sanctum middleware with OR condition

I am using Laravel Sanctum for Token Based Authentication.
I have created a customer guard for customer. For Other users its default users table.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'customer' => [
'driver' => 'sanctum',
'provider' => 'customers',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => Customer::class,
],
],
I was authenticating all other users using middleware' => ['auth:sanctum'] now for the given particular route, I want customer guard can also access this Route. But there is no such Functionality provided by Laravel for OR condition.
Route::group(['as' => 'business', 'prefix' => 'business', 'middleware' => ['auth:sanctum']], function () {
Route::get('/categories', [BusinessCategoryController::class, 'listBusinessCategories'])->name('business-category');
Route::get('/categories/{parentId}', [BusinessCategoryController::class, 'listBusinessSubCategories'])->name('business-sub-category');
});
I just want middleware' => ['auth:sanctum'] or ['auth:customer']

Laravel Passport: how to customize the authentication guard for the user provider in your application

Laravel by default creates the model and the User table. The problem is that I create and use the table in the database, a model and a controller called Citizen.
And here comes my doubt, when we configure laravel-passport we must specify the guard, this is the Laravel example:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
What is the provider? Taking into account this line:
'provider' => 'users',
Should I use 'provider' => 'citizen', instead of 'provider' => 'users'?
You can create custom provider. This has to be done in config/auth.php
There is one provider named users by default. You can create your own guard and providers as follows:
guards:
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'citizens' => [
'driver' => 'passport',
'provider' => 'citizens',
],
],
In the providers section:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'citizens' => [
'driver' => 'eloquent',
'model' => App\Models\Citizen::class, //path to your model
],
],
Now you can use the middleware with custom guard by using auth:citizens as middleware.
For example, in route file,
Route::middleware('auth:citizens')->group(function(){
});

Laravel 7 guard doesn't log in

I have a question regarding to laravel 7 custom guard auth.
$this->guard()->attempt(
$this->credentials($request), $request->filled('remember')
);
This line of code use the default guard 'web', which works just fine.
$this->guard('agent')->attempt(
$this->credentials($request), $request->filled('remember')
);
This line of code use the custom guard 'agent', is has been configured correctly, as it does return '1' as true, I can also get the user info by using guard('agent')->user().
However, it does not log the user into the application. It loop back to the login page. Any help would be appreciated.
Guard Code:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'agent' => [
'driver' => 'session',
'provider' => 'agents',
],
],
Provider Code:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'agents' => [
'driver' => 'eloquent',
'model' => App\Agent::class,
],
],
instead of using $this->guard()->attempt(
replaced it to
auth()->attempt([$this->credentials($request), $request->filled('remember')])
if you want to use the default guard or if you want to use specific guard just do it this way. we'll use your existing guards that can be used.
auth('agent')->attempt([$this->credentials($request), $request->filled('remember')])

How to solve Auth guard [web] is not defined problem

I make 2 guards 1. For admin and 2. for student. Now when i click on my button to show the login form i always caught that error that Auth guard [web] is not defined.Now how i solve this.
here is my config/auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'student' => [
'driver' => 'session',
'provider' => 'student',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'students' => [
'driver' => 'eloquent',
'model' => App\Student::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
I am trying login the admin using admin guard.Please help me to solve that and i dont use default User.php model.
That's because you're removing web guard from the application.
Just change your default guard from web to your new guard.
'defaults' => [
'guard' => 'admins',
'passwords' => 'users',
],

How to verify emails on Laravel 5.7 Multiple Guards?

I am using multiple guards and want to use Laravel inbuilt system for sending verification emails. I am doing following
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'frontEnd' => [
'driver' => 'session',
'provider' => 'customers',
],
...
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Customer::class,
],
...
],
In the User Model and Customer Model, I am using MustVerifyEmail. Both Model has email_verified_at column. When I am creating a user and when that user tries to login he redirects to email/verify the route. Email does not trigger at the time of creating a User.
For some reasons I am not using Auth::routes(['verify' => true]); insted i put
Route::get('email/verify', 'Auth\VerificationController#show')->name('verification.notice');
Route::get('email/verify/{id}', 'Auth\VerificationController#verify')->name('verification.verify');
Route::get('email/resend', 'Auth\VerificationController#resend')->name('verification.resend');
On route/web.php file

Resources