Skip to content

Instantly share code, notes, and snippets.

@ollieread
Created January 7, 2014 18:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ollieread/8303638 to your computer and use it in GitHub Desktop.
Save ollieread/8303638 to your computer and use it in GitHub Desktop.
Replacing the default auth filter with one for multiauth
<?php
// original auth filter
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::guest('login');
});
// new multiauth filter
Route::filter('auth', function()
{
if (Auth::admin()->guest()) return Redirect::guest('login');
});
@cyborgcassowary
Copy link

I'm running into some trouble replacing the default filter Auth::guest() with Auth::admin()->guest(). You mention that the default filters no longer works. Is there another way to logout without using Auth::guest()? My code in my filters.php file is below:

Route::filter('auth', function()
{
  if (Auth::admin()->guest())
      {
          if (Request::ajax())
          {
                return Response::make('Unauthorized', 401);
           }
              return Redirect::guest('login');
        }
 });

Screenshot of error I'm receiving: http://imgur.com/no8bjKB

@cyborgcassowary
Copy link

I figured it out. Did a couple of tests with php artisan tinker and was successfully able to logout a user with the above code adjustment in filter.php

 >var_dump(Auth::user()->loginUsingId(1));
   object(Illuminate\Auth\GenericUser)#651 (1) {
   ["attributes":protected]=>
   array(7) {
   ["id"]=>
   string(1) "1"
   ["username"]=>
   string(11) "ryanhettler"
   ["email"]=>
   string(20) "ryan@ryanhettler.com"
   ["password"]=>
   string(60) "$2y$10$zVw8.SzNtbM7sqTLmNlqm.OdK72lfESrg2nJdXowjKC8AIKCnkLAK"
   ["remember_token"]=>
   string(60) "n3WyEIKyUURGojCKKGE5QXhThtdluFNpSW6p7kYAb5eP33bCqRfEEuQPomRz"
   ["created_at"]=>
   string(19) "2014-12-18 16:42:09"
   ["updated_at"]=>
     string(19) "2014-12-18 16:42:09"
   }
 }
 >var_dump(Auth::user()->get());
    object(Illuminate\Auth\GenericUser)#651 (1) {
    ["attributes":protected]=>
      array(7) {
      ["id"]=>
     string(1) "1"
     ["username"]=>
     string(11) "ryanhettler"
    ["email"]=>
     string(20) "ryan@ryanhettler.com"
    ["password"]=>
    string(60) "$2y$10$zVw8.SzNtbM7sqTLmNlqm.OdK72lfESrg2nJdXowjKC8AIKCnkLAK"
    ["remember_token"]=>
    string(60) "n3WyEIKyUURGojCKKGE5QXhThtdluFNpSW6p7kYAb5eP33bCqRfEEuQPomRz"
     ["created_at"]=>
      string(19) "2014-12-18 16:42:09"
     ["updated_at"]=>
    string(19) "2014-12-18 16:42:09"
  }
 }
 >var_dump(Auth::user()->logout());
 NULL
 >var_dump(Auth::user()->get());
 NULL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment