Commit dc613d61 by martinus

Menambahkan Controller,membuat middleware guna untuk meverifikasi admin atau user

parent 256e9de6
<?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 ...@@ -60,6 +60,7 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::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 @@ ...@@ -3,7 +3,7 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
/** /**
...@@ -24,5 +24,6 @@ class AppServiceProvider extends ServiceProvider ...@@ -24,5 +24,6 @@ class AppServiceProvider extends ServiceProvider
public function boot() public function boot()
{ {
// //
Schema::defaultStringLength(191);
} }
} }
...@@ -16,7 +16,7 @@ class User extends Authenticatable ...@@ -16,7 +16,7 @@ class User extends Authenticatable
* @var array * @var array
*/ */
protected $fillable = [ protected $fillable = [
'name', 'email', 'password', 'username', 'email', 'password','firstname','lastname','gender','role'
]; ];
/** /**
......
...@@ -15,9 +15,12 @@ class CreateUsersTable extends Migration ...@@ -15,9 +15,12 @@ class CreateUsersTable extends Migration
{ {
Schema::create('users', function (Blueprint $table) { Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id'); $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->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password'); $table->string('password');
$table->rememberToken(); $table->rememberToken();
$table->timestamps(); $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> @extends('layout.layout')
<html lang="en"> @section('title','Dashboard')
<head> @section('container')
<title>Coba</title> @if($message = \Session::get('sukses'))
<meta charset="utf-8"> <div class="alert alert-success alert-block">
<meta name="viewport" content="width=device-width, initial-scale=1"> <strong>{{$message}}</strong>
<link rel="stylesheet" href="{{asset('/css/bootstrap.min.css')}}"> </div>
<script src="{{asset('/js/jquery.min.js')}}"></script> @endif
<script src="{{asset('/js/bootstrap.min.js')}}"></script>
<script src="{{asset('/js/popper.js')}}"></script>
<style type="text/css">
</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>
<div class="container register"> <div class="container register">
<div class="row"> <div class="row">
<div class="col-md-7 register-left"> <div class="col-md-7 register-left">
...@@ -38,33 +24,38 @@ ...@@ -38,33 +24,38 @@
<div class="tab-content" id="myTabContent" style="background:white;box-sizing:border-box; border:2px solid white;"> <div class="tab-content" id="myTabContent" style="background:white;box-sizing:border-box; border:2px solid white;">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab"> <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<br> <br>
<form action="/login" method="post">
{{ csrf_field() }}
<div class="row register-form"> <div class="row register-form">
<div class="col-md-12"> <div class="col-md-12">
<div class="form-group"> <div class="form-group">
<input type="text" class="form-control" placeholder="Nama Pengguna"> <input type="text" name="username" class="form-control" placeholder="Nama Pengguna">
</div> </div>
</div> </div>
<div class="col-md-12"> <div class="col-md-12">
<div class="form-group"> <div class="form-group">
<input type="text" class="form-control" placeholder="Kata Sandi"> <input type="password" name="password" class="form-control" placeholder="Kata Sandi">
</div> </div>
</div> </div>
<div class="col-md-12"> <div class="col-md-12">
<input type="submit" class="form-control btn btn-primary"> <input type="submit" class="form-control btn btn-primary">
</div> </div>
</div> </div>
</form>
</div> </div>
<div class="tab-pane fade show" id="profile" role="tabpanel" aria-labelledby="profile-tab"> <div class="tab-pane fade show " id="profile" role="tabpanel" aria-labelledby="profile-tab">
<br> <br>
<form action="/register" method="POST">
{{ csrf_field() }}
<div class="row register-form"> <div class="row register-form">
<div class="col-md-6"> <div class="col-md-6">
<div class="form-group"> <div class="form-group">
<input type="text" class="form-control" placeholder="Nama Depan *" value="" /> <input type="text" name="namadepan" class="form-control" placeholder="Nama Depan *" value="{{old('namadepan')}}"/>
</div> </div>
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
<div class="form-group"> <div class="form-group">
<input type="text" class="form-control" placeholder="Nama Belakang *" /> <input type="text" name="namabelakang" class="form-control" placeholder="Nama Belakang *" value="{{old('namabelakang')}}"/>
</div> </div>
</div> </div>
<div class="col-md-12"> <div class="col-md-12">
...@@ -86,28 +77,49 @@ ...@@ -86,28 +77,49 @@
</div> </div>
<div class="col-md-12"> <div class="col-md-12">
<div class="form-group"> <div class="form-group">
<input type="text" placeholder="Email" class="form-control"> <input type="email" name="email" placeholder="Email" class="form-control {{$errors->has('email')?'is-invalid':''}}" value="{{old('email')}}">
@if($errors->has('email'))
<div class="invalid-feedback">
{{$errors->first('email')}}
</div>
@endif
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="text" name="username" placeholder="Username" class="form-control {{$errors->has('username')?'is-invalid':''}}" value="{{old('username')}}">
@if($errors->has('username'))
<div class="invalid-feedback">
{{$errors->first('username')}}
</div>
@endif
</div> </div>
</div> </div>
<div class="col-md-12"> <div class="col-md-12">
<div class="form-group"> <div class="form-group">
<input type="text" placeholder="Username" class="form-control"> <input type="password" name="password" placeholder="Kata Sandi" class="form-control {{$errors->has('password')?'is-invalid':''}}" required>
@if($errors->has('password'))
<div class="invalid-feedback">
{{$errors->first('password')}}
</div>
@endif
</div> </div>
</div> </div>
<div class="col-md-12"> <div class="col-md-12">
<div class="form-group"> <div class="form-group">
<input type="text" placeholder="Kata Sandi" class="form-control"> <input type="password" name="password_confirmation" placeholder="Konfirmasi Kata Sandi" class="form-control" required>
</div> </div>
</div> </div>
<div class="col-md-12"> <div class="col-md-12">
<div class="form-group"> <div class="form-group">
<input type="text" placeholder="Konfirmasi Kata Sandi" class="form-control"> <input type="number" name = "role" min="1" max="4">
</div> </div>
</div> </div>
<div class="col-md-12"> <div class="col-md-12">
<input type="submit" class="form-control btn btn-primary"> <input type="submit" class="form-control btn btn-primary">
</div> </div>
</div> </div>
</form>
</div> </div>
</div> </div>
</div> </div>
...@@ -115,7 +127,4 @@ ...@@ -115,7 +127,4 @@
</div> </div>
</div> </div>
</body> @endsection
<script> \ No newline at end of file
</script>
</html>
<!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 @@ ...@@ -11,6 +11,25 @@
| |
*/ */
use Illuminate\Support\Facades\Auth;
Route::get('/', function () { 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