Laravel Admin - Voyager route redirect - php

I have done laravel auth with is_admin field like:
if(auth()->user()->is_admin == 1){
return $next($request);
}
and defined it in route so if the user is admin, can be redirected to voyager dashboard page.
Route::group(['prefix' => 'admin'], function()
{
Voyager::routes();
Route::get('admin', 'HomeController#admin')->middleware('admin');
});
When I login, it's redirected to specific page but with error message:
Target class [VoyagerController] does not exist.
What should I do to return voyager admin page?

You can use middleware admin.user.
Route::group(['prefix' => 'admin'], function()
{
Voyager::routes();
Route::get('admin', 'HomeController#admin')->middleware('admin.user');
});
Hope this will work.
This will:
Access route only when user is logged in.
Redirect to voyager login page when accessing the restricted route.

Related

Laravel Redirect to an specific URL

I am using laravel 5.8 and i want a middleware or some technique that can stops unprivileged user to visit any other URL except an specific URL.
When unprivileged user visit a url, he/she should immediately be redirected to a specific allowed page.
Route::get('/home', 'HomeController#index')->name('home')->middleware(['verified',...]);
In you routes file you should run all routes where only authenticated user should have access through the auth middlware.
Route::group(['middleware' => ['auth']], function() {
Route::get('/home', 'HomeController#index')->name('home');
// more routes
});
To specify where the user should be redirected to you can open the middleware at app/Http/Middlware/Authentication.php and check the method
protected function redirectTo($request)

my laravel admin middleware redirecting to another url

I am working on a project, i want to check if a user is an admin, if he is not an admin, he will be redirected to the login page, but if he is an admin, he will be redirected to the admin page, i initially made it the home url but later changed it to login url, so if the user is an admin it will redirect to the admin url but if his not, it will still he redirection to the home url instead of the login url that i changed it to here is the admin url code
<?php
namespace App\Http\Middleware;
use Closure;
class Admin{
public function handle($request, Closure $next){
if(auth()->user()->isAdmin == 1){
return $next($request);
}
return redirect('login')->with('error','You have not admin access');
}
}
here is the web.php code
Route::get('/', 'PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/dashboard', 'DashboardController#index');
Auth::routes();
Route::get('admin/routes', 'AdminController#index')->middleware('admin');
How can I solve the issue?
Try with this, because you are redirecting user with route.
return redirect()->route('login')->with('error','You have not admin access');
Hope this helps :)
Put route which called after access login in middleware.
Auth::routes();
Route::get('/', 'PagesController#index');
Route::get('/about', 'PagesController#about');
Route::group(['middleware' => 'admin'], function() {
Route::get('/dashboard', 'DashboardController#index');
Route::get('admin/routes', 'AdminController#index')
});

How to prevent to access visited page after logout in laravel using middleware class?

I want to make prevent access to visited page after logout from the laravel project. Here I have used laravel middleware
Route::group(['middleware' => ['web']], function ()
{
Route::get('/logout',[
'uses'=>'UserController#getLogout',
'as'=>'logout'
]);
});
I have included the all the routes in above Route::group route
and used auth facade. I want to prevent to access visited page after logout and after accidentally pressing the back button from the browser.
Laravel Route middleware can be used to allow only authenticated users to access a given route. All you need to do is attach the middleware to a route definition:
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
}]);
Check this Laravel Auth Documentation

Laravel Auth::guest() doesn't work

I have installed authentication using
php artisan make:auth
Before and after loggingin menu shows login and Register link appears even user has logged in. I checked the code below and I found it was correct, there may be problem in Auth::guest(). check the code below
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
I referred some of the questions and they have given some dirty fix. example below.
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'HomeController#index');
});
Actually what this fix does,is force user to login by showing login page.Actually when the user logged in it should hide and show the user name. When I click on home link, it hides and shows username.
Make sure you have the view on the web middleware, use the following code.
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function () { return view('welcome'); });
Route::get('/home', 'HomeController#index');
});

Laravel 5.2 Auth register users only by logged user or by admin only

I have an issue with laravel route. I want that auth register route can be access by only admin or logged users. To achieve this i have removed Route::auth(); from routes.php and create my own route entry in auth middleware.
Effort
Route::group(['middleware' => 'web'], function () {
// Authentication Routes...
$this->get('login', 'Auth\AuthController#showLoginForm');
$this->post('login', 'Auth\AuthController#login');
$this->get('logout', 'Auth\AuthController#logout');
// Password Reset Routes...
$this->get('password/reset/{token?}', 'Auth\PasswordController#showResetForm');
$this->post('password/email', 'Auth\PasswordController#sendResetLinkEmail');
$this->post('password/reset', 'Auth\PasswordController#reset');
Route::get('/home', 'HomeController#index');
Route::post('/ajax/getStates', 'ConfigurationController#getStates');
Route::post('/ajax/getCities', 'ConfigurationController#getCities');
});
Route::group(['middleware' => ['web','auth']], function () {
// Registration Routes...
$this->get('register', 'Auth\AuthController#showRegistrationForm');
$this->post('register', 'Auth\AuthController#register');
});
Above code working fine. when i tried to access register url it simply redirect me to the login page. Now actual problem start after login.
After login, i tried to access register page, but it does not show up, instead it redirect me to the home like http://localhost/.
Please suggest me the solution.
Check your AuthController's constructor. It has the guest middleware assigned to all methods except logout.
I got a simple solution. I am not expert, but I am going by this way and It's working as you want.
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/edit', function () {
if (Auth::user()) {
return view('auth.user_edit'); //Page which you want to show for loged user.
} else {
return "MM"; //You can redirect from here, if user is not logged in
}
});
});

Categories