Skip to content

Instantly share code, notes, and snippets.

@sboo
Last active December 6, 2018 10:37
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save sboo/10943f39429b001dd9d0 to your computer and use it in GitHub Desktop.
Save sboo/10943f39429b001dd9d0 to your computer and use it in GitHub Desktop.
multiauth
<?php namespace App\Http\Controllers\Auth;
use Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers;
/**
* Create a new authentication controller instance.
*
* @param \Illuminate\Contracts\Auth\Registrar $registrar
*/
public function __construct(Registrar $registrar)
{
$this->auth = Auth::admin();
$this->registrar = $registrar;
$this->middleware('guest', ['except' => 'getLogout']);
}
}
<?php namespace App\Http\Controllers\Auth;
use Auth;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Password;
use Illuminate\Foundation\Auth\ResetsPasswords;
class PasswordController extends Controller {
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset requests
| and uses a simple trait to include this behavior. You're free to
| explore this trait and override any methods you wish to tweak.
|
*/
use ResetsPasswords;
/**
* Create a new password controller instance.
*
*/
public function __construct()
{
$this->auth = Auth::admin();
$this->passwords = Password::admin();
$this->middleware('guest');
}
}
<?php namespace App\Http\Middleware;
use Auth;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class Authenticate {
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
*/
public function __construct()
{
$this->auth = Auth::admin();
}
/**
* 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('auth/login');
}
}
return $next($request);
}
}
<?php namespace App\Http\Middleware;
use Auth;
use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\RedirectResponse;
class RedirectIfAuthenticated {
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
*/
public function __construct()
{
$this->auth = Auth::admin();
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->check())
{
return new RedirectResponse(url('/home'));
}
return $next($request);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<link href="{{ asset('/css/app.css') }}" rel="stylesheet">
<!-- Fonts -->
<link href='//fonts.googleapis.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Laravel</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="{{ url('/') }}">Home</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
@if (Auth::admin()->guest())
<li><a href="{{ url('/auth/login') }}">Login</a></li>
<li><a href="{{ url('/auth/register') }}">Register</a></li>
@else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{{ Auth::admin()->get()->name }} <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="{{ url('/auth/logout') }}">Logout</a></li>
</ul>
</li>
@endif
</ul>
</div>
</div>
</nav>
@yield('content')
<!-- Scripts -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>
Copy link

ghost commented Jul 12, 2015

Also I have another question, why we always write:
$this->auth = Auth::admin();

Why the admin user type? Every time I try to log in as a different user type, it always login as an admin.

I think this is the same problem as @samsoft00 and @marcellorg .. Do I need to create Separate Authenticate.php and RedirectIfAuthenticated files?

@angelobiscola
Copy link

when use method Auth :: Admin () -> login ( $ user , 1); It does not create the session .
Auth :: admin () -> check () returns false
Auth :: admin () -> get () returns null

need help...

@JFSolorzano
Copy link

I've got problems using this package, please check this question at SO please, the main topic there is how to setup multiauth in a multisite project, basically i have two tables for users [Employees, Clients], both of them can only login to a specific site [CMS, Site] correspondingly, the above files only work for a user type admin, and it works with admin because it's using eloquent driver that means that you're using users table for admin users, but you don't demostrat how to work with the other type of user; in my case i want to set up multiauth without making use of users table, the problem right now is that app/Services/Registrar.php is configured for Users model...

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