Maria Vilaró
Maria Vilaró

Reputation: 314

Laravel 5 Authentication: make Controller decides which methods are public

This is a very long question for (I think) a very simple answer, but I am a total newbie in Laravel and I need to know if I am addressing it the right way.

What I did

I followed the Intermediate Tutorial and modified it just a little bit, and now I have a simple task list that uses authentication. I had this in my TaskController constructor:

public function __construct(TaskRepository $tasks)
{       
    $this->middleware('auth');
}

This checks if the user is logged in before launching any methods, so I just need to call them like this in my routes file:

Route::get('/home', 'TaskController@index');
Route::get('/tasks', 'TaskController@indexUser');

Then I wanted to remove the authentication requirement for the index method, so that all users can see the /home page (where I list all the tasks), and only authenticated users can see the /tasks page (where I list only the user tasks and allow to delete them). I managed to do it like this:

1) I removed $this->middleware('auth') from the TaskController constructor

2) I modified my routes file to look like this:

Route::get('/home', 'TaskController@index');
Route::get('/tasks', [
    'middleware' => 'auth',
    'uses' => 'TaskController@indexUser'
]);

Ok, this works, but:

What I want to achieve

I don't want to have this logic in my routes file, I want that my Controller decides which methods are public and which not. But I got stuck. I think it should be something like this:

class TaskController extends Controller
{
    /**
     * Display a list of all current tasks
     */
    public function index()
    {
        return view('tasks.index', [
            'tasks' => Task::orderBy('created_at', 'asc')->get()
        ]);            
    }

    /**
     * Display a list of all of the logged user's task.
     *
     */
    public function indexUser(Request $request)
    {
        if (Auth::check() {
            return view('tasks.index_user', [
                'tasks' => $this->tasks->forUser($request->user()),
            ]);
        } else {
            //This is what I don't know how to do:
            redirect_to_login
        }
    }
}

How can I achieve this?

Upvotes: 0

Views: 74

Answers (2)

Jirennor
Jirennor

Reputation: 1289

You can do the following if you want this logic in your controller:

if (!Auth::check()) {
   return redirect('/path/to/login/page');
}

This way you don't have giant if else statement in your controller. (Handy if your controller contains more logic then the sample above)

I personally would go for the answer Christian Giupponi provided. Cause it makes much more sense to handle this logic in the construct function then in your controller.

Upvotes: 1

Christian Giupponi
Christian Giupponi

Reputation: 7618

You can decide which controller method should execute the middleware:

public function __construct()
{
   $this->middleware('auth', ['only' => ['indexUser', 'update'] ];
}

just add in only the method you want to protect.
When a user try to access a method will automatically redirect to the login page.

Here you can find the documentation: https://laravel.com/docs/5.1/controllers#controller-middleware

Upvotes: 2

Related Questions