Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protecting all admin/ routes with auth in Laravel

I am brand new to laravel and am setting up admin panel authorization on my first application. The way I have my files setup currently setup is:

controllers/
    admin/
        dashboard.php
        settings.php
    non-admin-controller1.php
    non-admin-controller1.php
views/
    admin/
        dashboard.blade.php
        login.blade.php
        template.blade.php
    non-admin-view1.php
    non-admin-view1.php
    non-admin-view1.php

...and these are my routes

Route::get('admin/login', function()
{
    return View::make('admin.login');
});

Route::get('admin/logout', function()
{
    return Auth::logout();
    return Redirect::to('admin/login');
});

Route::post('admin/login', function()
{
    $userdata = array('username' => Input::get('username'),
                      'password' => Input::get('password'));
    
    if (Auth::attempt($userdata))
    {
        return Redirect::to('admin');
    }
    else
    {
        return Redirect::to('admin/login')->with('login_errors',true);
    }
});

Route::controller('admin.dashboard');

Route::get('admin', array('before' => 'auth', function() {
    return Redirect::to_action('admin@dashboard');
}));

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::to('admin/login');
});

When I go to /admin I am redirected to admin/login and asked to login which is exactly how I need it to work. Upon logging in I am redirected to admin/dashboard and it all looks good there too. I am having 2 problems however.

  1. When I go to admin/logout I am logged out but greeted with a blank page (it's not redirecting to admin/login)

  2. When logged out, if I go to admin/dashboard I am greeted with the error

Error rendering view: [admin.dashboard]

Trying to get property of non-object

What am I doing wrong here? What am I doing right? Would it make more sense to create a separate bundle for admin? Thanks!

like image 646
Yev Avatar asked Apr 04 '13 22:04

Yev


People also ask

How do I protect my admin route?

Protect a route Open the file routes/web. php and add following code at the end of it: Route::middleware('can:accessAdminpanel')->group(function() { Route::get('/adminpanel/dashboard', 'Adminpanel\Dashboard@index'); // future adminpanel routes also should belong to the group });

What is Auth :: Routes () in Laravel?

Auth::routes() is just a helper class that helps you generate all the routes required for user authentication. You can browse the code here https://github.com/laravel/framework/blob/5.3/src/Illuminate/Routing/Router.php instead.

How do I login with Auth in Laravel?

How do I enable authentication in Laravel? You need to Install the laravel/ui Composer bundle and run php artisan ui vue –auth in a new Laravel application. After migrating your database, open http://your-app.test/register or any other URL that's assigned to your application on your browser.


2 Answers

So I was able to solve my problem a slightly different way. I created an (base) Admin_Controller in the root of the controllers folder, with a constructor calling the auth filter before execution:

class Admin_Controller extends Base_Controller {

    public function __construct()
    {
        $this->filter('before', 'auth');
    }

}

and then made all my admin related controllers in /controllers/admin extend Admin_Controller and call the parent constructor:

class Admin_Dashboard_Controller extends Admin_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function action_index()
    {
        return View::make('admin.dashboard');
    }

}

This might not be the most eloquent solution, but it does the job!

like image 93
Yev Avatar answered Oct 30 '22 18:10

Yev


In your admin/login route you have an unnecessary return before the Auth::logout() call, nuke that and it should fix it up.

Another issue here is that only your one 'admin' route is getting filtered. You could wrap all of your admin routes with a Route::group() and apply the 'auth' before filter or you could use Route::filter('pattern: admin/*', 'auth') too.

Check out:

http://laravel.com/docs/routing#filters

For the second issue, is your Admin Dashboard controller class named Admin_Dashboard_Controller and if so, do you have an action_index() or get_index() function in there returning a view?

Check out:

http://laravel.com/docs/controllers#nested-controllers

(I'm assuming you're using L3 here btw.)

like image 45
Darren Monahan Avatar answered Oct 30 '22 19:10

Darren Monahan