-
Couldn't load subscription status.
- Fork 3
Setup Information
If you use Eloquent-Models this is how it works:
1.) Install the package and add the ServiceProvider to the app.php like mentioned in the readme.md
2.) Add two models (for example: Admin and User)
class Admin extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
protected $table = 'admin_users'; //change this to your table name
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
}
and
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
protected $table = 'users'; //change this to your table name
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
}
3.) Update your config/auth.php
<?php
return [
'default' => 'user',
'multi' => [
'admin' => [
'driver' => 'eloquent',
'model' => App\Models\AdminUser::class,
],
'user' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
]
],
...
];
4.) Create a Controller extending the default AuthController so you are able to change the $redirectPath and $loginPath for the admin auth. Also extending the default PasswordController to change the $redirectTo
namespace App\Http\Controllers\Auth;
class AdminAuthController extends AuthController
{
protected $redirectPath = '/admin/dashboard';
protected $loginPath = '/admin/auth/login';
}
and
namespace App\Http\Controllers\Auth;
class AdminPasswordController extends PasswordController
{
protected $redirectTo = '/admin/dashboard';
}
5.) Update your app/Http/routes.php (just 1 way you can do it)
$router->group(['auth' => 'user'], function() use ($router)
{
$router->get('dashboard', function() {
return 'User - Logged in as '.Auth::user()->email.' <a href="/auth/logout">Logout</a>';
});
$router->controller('auth', 'Auth\AuthController');
$router->controller('password', 'Auth\PasswordController');
});
$router->group(['prefix' => 'admin', 'auth' => 'admin'], function() use ($router)
{
$router->get('dashboard', function() {
return 'Admin - Logged in as '.Auth::user()->email.' <a href="/admin/auth/logout">Logout</a>';
});
$router->controller('auth', 'Auth\AdminAuthController');
$router->controller('password', 'Auth\AdminPasswordController');
});
6.) Now you have to change the Post-Path of the form for the login by the current auth used. Here is an example how the resources/views/auth/login.blade.php can look like
<?php
$authName = Auth::currentType();
if($authName == 'user') {
$url = '/auth/login';
} else {
$url = '/admin/auth/login';
}
?>
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="POST" action="{{ $url }}">
{!! csrf_field() !!}
<div>
Email
<input type="email" name="email" value="{{ old('email') }}">
</div>
<div>
Password
<input type="password" name="password" id="password">
</div>
<div>
<input type="checkbox" name="remember"> Remember Me
</div>
<div>
<button type="submit">Login</button>
</div>
</form>