Laravel Pasport, Two Login Login tables

0

I am trying to make a user authentication with two tables, a different table to 'users' and the one of 'users' which is the default Laravel, use laravel passport. The detail is when I try to login with the second table I can not do the authentication because it does not recognize the users of the second table.

-I have the model User2 - > protected $ guard_name = 'api2';

-Use another connection - > protected $ connection = 'mysql2'

en config/auth.php
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],

    'api2' => [
        'driver' => 'passport',
        'provider' => 'users2',
      ],
],



 'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    'user2' => [
        'driver' => 'eloquent',
        'model' => App\User2::class,
    ],
],

And in my controller

introducir el código aquí
 public function login(Request $request)
 {
if (Auth::attempt($request->only('email', 'password'))) {
  $email = $request->only('email');
  $token =  $user->createToken('123456')->accessToken;
  $user->withAccessToken($token);
  $email=json_decode(json_encode($email));
  $user = User2::where('email', $email->email )->first()->only('id','name','email','employee_id');

  $user1 = User::find($user['id']);

  if(!$user1->hasAnyRole(Role::all())){
    return response()->json([
      'success' => false,
      'data' => '',
      'msg' => "does not have assigned role"
    ], 403);
  }
  $roles = $user1->getRoleNames()->first();
  $role = Role::findByName($roles,'api');
  $user=json_decode(json_encode($user));

  return response()->json([
    'success' => true,
    'token' => $token,
    'data' => [
      'user_id' => $user->id,
      'name' => $user->name,
      'email' => $user->email,
      'employee_id' => $user->employee_id,
      'role_id' => $role->id,
      'role_name' => $role->name
        ],
    'msg' => "Successfully"
  ], 200);
} else {
  return response()->json(['error' => 'Unauthorised'], 401);
}

}

    
asked by rodrigo sauceda 29.11.2018 в 20:13
source

0 answers