I want to make a login system in Laravel. I have a login page and a dashboard. Before accessing the dashboard, I want to check if the admin is logged in. Everything works fine but the only problem is if a user try to access the dashboard directly from the URL (like http://localhost/myproject/dashboard ), I want the system to check if the session is set or not. If it is set, load the page. If not, redirect it to the login page with a message. I have written the checking code in master.blade.php file to see if session is set or not
This is my Login Page with the form (view):
<div class="container">
@if (session('status'))
<div class="alert alert-danger fade in">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<i class=""></i> <strong align="center">{{session('status')}}</strong>
</div>
@endif
<form class="form-signin" action="auth_admin" method="post">
<h2 class="form-signin-heading">admin login</h2>
<div class="login-wrap">
<input type="text" class="form-control" name="admin_email" placeholder="Email ID" autofocus>
<input type="password" class="form-control" name="admin_password" placeholder="Password">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<label class="checkbox">
<span class="pull-right"> <a href="#"> Forgot Password?</a></span>
</label>
<button class="btn btn-lg btn-login btn-block" type="submit">LOGIN</button>
</div>
</form>
</div>
This is my Route:
Route::get('webadmin','Admin_controller@index');
Route::get('adminlogin','Admin_controller@admin_login_page');
Route::post('auth_admin','Admin_controller@auth_admin');
This is my Controller :
<?php
namespace App\Http\Controllers;
use App\General_model;
use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use DB;
class Admin_controller extends Controller
{
public function index()
{
return view('adminpanel.index');
}
public function admin_login_page()
{
return view('adminpanel.loginpage');
}
public function auth_admin(Request $request)
{
$admin_email = $request->input('admin_email');
$admin_password = md5($request->input('admin_password'));
$checklogin = DB::table('admin_login')->select('admin_id','admin_email','admin_name')->where(['admin_email'=>$admin_email,'admin_password'=>$admin_password])->first();
if(count($checklogin) > 0)
{
$request->session()->put('admin_id',$checklogin->admin_id);
$request->session()->put('admin_name',$checklogin->admin_name);
$request->session()->put('admin_email',$checklogin->admin_email);
return redirect()->action('Admin_controller@index');
} else {
return redirect()->action('Admin_controller@admin_login_page')->with('status','Incorrect Email ID or Password');
}
}
}
This is my master.blade.php (view)
<!DOCTYPE html>
<html lang="en">
<head>
@include('adminpanel.template.header')
<?php
if(!Session::has('admin_email') && !Session::has('admin_name')) {
return Redirect::to('Admin_controller@admin_login_page')->with('status','Please login to continue!');
}
?>
</head>
<body>
<section id="container" class="">
<!--top bar start-->
<header class="header white-bg">
@include('adminpanel.template.top_bar')
</header>
<!--top bar end-->
<!--sidebar start-->
<aside>
@include('adminpanel.template.sidebar')
</aside>
<!--sidebar end-->
<!--main content start-->
<section id="main-content">
<section class="wrapper">
@yield('main_content')
</section>
</section>
<!--main content end-->
</section>
@include('adminpanel.template.footer')
</body>
</html>
add thi method
public function __construct()
{
$this->middleware("auth");
}
in your controller
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.