Laravel 5.3 - Authentication is broken - php

About a year ago we took over an existing Laravel 5.1 site and upgraded to 5.3 - We recently became aware that an admin panel that was part of the old site no longer works (or unable to authenticate).
The original routes file contains the following:
//Login
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
//Admin
//Dashboard
Route::group(array('prefix' => 'admin', 'middleware' => 'auth'), function() {
//Dashboard
Route::get('/webadmin', array('as' => 'dashboard', 'uses' => 'Admin\DashboardController#index'));
});
Which after the upgrade stopped working as I understand the Route::controllers method was depreciated. We changed it to the following as I understand that was the replacement:
//Login
Route::resource('password','Auth\PasswordController');
Route::resource('auth','Auth\LoginController');
//Admin
//Dashboard
Route::group(array('prefix' => 'admin', 'middleware' => 'auth'), function() {
//Dashboard
Route::get('/webadmin', array('as' => 'dashboard', 'uses' => 'Admin\DashboardController#index'));
});
However, when we access the sites admin panel by example.com/admin/webadmin we are automatically redirect to example.com/login which then displays the dreadful NotFoundHttpException in compiled.php
This leads me to believe that the authentication middleware is not registered correctly. I am not sure what the correctly route is to take so will gladly appreciate any assistance :)

The redirect is happening because your not being authenticated and you are redirected to login route because the unauthenticated method in the app\Exceptions\Handler class redirects the user to /login using something like this:
return redirect()->guest('login');
So, you've to either create a /login route or change the above line to this:
return redirect()->guest('auth');
This should work and your AuthController::index method should show the login form because this will hit the index method in your AuthController because it's a resource controller.

Related

Route doesn't work on my laravel 5.1 project

I'm trying to define routes to access to some views, but it doesn't work :/
Route::group(['prefix' => 'messages'], function () {
Route::get('/', ['as' => 'messages', 'uses' => 'MessagesController#index']);
Route::get('create', ['as' => 'messages.create', 'uses' => 'MessagesController#create']);
Route::post('/', ['as' => 'messages.store', 'uses' => 'MessagesController#store']);
Route::get('{id}', ['as' => 'messages.show', 'uses' => 'MessagesController#show']);
Route::put('{id}', ['as' => 'messages.update', 'uses' => 'MessagesController#update']);
});
I'm always redirected to the home view ('/') of my project when I'm logged.
If I try to access as guest for example : http://myproject.dev/messages I receive a 404 error !
The package that I try to put in my project is : https://github.com/cmgmyr/laravel-messenger
I've installed it on a fresh installation of laravel 5.1 and it's work, but on my project it doesn't work !
My project is in local with MAMP PRO.
With the command php artisan route:list, I can see that routes exists !
EDIT : My routes for authentification was bad !
Route::controller('/','Auth\AuthController') is not a good solution !
To use the MessagesController the user must be authenticated. That means that the user must be logged. The idea is that if a user is not logged he must be redirect to the login page. To make this you must add the auth middleware to the Route::group. The auth middleware will detect if the user is logged and if is not logged it will be redirect to the login page.
Route::group(['middleware' => 'auth', 'as' => 'messages.', 'prefix' => 'messages'], function () {
Route::get('', ['as' => 'index', 'uses' => 'MessagesController#index']);
Route::get('create', ['as' => 'create', 'uses' => 'MessagesController#create']);
Route::post('', ['as' => 'store', 'uses' => 'MessagesController#store']);
Route::get('{id}', ['as' => 'show', 'uses' => 'MessagesController#show']);
Route::put('{id}', ['as' => 'update', 'uses' => 'MessagesController#update']);
});
I think that is this the error
I still can't comment, so I'll write an answer.
I never was able to put a 'root' route inside a Route's group. Laravel need's something there, you can't use "/" or "".
Seeing your code, I suggest you to use a resource view.
Route::resource('messages', 'MessagesController');
It will generate all your routes. I hope it helps.

Laravel post route controller method not getting called

I'm building an app in Laravel and I already had my user registration working fine, I now want to modify how the registration works so I modified my route to call a different method.
Originally the route was defined like so:
Route::post('register', 'Auth\AuthController#postRegister');
And this worked fine. I then changed it to:
Route::post('register', 'Auth\AuthController#someOtherMethod');
This method is defined as follows:
public function someOtherMethod(Request $request)
{
die('If the method is called you should see this');
}
However, it doesn't get called (the message doesn't show). Instead it redirects me to the root of the site.
Note that I have a cache-busting script on my server that I run every time I have weird issues like this which runs the following commands:
php artisan route:clear
php artisan cache:clear
service php5-fpm restart
service nginx restart
I also run the page in an incognito/private window every time I make a change.
Now for the interesting part; I tried undoing the changes I made so that it calls postRegister again, I fully expected this to make it revert to the default behaviour but it still redirects me to the root of the site! So now I don't even have a registration page that functions at all.
Does anyone have any idea what's going on?
Thanks in advance for your help.
Edit:
Here's my full routes.php:
use Illuminate\Http\Request;
Route::group(['middleware' => 'web'], function () {
/** Public routes **/
Route::get('', 'HomepageController#index');
Route::get('/', 'HomepageController#index');
Route::get('terms', function() {
return view('terms');
});
Route::get('privacy', function() {
return view('privacy');
});
/** Public auth routes **/
Route::get('register', 'RegistrationController#index');
Route::post('register', 'Auth\AuthController#postRegister');
Route::get('login', function() {
return view('auth.login');
});
Route::post('login', 'Auth\AuthController#postLogin');
Route::get('logout', 'Auth\AuthController#getLogout');
Route::get('dashboard/login', function() {
return view('admin.login');
});
Route::post('dashboard/login', 'AdminAuth\AuthController#postLogin');
Route::get('dashboard/logout', 'AdminAuth\AuthController#getLogout');
/** Admin routes **/
Route::get('dashboard', [
'middleware' => 'admin',
'uses' => 'Admin\DashboardController#index'
]);
Route::get('dashboard/users', [
'middleware' => 'admin',
'uses' => 'Admin\DashboardController#showUsers'
]);
Route::get('dashboard/search', [
'middleware' => 'admin',
'as' => 'adminSearch',
'uses' => 'Admin\DashboardController#query'
]);
/** Admin auth routes **/
Route::get('dashboard/staff/create', [
'middleware' => 'admin',
function () {
return view('admin.register');
}
]);
Route::post('dashboard/staff/create', [
'middleware' => 'admin',
'uses' => 'AdminAuth\AuthController#postRegister'
]);
/** Controllers **/
Route::controllers([
'password' => 'Auth\PasswordController',
]);
});
make sure that you config/auth.php is calling proper Auth guard. Also redirectsTo is by default set to '/' which is route of site. What is in your middleware? the default RedirectIfAuthenticated middleware has by default entry to point to root of the site. Only these 3 scenarios possibly acting other than expected.

Laravel 5.1 redirect after auth

I am using laravel 5.1 and have a route similar to:
Route::get('/{id}/write-review', ['middleware' => 'auth', 'uses' => 'ItemController#writeReview']);
The problem I have is that when someone goes to this page (when not logged in) they get redirected to login, which is fine, but then redirected to the $redirectPath set in AuthController.php. I would like them to be redirected back to the review page.
Thanks in advance.
The intended redirect function will redirect the user to the URL they were attempting to access before being caught by the authentication filter. A fallback URI may be given to this method in case the intended destination is not available.
https://laravel.com/docs/5.1/authentication#authenticating-users
return redirect()->intended('dashboard');
I think i found the problem route {id}/write-review must be under the web middleware
Route::group(['middleware' => ['web']], function () {
Route::get('login', 'Auth\AuthController#getLogin');
Route::post('login', 'Auth\AuthController#postLogin');
Route::get('logout', 'Auth\AuthController#getLogout');
Route::group(['middleware' => ['auth']], function () {
Route::get('/{id}/write-review', ['uses' => 'ItemController#writeReview']);
});
});

Redirecting to named sub-domain routes in Laravel 5.1 that share the same name?

I've set up sub-domains in Laravel 5.1 and they work fine when manually going to the URL of the route, but when I try to redirect from my controller using a named route, the basic user home route will always be loaded regardless of the sub-domain that sent the request.
Route::group(['domain' => 'admin.myapp.app'], function()
{
Route::get('/home', ['as' => 'home', 'uses' => 'AdminController#getHome']);
});
Route::group(['domain' => 'basic.myapp.app'], function()
{
Route::get('/home', ['as' => 'home', 'uses' => 'BasicController#getHome']);
});
One work around I found was to use
redirect()->to(route('home', [], false));
which will just return the URL of the named route. Anyone know if there is a better solution for this?

Laravel Route errors when filters are applied

I'm trying to add authentication into my Laravel app. Users can log in just fine, but I want to ensure that anyone who goes to a page is logged in - I don't want someone to bookmark a page within the application and go to it without ensuring they're logged in.
As such, I've been looking into Filters. I must not be implementing it right, because I get the following error call_user_func_array() expects parameter 1 to be a valid callback, no array or string given when I go to one of the routes I've added the filter to, or any page that links to a filtered route.
Routes.php
Route::get( '/admin/login', 'UserController#getAdminLoginForm');
Route::get( '/admin/races', array('before' => 'auth', 'RaceController#getAllRacesView'));
Route::get( '/admin/geographies', array('before' => 'auth', 'GeographyController#getLocalGeographiesView'));
Route::get( '/admin/candidates', array('before' => 'auth', 'CandidateController#getAllCandidatesView'));
Filters.php
Route::filter('auth', function() {
if (Auth::guest()) return Redirect::guest('/admin/login');
});
I had the pages working prior messing around with filters, so what am I missing about implementing this?
Here's what the documentation says about adding a filter to controller actions:
Attaching A Filter To A Controller Action:
Route::get('user', array('before' => 'old', 'uses' => 'UserController#showProfile'));
So in short, you are missing the array key uses. Try something like this:
Route::get('/admin/races', array('before' => 'auth', uses => 'RaceController#getAllRacesView'));

Categories