Cant use multi auth middleware with guard in route - php

im using two auth table in my application
organizer and admin.
my config/auth.php file is here
return [
'defaults' => [
'guard' => 'manager'
],
'guards' => [
'manager' => [
'driver' => 'session',
'provider' => 'managers',
],
'organizer' => [
'driver' => 'session',
'provider' => 'organizers',
],
],
'providers' => [
'managers' => [
'driver' => 'eloquent',
'model' => App\Models\Manager::class,
],
'organizers' => [
'driver' => 'eloquent',
'model' => App\Models\Organizer::class,
],
],
'passwords' => [
],
'password_timeout' => 10800,
]
here is my problem on web.php (route)
Route::middleware('auth:manager')->prefix('/account')->group(function()
{
Route::get('/desk',function()
{
echo 1;
});
});
Route::middleware('auth:organizer')->prefix('/account')->group(function()
{
Route::get('/desk',function()
{
echo 2;
});
});
there is any problem here? because when i login into manager or organizer and set auth for theme its not working on middlware.
can someone solve my problem?

Related

How to Implement Authentication Guard without Authentication Defaults

Authentication Defaults
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
Authentication Guards
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
'student' => [
'driver' => 'session',
'provider' => 'students',
]
],
User Providers
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'students' => [
'driver' => 'eloquent',
'model' => App\Student::class,
]
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
when i try to login using student table username, password its not login but when i change the
Auth Defaults to students
'defaults' => [
'guard' => 'api',
'passwords' => 'students',
],
and api to students provider
'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
its login using students table data but the users table datas not allow to login.
How can i implement this two users table to login??
thanks in advance
you should pass guard name to auth function :
auth('students')->login();
or
Auth::guard('students')->login()
or you override default guard by passing there to auth middleware:
Route::group(['middleware' => ['auth:students'] ], function(){
//your routes here
});

Auth::guard('admin')->user() return always null

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');
// ...
});

How to use multiple authentication passwords in Laravel?

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.

How to defined multiple guard and access to it's class member in from laravel4.2 to Laravel5.3.19

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.

How to Use two Laravel Default Guard?

I'm trying to login an Admin and a User with the same form, but the config/auth I just only can to set a one default
This is my config/auth
'defaults' => [
'guard' => 'web_users',
'passwords' => 'users',
],
'guards' => [
'web_users' => [
'driver' => 'session',
'provider' => 'users',
],
'web_admins' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
When I log in a User with the default "web_users" I can retrieve his data with the method Auth::user(), but if I log in a Admin with the default "web_admins" can't retrieve anything.
Routes:
Route::resource('log','LogController');
Route::get('logout','LogController#logout');
Log Controller
public function store(Request $request)
{
if(Auth::guard('web_users')->attempt(['email'=>$request['email'],'password'=>$request['password']])) {
return Redirect::to('/');
}
if(Auth::guard('web_admins')->attempt(['email'=>$request['email'],'password'=>$request['password']])) {
return Redirect::to('/');
}
}
public function logout()
{
Auth::logout();
return Redirect::to('log');
}
Note: sorry for my english, I don't speak it very well
try this for Routes file
Route::group(['middleware' => 'auth:web_users,web_admin'], function () {
Route::resource('log','LogController');
Route::get('logout','LogController#logout');
});

Categories