-
-
Save sboo/10943f39429b001dd9d0 to your computer and use it in GitHub Desktop.
<?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> |
hey, thank you so much for doing this! upgrading my L4 to L5 now. i've made the required changes, and i 'assume' that where you have Auth::admin() I should put my user type, Auth::teacher(). But I also have Auth::student(). How to handle this?
He logs perfectly, but when I try to use on some pages dd(\Auth::Usuario()->check()); = false.
Thanks @sboo, everything is working perfectly right now but i've a problem, whenever i navigate to http://localhost:8888/admin, it display... The requested resource /admin was not found on this server.
Route
Route::group(['prefix' => 'admin'], function(){
Route::get('/', 'AdminController@index');
});
i've this in my config/auth in my file...
'multi' => [
'admin' => [
'driver' => 'eloquent',
'model' => 'App\Admin',
],
'client' => [
'driver' => 'database',
'table' => 'partner',
'email' => 'partner.emails.password',
]
],
please what can i do?
@marcellorg please how do you resolved the issue with dd(Auth::Usuario()->check()); = false ?
@sboo hey, thanks for all of this, works perfectly like a charm. But, now i've some question. If i set $this->auth with maybe Auth::admin(), so where i must set $this->auth for Auth::client() ? Okay, maybe i can make another AuthController. So, if like that, i also must make another /auth/login ?
Can i just use one /auth/login but it can handle the two (Auth::admin() and Auth::client()) ? How to make it. Thanks
hi all;
@sboo you've done great work.
so i followed exactly the guidance (installation, config, controllers modifs ...) but now i'm little bit confused on 'Usage' part, and as i see i'm not the only one !! i have the same confusion as @nmfzone.
in which controller should i put the logic :
Auth::admin()->attempt(array(
'email' => $attributes['email'],
'password' => $attributes['password'],
));
Auth::client()->attempt(array(
'email' => $attributes['email'],
'password' => $attributes['password'],
));
Auth::admin()->check();
Auth::client()->check();
plz, how to deal with this ? i really appreciate your help.
How to use this for client specific auth route. Now its working only on route auth/login for admins table but for clients. I'm confused about auth/login route (where is definition of this route because i not see it in routes.php). It's not complete guide. Regards
No it's not a complete guide, you still need to understand Laravel a little bit. in routes.php you see
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Please read http://laravel.com/docs/5.0/controllers#implicit-controllers to understand how this works. From there on, you will be able to figure out how to do different routes for different auth-types
This is great. Just got it working with L5, I'm a bit new to laravel so the setup was a bit daunting, but once it works it works brilliantly.
Thank you for your great efforts.
I just have a question:
I have 3 user types: Admin, Company, and Employee. What I want to do is that I want to redirect each user type to a specific login page if they are trying to access a page without signing in.
For example:
- When a Company user account tries to access localhost/company, he must be redirected to localhost/company/login
- When an Employee tries to access localhost/employee, he must be redirected to localhost/employee/login
- When Admin tries to access localhost/admin, he must be redirected to localhost/admin/login
EDIT:
OK, now I was checking, and I found some properties in AuthenticatesAndRegistersUsers class that I think I will have to use such as: redirectPath, loginPath
How to implement these attributes in my Authentication controller, knowing that I have an authentication controller for each user type
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?
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...
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...
when i try it and it showed an error "FatalErrorException in RedirectIfAuthenticated.php line 35:
Call to a member function check() on null" which is mean that (Auth::admin() revers to null value). how to solve it?