Laravel 7 guard doesn't log in - laravel

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')])

Related

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(){
});

Is there way to fix argument 2 passed to Tymon\\JWTAuth\\JWTGuard::__construct() using JWT and Laravel?

I have problem on my jwt authentication. When I hit the api for the login of admin it shows error on my postman says that "message": "Argument 2 passed to Tymon\JWTAuth\JWTGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given.
I read some threads regarding on this, they said that there is problem on auth.php of laravel I guess it is providers.
Here is what is inside on my auth.php and my controller.
Controller
public function __construct()
{
Config::set('jwt.user', Admin::class);
Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Admin::class,
]]);
}
Auth.php
'guards' => [
// 'web' => [
// 'driver' => 'session',
// 'provider' => 'users',
// ],
'api' => [
'driver' => 'jwt',
'provider' => 'assureds',
'hash' => false,
],
'admins' => [
'driver' => 'jwt',
'provider' => 'admins',
],
],
Provider:
'providers' => [
'assureds' => [
'driver' => 'eloquent',
'model' => App\Assured::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],

how to solve Laravel 6.0 error upload file

I'm trying to input some data to database
i cant input some data to database because i got that error
but i can login with multi auth in my project
and i got some error
how i can solve this error?
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Jurusan;
use Auth;
class AdminActionsController extends Controller
{
public function addjurusan(Request $request)
{
$jurusan = new Jurusan();
$jurusan->nama_jurusan=$request->nama;
$file=$request->file('fotohimpunan');
if (!$file) {
return redirect()->route('in.jurusan')->with('alert','foto harus diisi!');
}
$file_name=$file->getClientOriginalName();
$path=public_path('/img');
$file->move($path,$file_name);
$jurusan->fotohimpunan='public/img/'.$file_name;
$jurusan->status='disable';
// dd($jurusan);
$jurusan->save();
return redirect()->route('in.jurusan');
}
}
guard
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
// Guard
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admin',
],
'panitia' => [
'driver' => 'session',
'provider' => 'panitia',
],
'panitia-api' => [
'driver' => 'token',
'provider' => 'panitia',
],
'mahasiswa' => [
'driver' => 'session',
'provider' => 'mahasiswa',
],
'mahasiswa-api' => [
'driver' => 'token',
'provider' => 'mahasiswa',
],
],
// Providers
'providers' => [
'panitia' => [
'driver' => 'eloquent',
'model' => App\Panitia::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'mahasiswa' => [
'driver' => 'eloquent',
'model' => App\Mahasiswa::class,
],
],
// Password
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
how i can solve my problems please help me
i can login with multi auth but if i try to input some data i got that error
You don't have a users provider:
'providers' => [
'panitia' => [
'driver' => 'eloquent',
'model' => App\Panitia::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'mahasiswa' => [
'driver' => 'eloquent',
'model' => App\Mahasiswa::class,
],
],
The web guard is set to use a provider named users:
'web' => [
'driver' => 'session',
'provider' => 'users',
],
You need to adjust that to use a provider you have registered, or add a users provider.
Are you intending to be using the web guard for which ever route you are hitting and getting the error? It is possible you intend to use a different guard completely.

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',
],

Laravel Multiple Auth::attempt

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);

Resources