1. 스키마생성
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('phone');
$table->string('usertype')->nullable();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
2. 미들웨어 추가
php artisan make:middleware AdminMiddleware
app/Http/Middleware/AdminMiddleware.php
유저타입이 'admin' 이면 계속
아니면 홈으로 리다이렉트 'status'값을 같이 보낸다.
public function handle($request, Closure $next)
{
if(Auth::user()->usertype == 'admin')
{
return $next($request);
}else{
return redirect('/home')->with('status', 'You are Not Allowed To Adimin Dashboard');
}
}
3. Kernel Middleware추가
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'admin' => \App\Http\Middleware\AdminMiddleware::class,
];
4. app/Http/Controllers/Auth/LoginController.php
로그인 유저 타입이 'admin' 이면 호스트/dashboard
아니면 호스트/home 로
use Illuminate\Support\Facades\Auth;
protected function redirectTo()
{
if(Auth::user()->usertype == 'admin')
{
return 'dashboard';
}else{
return 'home';
}
}
5. 라우터 설정 (미들웨어설정 auth,admin 만 접속가능하다)
Route::group(['middleware' => ['auth', 'admin']], function(){
Route::get('/dashboard', function(){
return view('admin.dashboard');
});
});
admin 유저로 로그인 했을경우
일반유저로 로그인 했을 경우