Redirect from web routes using can - laravel

By default on using can if not authorized, an exception is thrown. I want to redirect instead of showing this exception.
web.php
Route::group(['middleware' => 'auth'], function(){
Route::get('/users', [userController::class, 'index'])->can('view', 'user');
});
The exception is executed from response.php:
public function authorize()
{
if ($this->denied()) {
throw (new AuthorizationException($this->message(), $this->code()))
->setResponse($this);
}
return $this;
}
I tried to redirect from there but it's not working:
public function authorize()
{
if ($this->denied()) {
return redirect('/');
}
return $this;
}
UserPolicy.php:
public function view(User $user)
{
return $user->hasPermission('user-view');
}
is it possible to redirect instead of throwing this exception or I have to check from the controller and redirect from there?

Related

Having problem with authentication via middleware and also page not found in laravel

**I just want to know what did i do wrong. Below are the code. Whenever I try to excess
127.0.0.1::8000/admin/profile
without login, it shows me error rather than redirecting me to login page as I have used middleware so it should not show me error but redirect me to admin login page which is not happening here. Also after when i logged in and try to excess
127.0.0.1::8000/admin/profile/edit/{id} it show 404 page not found. why?*
Route::get('/', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Route::prefix('admin')->name('admin.')->group(function(){
Route::middleware('admin')->group(function(){
Route::get('/dashboard', [AdminController::class, 'displayDashboard'])->name('dashboard');
Route::get('/logout', [AdminController::class, 'logout'])->name('logout');
Route::get('profile', [AdminProfileController::class, 'displayProfile'])->name('admin.profile');
Route::get('profile/edit/{id}', [AdminProfileController::class, 'editProfile'])->name('profile.edit');
});
Route::get('/login', [AdminController::class, 'login'])->name('login')->middleware('guest:admin');
Route::post('/login', [AdminController::class, 'loginvalidate'])->name('loginvalidate')->middleware('guest:admin');
});
AdminProfileController.blade.php
class AdminProfileController extends Controller
{
public function displayProfile()
{
$loggedinUser = Auth::guard('admin')->user();
return view('admin.profile.profile_master', compact('loggedinUser'));
}
public function editProfile($id)
{
return view('admin.profile.profile_edit');
}
}
Admin middleware
public function handle(Request $request, Closure $next)
{
if(!Auth::guard('admin')->check())
{
return redirect()->route('admin.login');
}
return $next($request);
}

Beginner in Laravel not working my routes inside middleware guest, and middleware auth all routes working well

This is all my routes inside web.php
Route::get('/', function(){
return view('welcome-body');
});
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('userdashboard')->middleware('auth');
// Route::post('logout', 'Auth\LoginController#logout')->name('logout');
// Route::get('logout', 'Auth\LoginController#logout');
Route::get('logout','Auth\LoginController#logout');
Route::group(['middleware' => ['guest']], function ()
{
// About dropdown pages route
Route::get('/office-mayor', 'WebController#officemayor_about_page');
Route::get('/organizational-chart', 'WebController#organizationalchartpage');
Route::get('/directory', 'WebController#directorypage');
Route::get('/barangays', 'WebController#barangayspage');
Route::get('/barangays-detail', 'WebController#barangays_detail_page');
Route::get('/history', 'WebController#historypage');
// Stories & News dropdown pages route
Route::get('/stories', 'WebController#storiespage');
Route::get('/stories-detail', 'WebController#storiesdetailspage');
Route::get('/news', 'WebController#newspage');
Route::get('/news-detail', 'WebController#newsdetailspage');
Route::get('/gallery', 'WebController#gallerypage');
Route::get('/videos', 'WebController#videospage');
Route::get('/bids-and-awards', 'WebController#bidsandawardspage');
// Transparency route
Route::get('/transparency', 'WebController#transparencypage');
// Covid-19 update route
Route::get('/covid-updates', 'WebController#covidupdatespage');
});
Route::group(['middleware' => ['auth']], function ()
{
Route::get('authors/table', 'AllSystemController#indexauthorsdata')->name('authors.table');
Route::get('/authors/add', 'AllSystemController#createviewauthors')->name('create.authors');
Route::post('/authors/authorsdata/submit', 'AllSystemController#submitauthors')->name('submit.authors.data');
Route::get('/authors/view/{id}', 'AllSystemController#showviewauthors')->name('view.authors.data');
Route::get('/authors/edit/{id}', 'AllSystemController#editviewauthors')->name('edit.authors');
Route::patch('/authors/{id}', 'AllSystemController#updateauthordata')->name('update.authors.data');
Route::get('stories/table', 'AllSystemController#indexstoriesdata')->name('stories.table');
Route::get('/stories/add', 'AllSystemController#createviewstories')->name('create.stories');
Route::post('/stories/storiesdata/submit', 'AllSystemController#submitstories')->name('submit.stories.data');
Route::get('/stories/view/{id}', 'AllSystemController#showviewstories')->name('view.story.data');
Route::get('/stories/edit/{id}', 'AllSystemController#editviewstories')->name('edit.stories');
Route::patch('/stories/{id}', 'AllSystemController#updatestorydata')->name('update.story.data');
Route::get('activities/table', 'AllSystemController#indexactivitiesdata')->name('activities.table');
Route::get('/activities/add', 'AllSystemController#createviewactivities')->name('create.activities');
Route::post('/activities/activitiesdata/submit', 'AllSystemController#submitactivities')->name('submit.activities.data');
Route::get('/activities/view/{id}', 'AllSystemController#showviewactivities')->name('view.activity.data');
Route::get('/activities/edit/{id}', 'AllSystemController#editviewactivities')->name('edit.activities');
Route::patch('/activities/{id}', 'AllSystemController#updateactivitydata')->name('update.activity.data');
Route::get('blogs/table', 'AllSystemController#indexblogsdata')->name('blogs.table');
Route::get('/blogs/add', 'AllSystemController#createviewblogs')->name('create.blogs');
Route::post('/blogs/blogsdata/submit', 'AllSystemController#submitblogs')->name('submit.blogs.data');
Route::get('/blogs/view/{id}', 'AllSystemController#showviewblogs')->name('view.blog.data');
Route::get('/blogs/edit/{id}', 'AllSystemController#editviewblogs')->name('edit.blogs');
Route::patch('/blogs/{id}', 'AllSystemController#updateblogdata')->name('update.blog.data');
Route::get('news/table', 'AllSystemController#indexnewsdata')->name('news.table');
Route::get('/news/add', 'AllSystemController#createviewnews')->name('create.news');
Route::post('/news/newsdata/submit', 'AllSystemController#submitnews')->name('submit.news.data');
Route::get('/news/view/{id}', 'AllSystemController#showviewnews')->name('view.new.data');
Route::get('/news/edit/{id}', 'AllSystemController#editviewnews')->name('edit.news');
Route::patch('/news/{id}', 'AllSystemController#updatenewdata')->name('update.new.data');
Route::post('/storiesgallery/submit', 'AllSystemController#submitstoriesgallery')->name('add.stories.galleries');
Route::post('/activitiesgallery/submit', 'AllSystemController#submitactivitiesgallery')->name('add.activities.galleries');
Route::post('/blogsgallery/submit', 'AllSystemController#submitblogsgallery')->name('add.blogs.galleries');
Route::post('/newsgallery/submit', 'AllSystemController#submitnewsgallery')->name('add.news.galleries');
Route::get('contact_us', 'AllSystemController#indexcontactus')->name('sending.mail');
Route::post('contact_us/submit', 'AllSystemController#submitcontactus')->name('sending.mail');
});
and this is my Controller WebController
public function __contruct()
{
$this->middleware('guest');
}
// TOP BAR PAGES
public function officemayor_about_page()
{
return view('web-routes.about.office-mayor');
}
public function organizationalchartpage()
{
return view('web-routes.organizational.organizational-chart');
}
public function directorypage()
{
return view('web-routes.about.directory');
}
public function barangayspage()
{
return view('web-routes.about.barangays');
}
public function barangays_detail_page()
{
return view('web-routes.about.barangays-detail');
}
public function historypage()
{
return view('web-routes.about.history');
}
public function storiespage()
{
return view('web-routes.stories.stories');
}
public function storiesdetailspage()
{
return view('web-routes.stories.stories-detail');
}
public function newspage()
{
return view('web-routes.news.news');
}
public function newsdetailspage()
{
return view('web-routes.news.news-detail');
}
public function gallerypage()
{
return view('web-routes.gallery.gallery');
}
public function videospage()
{
return view('web-routes.video.videos');
}
public function bidsandawardspage()
{
return view('web-routes.bids_awards.bids-and-awards');
}
public function officemayorpage()
{
return view('web-routes.office-mayor.office-mayor');
}
public function covidupdatespage()
{
return view('web-routes.covid-updates.covid-updates');
}
public function transparencypage()
{
return view('web-routes.transparency.transparency');
}
if i want to go in the page example /office-mayor its return like this
error:
This page isn’t working right now localhost can't currently handle this request.
HTTP ERROR 500
I already use config:cache, route:cache and dump-autoload but doesn`t work it
How can i fix this...
I think this is happening because you have misspelled constructor it should be like public function __construct() and you don't have to define middleware in your constructor because you have already used it in routes.

Laravel fails to redirect when specifying URL

I want Laravel to redirect to '/dashboard' instead, Laravel keeps taking me to '/' after login.
LoginController.php
public function redirectPath()
{
return '/dasboard';
}
Middleware
if (Auth::guard($guard)->check())
{
return redirect()->intended('/dashboard');
}
protected function authenticated()
{
return redirect('/home');
}

My laravel login route leads to a blank page with the same URL

My laravel route fails to go to the specified route. I have made a custom authentication page that should lead to dashboard.blade.php when a registered user logs in. But it returns a blank page with the same URL for login when a user uses wrong credentials.
This is my route list:
This is my form tag for the login form
<form method="post" action="{{route('login.store')}}">
Controller store() method:
public function store(Request $request) {
$credentials = $request->only('employeeID', 'password');
if (Auth::attempt($credentials)) {
Session::flash('login','login Successful!');
return view('dashboard');
}
}
Controller index() method:
public function index() {
return view('login');
}
This web.php source code:
Route::get('/', function () {
return view('register');
});
Route::resource('register', 'registerController');
Route::get('/login',function(){
return view('login');
});
Route::resource('login', 'loginController');
Route::get('/dashboard',function(){
return view('dashboard');
});
Route::resource('/logout', 'logoutcontroller#logout');
Route::resource('/dashboard', 'dashboardcontroller#index');
DashboardController source code:
public function index() {
return view('dashboard');
}

Laravel After Logging in is going to / Route

This is mine LoginController
protected $redirectTo;
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
if(Auth::check() && Auth::user()->role->id == 1){
$this->redirectTo=route('admin.dashboard');
}
else{
$this->redirectTo=route('user.dashboard');
}
$this->middleware('guest')->except('logout');
}
}
Here is mine web.php
Route::get('/', function () {
return view('welcome');
})->name('home');
Auth::routes();
I don't know Why after Successfully logging in its going to / route i want to send it to another route which after logging in i manually Enter the route i successfully viewed that route if i am logged in its not going to show but how can i manage after logging in route
You are doing it wrong. You shouldn't put such code into controller constructor because it won't probably work.
Instead in your controller you should define custom redirectTo method:
protected function redirectTo()
{
if(Auth::check() && Auth::user()->role->id == 1) {
return route('admin.dashboard');
}
return $this->redirectTo=route('user.dashboard');
}
This should work because in Illuminate/Foundation/Auth/RedirectsUsers.php trait there is method:
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
defined that is used later in LoginController by default when user was succesfully logged.

Resources