Commit dc613d61 by martinus

Menambahkan Controller,membuat middleware guna untuk meverifikasi admin atau user

parent 256e9de6
......@@ -26,7 +26,7 @@ class LoginController extends Controller
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
......
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class AuthController extends Controller
{
//
public function postLogin(Request $request){
if(!\Auth::attempt(['email'=>$request->username,'password' => $request->password])){
return redirect()->back();
}
else if(\Auth::attempt(['email'=>$request->username,'password' => $request->password]) && \Auth::user()->role == 1)
return redirect()->route('admin');
else if(\Auth::attempt(['email'=>$request->username,'password' => $request->password]) && \Auth::user()->role == 2)
return redirect()->route('user');
}
public function postRegister(Request $request){
$this->validate($request,[
'username' =>'required|min:4',
'email'=>'required|email|unique:users',
'password'=>'required|min:6|confirmed'
]);
$user = User::create([
'firstname' => $request->namadepan,
'lastname' => $request->namabelakang,
'gender' => $request->gender,
'username' => $request->username,
'email'=> $request->email,
'password'=>bcrypt($request->password),
'role'=> $request->role,
]);
//user login
\Session::flash('sukses','Anda Sudah bisa login sekarang');
return redirect()->route('back');
}
public function login(){
return view('index');
}
public function register(){
return view('index');
}
public function logout(){
\Auth::logout();
return redirect()->route('login');
}
}
......@@ -60,6 +60,7 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'role' => \App\Http\Middleware\Role::class,
];
/**
......
<?php
namespace App\Http\Middleware;
use Closure;
class Role
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$user = \Auth::user();
if(\Auth::check()){
if($user->role == 1){
return $next($request);
}
if($user->role == 2){
return $next($request);
}
}else{
return redirect()->route('home');
}
}
}
......@@ -3,7 +3,7 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
......@@ -24,5 +24,6 @@ class AppServiceProvider extends ServiceProvider
public function boot()
{
//
Schema::defaultStringLength(191);
}
}
......@@ -16,7 +16,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'username', 'email', 'password','firstname','lastname','gender','role'
];
/**
......
......@@ -15,9 +15,12 @@ class CreateUsersTable extends Migration
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('firstname');
$table->string('lastname');
$table->string('gender');
$table->string('username');
$table->integer('role');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
......
@extends('layout.layout')
@section('title','Dashboar Admin')
@section('container')
Selamat Datang Admin
@endsection
<h2>Selamat datang {{Auth::user()->firstname}}</h2>
<a href="{{route('logout')}}"> Logout </a>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<title>@yield('title')</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{{asset('/css/bootstrap.min.css')}}">
<script src="{{asset('/js/jquery.min.js')}}"></script>
<script src="{{asset('/js/bootstrap.min.js')}}"></script>
<script src="{{asset('/js/popper.js')}}"></script>
<style type="text/css">
body {
background-image: url('{{ asset('/image/bg2.jpg')}}');
}
</style>
</head>
<body>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div>
<img src="{{asset('/image/logo.png')}}" alt="">
</div>
@yield('container')
\ No newline at end of file
@extends('layout.layout')
@section('title','Dashboard User')
@section('container')
Selamat datang User
<a href="{{route('logout')}}"> Logout </a>
@endsection
\ No newline at end of file
......@@ -11,6 +11,25 @@
|
*/
use Illuminate\Support\Facades\Auth;
Route::get('/', function () {
return view('index');
});
$result = 0;
return view('index',['result'=>$result]);
})->name('back');
Route::get('/login','AuthController@login')->name('login')->middleware('guest');
Route::get('/register','AuthController@register')->name('register')->middleware('guest');
Route::post('/login','AuthController@postLogin')->middleware('guest');
Route::post('/register','AuthController@postRegister')->middleware('guest');
// Route::get('/home',function(){
// return view('home');
// })->middleware('auth')->name('home');
Route::get('/logout','AuthController@logout')->middleware('auth')->name('logout');
Route::get('/admin',function(){
return view('admin');
})->middleware('role')->name('admin');
Route::get('/user',function(){
return view('user');
})->middleware('role')->name('user');
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment