Laravel Login & Logout Using Session & Middleware
Create Middleware : LoginMiddleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class LoginMiddleware
{
public function handle(Request $request, Closure $next)
{
if(!$user = Session::get('user')){
return redirect('login');
}else {
return $next($request);
}
}
}
Now Register Middleware in App/Http/kernal.php
protected $routeMiddleware = [
'loginAuth' => \App\Http\Middleware\LoginMiddleware::class,
];
Now In Web.php
Route::get('/','LoginController@index')->name('login');
Route::get('/login','LoginController@index')->name('user-login');
Route::get('/logout','LoginController@logout')->name('logout');
Route::post('login-process','LoginController@loginProcess')->name('login-process');
Route::middleware(['loginAuth'])->group(function () {
//add Auth code..
}
Now create Login Controller :
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
class LoginController extends Controller
{
public function index(){
return view('login');
}
public function loginProcess(Request $request){
$validator = validator::make($request->all(), [
'email' => 'present|nullable',
'password' => 'required',
'name' => 'present|nullable',
//"email" => "required|exists:user,email",
]);
if ($validator->fails()) {
return response()->json("error", 404);
}
// return Hash::make($request->password);
if(!$user = User::orWhere('email',$request->user)->orWhere('name',$request->user)->first())
return redirect()->back()->with('ErrorMessage', 'User Not in our Database');
if(Hash::check($request->password, $user->password)){
Session::put('user',['id'=>$user->id,'name'=>$user->name,'email'=>$user->email]);
return redirect('dashboard')->with('SuccessMessage', 'Login Successfully');
// return "login";
}else {
return redirect()->back()->with('ErrorMessage', 'Wrong Password');
}
}
public function logout(){
Session::flush();
return redirect()->route('login')->with('SuccessMessage','Logut Successfully');
}
public function register(Request $request){
// dd($request->all());
$employeeId = Employee::create($request->all());
$path = 'profile-image/'.$employeeId->id;
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
$files=$request->file('image');
$name=$files->getClientOriginalName();
$files->move($path,$name);
$employee = Employee::where('id',$employeeId->id)->first();
$employee->image = $path."/".$name ;
$employee->save();
return redirect()->route('get-all-Employee')->with('SuccessMessage', 'Employee created Successfully');
}
}
Now Create Register From
<form action="{{route('add-Employee')}}" method="post" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-4">
<div class="form-group">
<label for="">Email Address</label>
<input type="email" class="form-control" name="email" placeholder="Enter Email Address" required>
</div>
</div>
<div class="col-4">
<div class="form-group">
<label for="">Password</label>
<input type="password" class="form-control" name="Password" placeholder="Enter Password" required>
</div>
</div>
<div class="col-4">
<div class="form-group">
<label for="">Company Logo</label>
<input type="file" class="form-control dropify" name="company_logo" data-allowed-file-extensions="png jpg jpeg">
</div>
</div>
<button type="submit" class="btn btn-success">Submit</button>
<button type="button" class="btn btn-danger pull-left" data-dismiss="modal">Close</button>
</div>
</form>
Ready For Use...........
Comments
Post a Comment