2
votes

If i go to http://www.yourdomain.com/admin/login i see my login page.

If i go to http://www.yourdomain.com/admin/example i have the redirect to http://www.yourdomain.com/login without the admin.

My web routes:

Auth::routes();

Route::prefix('admin')->group(function() {
    Route::get('/login','Auth\AdminLoginController@showLoginForm')->name('admin.login');
    Route::post('/login','Auth\AdminLoginController@login')->name('admin.login.submit');
    Route::get('/manifiesto','AdminController@getIndex')->name('admin.dashboard');
    Route::get('/logout','Auth\AdminLoginController@logout')->name('admin.logout');
    Route::get('/trabajadores','AdminController@showTrabajadores')->name('admin.trabajadores');
    Route::get('/clientes','AdminController@showClientes')->name('admin.clientes');
    Route::get('/proyectos','AdminController@showProyectos')->name('admin.proyectos');
    Route::get('/administradores','AdminController@showAdmins')->name('admin.administradores');
});

When i put some url with the /admin before and user isn't logged, i want to redirect to /admin/login.

Thanks.

More info:

App/http/Controllers/Auth/AdminLoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;
class AdminLoginController extends Controller
{

    protected $loginPath = 'admin/login';

    public function __construct()
    {
        $this->middleware('guest:admin', ['except' => ['logout']]);
    } 
    public function showLoginForm()
    {
        return view('backend.public.pages.login');
    }
    public function login(Request $request)
   {
       //validate the form data
       $this->validate($request, [
           'email' => 'required|email',
           'password' => 'required|min:6'
       ]);
       //attempt to log the user in
       if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){
            //if successful, then redirect to their intended location
            return redirect()->intended(route('admin.dashboard'));
       }
       return redirect()->back()->withInput($request->only('email','remember'));
   } 

   public function logout()
   {
       Auth::guard('admin')->logout();

       return redirect('/');
   }
}

App\Http\Middleware\AdminAuthenticate.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class AdminAuthenticate
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
 public function handle($request, Closure $next)
 {
    if ($this->auth->guest())
    {
        if ($request->ajax())
        {
            return response('Unauthorized.', 401);
        }
        else
        {
            return redirect()->guest('admin/login'); // <--- here
        }
    }

    return $next($request);
 }
}
4
Try this Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function() {....});Maraboc
Same problem, i have the redirect to /loginLluís Puig Ferrer
Is there other login routes or JUST admin/login ?Maraboc
Just admin/loginLluís Puig Ferrer
Did you added this middlware to your app/Http/Kernel.php in the $routeMiddleware property ? if yes and it's like this 'adminAuth' => \Illuminate\Auth\Middleware\AdminAuthenticate::class, then you need to do like this Route::group(['prefix' => 'admin', 'middleware' => 'adminAuth'], function() {....}); `Maraboc

4 Answers

2
votes
  1. Create an middleware
php artisan make:middleware AuthAdmin
  1. Check for guest in the handle method of the middleware
public function handle($request, Closure $next)
{
    if (Auth::guest()) {
        if ($request->ajax() || $request->wantsJson()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('admin/login');
        }
    }

    return $next($request);
}
  1. Add a key to the middleware in app/Http/Kernel.php in $routeMiddleware array
'auth_admin' => \App\Http\Middleware\AuthAdmin::class
  1. Attach the auth_admin middleware to the group
Route::group(['prefix' => 'admin',  'middleware' => 'auth_admin'], function() {
    // Your admin routes except login
});
0
votes

write bellow code in your route.php file

Route::group(array('prefix' => 'admin'), function() {
        Route::controller('login', 'AdminloginController');
    });
    Route::group(array('before' => 'admin_ajax', 'prefix' => 'admin'), function() 
    {
        //route for pages which are render after login
    });
    Route::get('/admin', function() {
        return View::make('admin.loginform');
    });

And Write bellow code in your filter.php file

Route::filter('admin_ajax', function() {
    if (!Auth::admin()->check()) {
        return Redirect::to('admin/login');
    } else {  
    }
});

And if you are using laravel 5.4

Route::get('/manage', function () {
    return redirect('manage/login');
});
Route::group(['prefix' => 'manage'], function() {
    //login bypass for the below listed controllers    
    Route::resource('login', 'AdminLoginController@showLoginForm');
    Route::post('dologin', 'AdminLoginController@login');
});
0
votes

All you can do is add the auth middleware like this :

Route::group(['prefix' => 'admin',  'middleware' => 'auth'], function() {
    Route::get('/login','Auth\AdminLoginController@showLoginForm')->name('admin.login');
    Route::post('/login','Auth\AdminLoginController@login')->name('admin.login.submit');
    Route::get('/manifiesto','AdminController@getIndex')->name('admin.dashboard');
    Route::get('/logout','Auth\AdminLoginController@logout')->name('admin.logout');
    Route::get('/trabajadores','AdminController@showTrabajadores')->name('admin.trabajadores');
    Route::get('/clientes','AdminController@showClientes')->name('admin.clientes');
    Route::get('/proyectos','AdminController@showProyectos')->name('admin.proyectos');
    Route::get('/administradores','AdminController@showAdmins')->name('admin.administradores');
});

But by default this will redirect to /login, if you want to override this you have two chocies depending on if you have other type of users that uses the /login route or not !!

  • If NO ONE uses /login route

1- You need to modify App\Http\Middleware\Authenticate::handle() method and change /login to admin/login.

2- Then you need to add $loginPath property to your \App\Http\Controllers\Auth\AuthController class.

Authenticate

namespace App\Http\Middleware;
class Authenticate {
        /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('admin/login'); // <--- here
            }
        }

        return $next($request);
    }
}

AuthController

namespace App\Http\Controllers\Auth;
class AuthController extends Controller
{
    protected $loginPath = 'admin/login'; // <--- here

    // ... other properties, constructor, traits, etc 
}
  • If there is someone using /login route

You must create you own middleware and do what it takes for auth checking in the handle method with redirecting to your admin/liging route :)

In this case :

  1. Add the following line in $routeMiddleware property at app/Http/Kernel.php file

    'adminAuth' => \App\Http\Middleware\YourNewMiddleware::class,
    
  2. Don't forget to add your new middleware in route group as follow

     Route::group(['prefix' => 'admin',  'middleware' => 'adminAuth'], function() 
     {
        // your admin routes
     });
    
0
votes

Make an another middleware for admin. follow the step

  1. Make a file named AdminAuthenticate in app/Http/Middleware location and copy the content of Authenticate in New file change the Class name as AdminAuthenticate

  2. Change the content of handle function as show below

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('/admin/login');
            }
        }
    
        return $next($request);
    }
    
  3. Add the following line in $routeMiddleware array at app/Http/Kernel.php file

    'AdminAuth' => \App\Http\Middleware\AdminAuthenticate::class,
    
  4. Now everything is okay. just add your new middleware in route group as follow

    Route::group(['prefix' => 'admin',  'middleware' => 'AdminAuth'], function() 
    {
       // all admin routes
    });
    

    Or you can add new middleware to constructor function of every admin controller as like below

    $this->middleware('AdminAuth');