简体   繁体   中英

Multiple Auth in Laravel 5.5

I want to integrate multiple table in authentication, i've gone through these steps:

First I. created a migration:

php artisan make:migration createTouristsTable

in Migration file:

public function up()
{
    Schema::create('tourists', function (Blueprint $table) {
        $table->increments('id');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();            
        $table->timestamps();
    });
}

run migrate

php artisan migrate

Tourists Model:

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Tourists extends Authenticatable
{
    use Notifiable;

    protected $table = 'tourists';

    protected $fillable = ['email',  'password'];

    protected $hidden = ['password',  'remember_token'];
}

i've modified config/auth.php file to add my custom guard "tourists".

'guards' => [

    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'tourists'  => [
      'driver'  => 'session',
      'provider' => 'tourists',
    ],

],

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

    'tourists' => [
        'driver' => 'eloquent',
        'model'  => App\Tourists::class,
    ],
]

Now in my LoginController I need to specify which guard I want to use:

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function guard()
    {
        return Auth::guard('tourists');
    }
}

Now, how I create a login and registration form for this Table?

You would create new login and registration pages just like you would any other page. The important difference is having either separate routes for each login/registration page or conditionally rendering partials that contain the forms based on some criteria you define so guests go to guest login and regular users go to regular login.

A third option, in my opinion the best fit for this case, is to use components and slots.

https://laravel.com/docs/5.5/blade#components-and-slots

The question is, how will you make the determination which page an unauthenticated user should go to?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM