The auth.php file has the following code
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'apidriver' => [
'driver' => 'passport',
'provider' => 'drivers',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'drivers' => [
'driver' => 'eloquent',
'model' => App\Models\Driver::class,
],
],
And this is the authentication code for the driver
if (Auth::guard('apidriver')->attempt(['email'=>request('email'),
'password'=>request('password')])) {
$driver=Auth::guard('apidriver')->user();
$success['token'] = $driver->createToken('Pizza App')->accessToken;
return response()->json(['success' => $success], $this->successStatus);
}else{
return response()->json(['errorrr'=>'Unauthorised'], 401);
}
And generates the following error when accessing
Call to undefined method Illuminate\Auth\RequestGuard::attempt()
When the user authentication code is working correctly
You are not parsing your request parameters properly,
replace this line
if(Auth::guard('apidriver')>attempt(['email'=>request('email'),'password'=>request('password')]))
with this:
if (Auth::guard('apidriver')->attempt(['email'=>request('email'),'password'=>request('password')]))
Also check that you called Illuminate\Http\Request; at the top of your class.
Related
I define a new guard "Admin" to have a multi Auth System User and admin in my project . when i login it is ok and i get the token to the rest of my api routes .When when I use The auth::guard('Admin')->user() i got always null
Any suggestion??
Auth.php :
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
Login method :
public function ALogin(Request $request){
// dd($request);
// dd($rrequest->header);
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {
$this->connectedAdmin = Auth::guard('admin')->user();
// dd( $connectedAdmin);
$success['token'] = $this->connectedAdmin->createToken('MyApp')->accessToken;
$return = new \stdClass();
$return->token = $success['token'];
//dd(self::$connectedAdmin);
return response()->json($return, 200);
}
// dd(Auth::guard('admin')->user());
return response()->json("error", 400);
}
*/
In your route file, to specify a guard for several endpoints you coud do:
/** routes/api.php */
Route::group(['guard' => 'admin'], function () {
Route::get('/a-route', 'AController#method');
// ...
});
I got exception after i made multi auth in my project :
Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given, called in D:\xaamp\htdocs\laravelapi\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php
Here is a picture:
I don't get it, please help, thanks.
here is the auth.php config file
'defaults' => [
'guard' => 'web',
'passwords' => 'user',
], 'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'library' => [
'driver' => 'session',
'provider' => 'libraries',
],
],'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'libraries' => [
'driver' => 'eloquent',
'model' => App\Library::class,
],
and i made for each guard loginController and login views
I'm getting this error:
Call to undefined method Illuminate\Auth\TokenGuard::attempt()
From this code:
if(Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){
return redirect()->intended(route('admin.dashboard'));
}else{
I have imported Illuminate\Support\Facades\Auth as the docs suggest
My auth.php may help
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 15,
],
],
];
I think you are probably trying to use a "session" guard driver instead "token". So, try to do this:
In config/auth.php configuration file:
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
You have to change it to
'admin-api' => [
'driver' => 'session',
'provider' => 'admins',
],
Then you should run:
php artisan cache:clear
php artisan config:cache
And give it a try again. Good luck!
The token guard doesn't have an attempt method, this is a function used in session authentication. So you will need to authorize the users yourself or use the Laravel Passport authentication https://laravel.com/docs/6.x/passport
I am trying to do multiple authentication with different tables. And i am also adding these tables in auth.php But When i attempt to login the error show Auth[] not define.
'defaults' => [
'users'=>[
'guard' => 'users',
'passwords' => 'users'
],
'branch'=>[
'guard' => 'branch',
'password' => 'branch'
]
],
'guards' => [
'users' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'branch'=> [
'driver' => 'session',
'provider' => 'branch',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'branch' => [
'driver' => 'eloquent',
'model' => App\Branch::class,
],
],
I'm just upgrade From laravel4.2 to laravel5.3.19 now i want to access Authentication data by below method
1: Auth::employees()->attempt($data)
2: Auth::admin()->logout()
3: Auth::admin()->get()->email
.................................
Example: Auth::guard->guardName->property.
I think All of those method should be call
1:Auth::user()->id
But i don't want to correct all old method because they used a lot of in this system so i want to find the solution by keeping those method as the same. And I have configured Auth.php in Configure directory as below but it simply not work as expected.
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin'
],
'employee' => [
'driver' => 'session',
'provider' => 'employee'
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'employee'=>[
'driver'=>'eloquent',
'model'=>App\Models\Employee::class
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class
],
],
Please help.