Laravel always takes me to login page - laravel

I'm using Laravel 5's auth module for my app. However after I created the auth functions with php artisan make:auth I can only access two paths:/login and /register no matter how I add routes in the routes.php. All other paths redirect me to the login page. How can I enable users to access certain paths without logging? Thanks.
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::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get('/patient', 'HomeController#registerPatient');
Route::get('test', 'HomeController#index');
Route::group(array('before' =>'auth'), function()
{
Route::get('about', array('as' => 'about','uses' => 'HomeController#about'));
}
);
Route::group(array('before' =>'auth'), function()
{
Route::get('/physician', array('as' => 'physician','uses' => 'HomeController#registerPhysician'));
}
);

Can you share your controller code? i mean do you have the auth middleware in ur controller construct?
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', 'HomeController#index');
Route::get('/patient', 'HomeController#registerPatient');
Route::get('test', 'HomeController#index');
});
Route::group(array('before' =>'auth'), function()
{
Route::get('/physician', array('as' => 'physician','uses' => 'HomeController#registerPhysician'));
Route::get('about', array('as' => 'about','uses' => 'HomeController#about'));
}
);
anyway check ur construct method in the homecontrollerand apply the auth middlewere only to certain methods
exemple
public function __construct()
{
$this->middleware('auth', ['except' => [
'about',
'index',
]]);
}
}

Related

Laravel add custom middleware to route group

in my web application i have admin panel and i'm trying to make access to users that they have admin role by this code:
namespace App\Http\Middleware;
use Closure;
class CheckUserAdminRole
{
public function handle($request, Closure $next)
{
if (auth()->check()) {
if (auth()->check() && !auth()->user()->hasRole('admin')) {
auth()->logout();
return redirect(route('system.messages','userIsNotAdmin'));
}
}
return $next($request);
}
}
and in my routs i have this route group:
Route::group(['namespace' => 'Dashboard', 'middleware' => ['auth:web'], 'prefix' => 'dashboard'], function () {
$this->group(['prefix' => 'administrator'], function () {
$this->get('panel', 'AdminController#index');
});
my kernel:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
...
\App\Http\Middleware\CheckUserAdminRole::class,
];
now when i add my middleware as CheckUserAdminRole to route group like with this code:
Route::group(['namespace' => 'Dashboard', 'middleware' => ['auth:web','CheckUserAdminRole'], 'prefix' => 'dashboard'], function () {
i get this error:
Class CheckUserAdminRole does not exist
this codes couldn't resolve my problem:
php artisan route:clear
php artisan cache:clear
php artisan config:clear
composer dump-autoload
Instead of registering your middleware in the $middleware array, you should register it in $routeMiddleware like so:
protected $routeMiddleware = [
...
'checkAdmin' => \App\Http\Middleware\CheckUserAdminRole::class,
];
Note: registering a middlware in the $middleware array results in it being executed for every request and therefore is not applicable on specific routes.
Then you can use it in your routes with the checkAdmin name:
Route::group(['namespace' => 'Dashboard', 'middleware' => ['auth:web','checkAdmin'], 'prefix' => 'dashboard'], function () {
Source
You can also use middleware and route group together easily like so:
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function()
{
//All the routes that belongs to the group goes here
Route::get('dashboard', function() {} );
});
Here's a simple fix without touching the Kernel file and taking advantage of the Policy. The accessAdmin is a function created inside Policy file.
Route::group(['namespace' => 'Dashboard', 'middleware' => 'can:accessAdmin, App\User', 'prefix' => 'dashboard'], function () {
$this->group(['prefix' => 'administrator'], function () {
$this->get('panel', 'AdminController#index');
});
You can try middleware with prefix and groups.
Route::middleware(['Auth'])->prefix('api/')->group(function() {
Route::group(['prefix' => 'review/'], function () {
Route::get('/', 'User\Controllers\Api\UserController#getUserReviews');
});
});
Hope its helps

Laravel 5.2 - Route authentication

I create the auth class from laravel and now i would like to know how i can put the /register page only for users who login in system?
my routes are:
Route::group(['middleware' => ['web']], function () {
//
Route::get('/', 'Auth\AuthController#getLogin');
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
//Route::get('/register', 'Auth\AuthController#getRegister');
});
For authenticate url just add a auth middleware, that will work after logged in a user. Follow the code below:
Route::group(['middleware' => ['web','auth']], function () {
Route::get('/register', 'YourController#getRegister');
});
You have to use a middleware to filter the users.
In the case of authentication, there is a built-in middleware
called 'auth'
You can filter by middlewares in group as AnowarCst showed you
or in single routes like this:
Route::get('/register', [
'middleware' => 'auth',
'yourController#yourFunction'
]);
Read the docs to better understand
MIDDLEWARE.
Don't be scared, it's more simple than how it looks. :)
I solved the problem with this:
public function __construct()
{
$this->middleware('guest', ['except' => ['logout', 'register', 'showRegistrationForm']]);
$this->middleware('auth', ['only' => ['register', 'showRegistrationForm']]);
}
And my routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'Auth\AuthController#getLogin');
});
Route::group(['middleware' => 'web'], function () {
Route::Auth();
Route::get('/dashboard', 'HomeController#index');
});
Route::group(['middleware' => ['web', 'auth']], function () {
Route::get('/register', 'Auth\AuthController#showRegistrationForm');
});
Thanks guys.

Laravel subdomains routing and session management

I am trying to set an isolated backend and frontend environments in the same Laravel5 application.
My ideal purpose is to get such isolation thru
subdomains
different guards(tables) for users at front and users at backend
Up now I've just defined my guard/providers at config/auth.php
and done :
Routing
Route::group(['domain' => 'front.mydomain.com'], function () {
Route::get('/', 'WelcomeController#index');
Route::group(['middleware' => ['web']], function () {
Route::auth();
});
Route::group(['middleware' => ['web','auth']], function () {
Route::get('/home', 'HomeController#index');
Route::group(['prefix' => 'record'], function () {
Route::get('all','RecordController#all');
});
});
});
Route::group(['domain' => 'admin.mydomain.com'], function () {
Route::get('/', 'WelcomeController#index');
Route::group(['middleware' => ['web']], function () {
//SOLO RUTAS de pantallas de LOGIN
Route::get('/password/reset', function() {
return Response::view('errors.404', array(), 404);
});
Route::auth();
});
Route::group(['middleware' => ['web','auth:admin']], function () {
Route::get('/home', function() {
return view('admin.dashboard');
});
Route::group(['prefix' => 'record'], function () {
Route::get('all', 'RecordController#all');
Route::get('edit/{id}', 'RecordController#edit')->where('id','[0-9]+');
});
});
});
Config/session (extract)
'domain' => '.urbina.biz',
I was expecting to get errors because I haven't yet implemented model/tables for the admin part, BUT all I get is redirection loops , apparently because the cookie is not really isolating my subdomains, looks like it's mixing it all up :(
Some one may help ?

Auth session killed after redirect | laravel 5.2

I made a simple login form. I log my user with :
Auth::loginUsingId($user->id, true);
But when I redirect the user to the ClientController I'm redirect to the login form, the Auth session is not persitent.
return redirect()->action('ClientController#index');
My routes :
Route::group(['middleware' => 'web'], function() {
Route::get('/', 'HomeController#index');
Route::post('/', 'HomeController#auth');
});
Route::group(['prefix' => 'admin', 'middleware' => 'web'], function() {
Route::get('/', 'AdminController#index');
});
Route::group(['prefix' => 'client', 'middleware' => ['auth', 'web']], function() {
Route::get('/', 'ClientController#index');
});
The web middleware needs to kick in before the auth middleware because the web middleware is responsible for booting up your session. Switch the order around like this:
Route::group(['prefix' => 'client', 'middleware' => ['web', 'auth']], function() {
Route::get('/', 'ClientController#index');
});
However, while we're on this subject, you can nest route groups within another route group so to prevent an error like this in the future, I would just recommend nesting everything inside the web middleware like this:
Route::group(['middleware' => 'web'], function() {
Route::get('/', 'HomeController#index');
Route::post('/', 'HomeController#auth');
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController#index');
});
Route::group(['prefix' => 'client', 'middleware' => 'auth'], function() {
Route::get('/', 'ClientController#index');
});
});

Laravel admin/user route prefix approach

so I have to make this system for transport management. The user can log in create/update/edit all his trips. But the admin can do the same for all users. I have divided user and admin in to route prefixes:
Route::group(['prefix' => 'admin/', 'middleware' => ['auth','admin']], function(){
Route::resource('trips', 'TripsController',
array('except' => array('show')));}
Route::group(['prefix' => 'user/', 'middleware' => ['auth', 'user']], function(){
Route::resource('trips', 'TripsController',
array('except' => array('show')));
}
The problem is in every method of the TripController I have to pass route variable with the correct url (the admin request will have a 'admin' prefix, and the users will have 'user' prefix)
return View('trips.index', compact('users', 'route'));
The question is there a way to do this nicely or should I just pull the trips Route::resource out of the both groups so that it wouldn't have any groups? What is the correct approach here?
I use this approach:
Route::group(['namespace' => 'Admin', 'as' => 'admin::', 'prefix' => 'admin'], function() {
// For Other middlewares
Route::group(['middleware' => 'IsNotAuthenticated'], function(){
// "admin::login"
// http://localhost:8000/admin/login
Route::get('login', ['as' => 'login', 'uses' => 'AdminController#index']);
});
// For admin middlewares
Route::group(['middleware' => 'admin'], function(){
// "admin::admin.area.index"
// http://localhost:8000/admin/area/{area}
Route::resource('Area', 'AreaController');
// "admin::dashboard"
// http://localhost:8000/admin/
Route::get('/', ['as' => 'dashboard', 'uses' => 'AdminController#dashboard']);
});
});
Whenever I need to access url in blade templates I simply use route helper method.
// For resourceful routes
{{ route('admin::admin.city.index') }}
or
//For regular get/post routes
{{ route('admin::dashboard') }}
Or simply run artisan command to list route names.
php artisan route:list
I did it with this:
//Admin Group&NameSpace
Route::namespace('Admin')->prefix('admin')->group(function () {
Route::get('/dashboard', 'DashboardController#index')->name('dashboard')->middleware('auth');
});
Even you can customize the ->middleware('auth'); with a custom middleware role based.

Resources