Dashboard targeting error in Laravel Breeze (involves Middleware with auth) - php

I'm doing some tests with Middleware to prevent the user not logged into the system from accessing pages that only logged in people can access. Apparently when I enter http://127.0.0.1:8000/fighter it returns to http://127.0.0.1:8000/login.
The problem occurs that when logging in I am redirected to http://127.0.0.1:8000/fighter and not to http://127.0.0.1:8000/dashboard which would be the correct.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FighterController;
use App\Http\Controllers\MasterController;
use App\Http\Controllers\DojoController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
// fighter folder
Route::middleware('auth')->group(function(){
Route::controller(FighterController::class)->group(function() {
Route::get('/fighter', 'index')->name('fighter.index');
Route::get('/add-fighter', 'create')->name('fighter.create');
Route::post('/add-fighter', 'store')->name('fighter.store');
Route::get('/update-fighter/{id}', 'edit')->name('fighter.edit');
Route::patch('/update-fighter/{id}', 'update')->name('fighter.update');
Route::delete('/delete-fighter/{id}', 'destroy')->name('fighter.destroy');
});
});
// master folder
Route::middleware('auth')->group(function(){
Route::controller(MasterController::class)->group(function() {
Route::get('/master', 'index')->name('master.index');
Route::get('/add-master', 'create')->name('master.create');
Route::post('/add-master', 'store')->name('master.store');
Route::get('/update-master/{id}', 'edit')->name('fighter.edit');
Route::patch('/update-master/{id}', 'update')->name('fighter.update');
Route::delete('/delete-master/{id}', 'destroy')->name('fighter.destroy');
});
});
// dojo folder
Route::middleware('auth')->group(function(){
Route::controller(DojoController::class)->group(function() {
Route::get('/dojo', 'index')->name('dojo.index');
Route::get('/add-dojo', 'create')->name('dojo.create');
Route::post('/add-dojo', 'store')->name('dojo.store');
Route::get('/update-dojo/{id}', 'edit')->name('dojo.edit');
Route::patch('/update-dojo/{id}', 'update')->name('dojo.update');
Route::delete('/delete-dojo/{id}', 'destroy')->name('dojo.destroy');
});
});
require __DIR__.'/auth.php';

Related

laravel specify the routes for every role

I am trying to set up routing for different roles in my application but I am encountering an error. I want to know if the approach I am using is correct. I would like to specify the routes for each role and I am unsure if my method is the right one to achieve this.
This is my web.php file:
<?php
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//guest pages
Route::get('/', function () {
return redirect()->route('login');
});
Auth::routes();
route::middleware('auth')->group(function () {
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::middleware('hasRole:Super Admin')->prefix('SuperAdmin')->group(function () {
Route::prefix('users')->group(function () {
//users routes
Route::get('', [\App\Http\Controllers\UserController::class, 'index'])->name('users.index');
Route::get('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show');
Route::put('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
Route::delete('/delete_user/{id}', [\App\Http\Controllers\UserController::class, 'destroy'])->name('users.destroy');
Route::get('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'create'])->name('user.create');
Route::post('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'store'])->name('user.store');
});
Route::prefix('fournisseurs')->name('fournisseur.')->group(function () {
//fournisseurs routes
Route::get('', [App\Http\Controllers\FournisseurController::class, 'index'])->name('index');
Route::delete('{id}', [App\Http\Controllers\FournisseurController::class, 'destroy'])->name('destroy');
Route::get('edit_fournisseur/{id}', [\App\Http\Controllers\FournisseurController::class, 'edit'])->name('edit');
Route::put('fournisseurs/{id}', [\App\Http\Controllers\FournisseurController::class, 'update'])->name('update');
Route::get('ajouter_fournisseur', [App\Http\Controllers\FournisseurController::class, 'create'])->name('create');
Route::post('ajouter_fournisseur', [App\Http\Controllers\FournisseurController::class, 'store'])->name('store');
});
Route::prefix('factures')->name('facture.')->group(function () {
//factures routes
Route::get('', [App\Http\Controllers\FactureController::class, 'index'])->name('index');
Route::delete('{id}', [App\Http\Controllers\FactureController::class, 'destroy'])->name('destroy');
Route::get('ajouter_facture', [App\Http\Controllers\FactureController::class, 'create'])->name('create');
Route::post('ajouter_facture', [App\Http\Controllers\FactureController::class, 'store'])->name('store');
Route::get('download/{id}', [App\Http\Controllers\FactureController::class, 'downloadFacture'])->name('downloadFacture');
});
});
Route::middleware('hasRole:Admin')->prefix('Admin')->group(function () {
Route::prefix('users')->group(function () {
//users routes
Route::get('', [\App\Http\Controllers\UserController::class, 'index'])->name('users.index');
Route::get('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show');
Route::put('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
Route::delete('/delete_user/{id}', [\App\Http\Controllers\UserController::class, 'destroy'])->name('users.destroy');
Route::get('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'create'])->name('user.create');
Route::post('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'store'])->name('user.store');
});
Route::prefix('fournisseurs')->name('fournisseur.')->group(function () {
//fournisseurs routes
Route::get('', [App\Http\Controllers\FournisseurController::class, 'index'])->name('index');
});
Route::prefix('factures')->name('facture.')->group(function () {
//factures routes
Route::get('', [App\Http\Controllers\FactureController::class, 'index'])->name('index');
Route::get('download/{id}', [App\Http\Controllers\FactureController::class, 'downloadFacture'])->name('downloadFacture');
});
});
});
And with this solution i get the error message
Optimization failed (See output console for more details)
Can someone help me to find out the solution for this issue or suggest me the right way to do it?
for role management in Laravel I suggest you use "spatie/Laravel-permission".
this is documention link:
https://spatie.be/docs/laravel-permission/v5/introduction
after install this package you must create middleware for each role and then check those on web.php

Prevent Back History middleware not working

I made a prevent back history middleware named RevalidateBackHistory.php, registered into kernel.php as 'revalidate'. This middleware is working on admin dashboard perfectly but when I sign out, it's not working, user can go back to the dashboard. I implemented this middleware on the whole application using Route::group and also tried to implement this on specific routes, but no luck. Thanks in advance.
Here's the code for the middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class RevalidateBackHistory
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* #return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$response->headers->set('Cache-Control','no-cache, no-store, max-age=0, must-revalidate');
$response->headers->set('Cache-Control: post-check=0, pre-check=0', false);
$response->headers->set('Pragma','no-cache');
$response->headers->set('Expires','Sun, 02 Jan 1990 00:00:00 GMT');
return $response;
}
}
Here's my web.php(routes):
<?php
use App\Http\Controllers\AdminController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\StoreController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\ReviewController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\PayPalController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::group(['middleware' => 'revalidate'], function() {
//Store Routes
Route::get('store',[StoreController::class, 'index'])->name('store');
Route::get('/', function () {return redirect()->route('store');});
//Single Product Page
Route::get('show/{id?}',[StoreController::class, 'show'])->name('show');
//Cart Functionalities
Route::get('cart',[CartController::class, 'cart'])->name('cart');
Route::post('add-to-cart', [CartController::class, 'addToCart'])->name('add-to-cart');
Route::get('load-cart-data',[CartController::class, 'cartLoadAjax'])->name('load-cart-data');
Route::post('update-cart-data',[CartController::class, 'updateCartData'])->name('update-cart-data');
//Redirect if already authenticated middleware(Admin)
Route::group(['middleware' => ['auth:web']], function() {
//Checkout
Route::get('checkout',[StoreController::class, 'checkOut'])->name('checkout');
//Create Comment
Route::post('comments/{id}',[ReviewController::class, 'store'])->name('comment');
//Paypal
Route::post('payment', [PayPalController::class, 'payment'])->name('payment');
Route::get('payment.success', [PayPalController::class, 'success'])->name('payment.success');
//getDetails
Route::get('get.details', [UserController::class, 'getDetails'])->name('get.details');
});
//Redirect if already authenticated middleware(Admin)
Route::group(['middleware' => ['guest:web']], function() {
//User Login and Registration
Route::get('UserLogin',[UserController::class, 'UserLogin'])->name('UserLogin');
Route::post('user.login',[UserController::class, 'UserCustomLogin'])->name('user.login');
Route::post('UserRegis',[UserController::class, 'customUserRegistration'])->name('UserRegis');
});
Route::group(['middleware' => ['guest:admin']], function() {
//Admin Register
Route::get('admin_register',[AdminController::class, 'registration'])->name('GetSignUp');
Route::post('post_Register',[AdminController::class, 'customRegistration'])->name('aSignUp');
//Admin Login
Route::get('admin_login',[AdminController::class, 'AdminGetLogin'])->name('login');
Route::post('post_login',[AdminController::class, 'CustomLogin'])->name('aAuth');
});
Route::group(['middleware' => ['auth:admin']], function() {
// define your route, route groups here
//Dashboard
Route::get('dashboard',[AdminController::class, 'dashboard'])->name('dashboard');
//SignOut
Route::get('admin_signout',[AdminController::class, 'signOut'])->name('signout');
//Products
Route::get('add_product',[ProductController::class, 'create'])->name('add_product');
Route::post('save_product',[ProductController::class, 'createProduct'])->name('save_product');
Route::get('view_products',[ProductController::class, 'viewProducts'])->name('view_products');
//New Edit
Route::get('editNew/{id?}',[ProductController::class, 'editNew'])->name('editNew');
//Update Product Data
Route::post('update_product/{id}',[ProductController::class, 'update'])->name('update_product');
//Delete Product
Route::delete('/delete_product/{id}',[ProductController::class, 'destroy'])->name('delete_product');
//Search Product
Route::get('search',[ProductController::class, 'search'])->name('search');
});
});
Can you try and set it as a global middleware in your App\Http\Kernel.php
The application's global HTTP middleware are run during every request to your application.
Hope that helps.

Why am I routed to login form rather than the route that I specified - Laravel

I have a customized Laravel login controller. If I don't have a record in a particular table I want to redirect to a selfRegister page. The login function gets to the code that checks for this record and fails to find it. I then do a redirect, but instead of going to selfregister function on my userController i wind up back at the login form and I have no idea why.
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ActivityController;
use App\Http\Controllers\NoteController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\GetNameController;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Auth::routes();
Route::any('/user/selfregister',[UserController::class, 'selfRegister'])->name('selfRegister');
Route::group(['middleware' => 'auth'], function () {
Route::any('/user/profile', [UserController::class, 'profile']);
.
.
.
From LoginController
public function login(Request $request)
{
if($this->validateLogin($request)){
return $this->sendFailedLoginResponse($request);
}
$req = $request->all();
$app_acc = New Application_access();
$app_acc->user_name = $req['user_name'];
$app_acc->access_date = date('Y-m-d h:i:s');
if ($this->attemptLogin($request)) {
echo('logged in 1<br>');
$app_acc->access_disposition = 'login';
$app_acc->save();
$reset_p = New Reset_password();
echo('logged in 2<br>');
if($reset_p->buildReset(Auth::user()->user_contact_id)){
$user = Users::find(auth()->user()->user_contact_id);
$user->activated = 0;
$user->save();
return redirect('/user/passwordupdate');
};
return $this->sendLoginResponse($request);
} else {
// login failed
// ******
// check if user is in the contact table
$contact = DB::table('contacts_t')->select('*')
->where('con_email',"=",$req['user_name'])
->first();
if (!$contact) {
// if we did not find the contact then redirect to self register
return redirect()->route('selfRegister');
//return redirect()->route('/user/selfregister');
}
return $this->sendLoginResponse($request);
}
return $this->sendFailedLoginResponse($request);
}
From UserController
public function selfRegister() {
die('selfRegister');
.
.
.
From Login.blade.php
.
.
.
<div class="centerLogin">
<form enctype="multipart/form-data" method="POST" action="{{ route('login') }}">
#csrf
<div class="form-group row mt-3">
.
.
.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ActivityController;
use App\Http\Controllers\NoteController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\GetNameController;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/user/selfregister',[UserController::class, 'selfRegister'])->name('selfRegister');
Auth::routes();
Route::group(['middleware' => 'auth'], function () {
Route::any('/user/profile', [UserController::class, 'profile']);
.
.
.
Try like this. Move your route before Auth::routes(); and change it to get.

Laravel: Login screen if not logged in errornbh

First Laravel Project. I want to redirect not logged in users to the login screen.
I configured the auth like in the documentation and found this routes script somewhere:
Route::group(['middleware' => 'auth'], function () {
and pasted it on the beginning of the routes/web.php
But now it drops to the myip/login and got routing error.
How to fix it?
(i know that if I delete it it will be fixed, but I want this feature.)
EDIT:
Route list:
---------------------+----------------+
| Domain | Method | URI | Name | Action | Middleware |
.
.
.
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController#showLoginForm | web,auth,guest |
| | POST | login | | App\Http\Controllers\Auth\LoginController#login | web,auth,guest |
EDIT2: The error message
The site is not properly diverted
Firefox has detected that the server is redirecting the request in a
way that will never be fulfilled.
This problem sometimes occurs because of a disabling or refusing to
accept cookies.
I got almost the same message in Chromium, too
EDIT3: routes/web.php
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
return view('index');
});
//AUTH
Route::get('/login', 'HomeController#index');
.
.
.
My other routes what are working perfectly
}
I am assuming you want guest to visit the index page. If you don't want guest to visit index page put it inside the auth group.
Route::get('/', function () {
return view('index');
});
Route::get('/login', 'HomeController#index');
Route::group(['middleware' => 'auth'], function () {
//AUTH
.
.
.
My other routes what are working perfectly
}
You need to put your index and login route outside the group.
Edit ::
Route::get('/', function () {
return view('index');
});
Route::auth(); // If you have this line, place it above the `auth` middleware.
Route::group(['middleware' => 'auth'], function () {
//AUTH
.
.
.
My other routes what are working perfectly
}
In your login form change route like this :
<form class="form-horizontal" role="form" method="POST" action="{{ route('login') }}">

Can't access Laravel admin panel

I'm new to the laravel framework. I bought a project and I installed it but when I try to login to the admin panel localhost/admin/home it redirects me to the first page. Why is this? Is the problem in the authentication?
The template is adminLTE.
This is routes.php:
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::group(['middleware' => ['web']], function () {
Route::auth();
Route::get('/', function () {
// $user = Auth::user();
// if($user->id == 2){
return view('welcome');
/* }else{
echo "Site En cours de mise a jours .. Veuillez patienter quelques minutes";
die();
}*/
});
Route::post('/post_login', 'HomeController#post_login');
Route::get('/admin/home', 'AdminController#index');
Route::get('/contact', 'HomeController#contact');
});
Route::group(['middleware' => 'web'], function () {
Route::get('/load_coup/{code}', 'HomeController#load_coup');
Route::get('/get_coup_div/{code}', 'HomeController#get_coup_div');
Route::get('/reset_coupon_div', 'HomeController#reset_coupon_div');
Route::get('/validate_coup/{price}/{type}', 'HomeController#validate_coup');
Route::get('/back', 'HomeController#back');
Route::get('/save/{price}', 'HomeController#save');
Route::get('/get_solde', 'HomeController#get_solde');
Route::get('/bet_list', 'HomeController#bet_list');
Route::get('/bet_list/{code}', 'HomeController#bet_details');
Route::get('/coupon/{id}', 'HomeController#print_coupon');
Route::get('/transaction/', 'HomeController#add_transaction');
Route::post('/transaction/', 'HomeController#store_transaction');
Route::get('/transaction_list/', 'HomeController#transaction_list');
Route::get('/profile/', 'HomeController#profile');
Route::post('/profile/', 'HomeController#edit_profile');
});
Route::group(['prefix' => 'admin', 'middleware' => ['web']], function () {
//Route::auth();
Route::get('/users', 'AdminController#user');
Route::get('/users/{id}', 'AdminController#edit_user');
Route::post('/update_user/', 'AdminController#update_user');
Route::post('/add_transaction/', 'AdminController#add_transaction');
Route::get('/match', 'AdminController#match');
//Route::post('/match', 'AdminController#match');
Route::get('/coupon', 'AdminController#coupon');
Route::get('/coupons/{id}', 'AdminController#edit_coupon');
});
Looks like you don't have admin or correction role. Because of that you are redirected to /.
Look at the post_login function in the HomeController there will be a return view or return redirect. You can change it to go to the view you wish.

Categories