当我尝试登录时,以下操作不起作用:
Auth::guard('pia')->attempt(['email' => $_POST['email'],
'password'=>$_POST['password']])
如果我删除guard('pia')
然后它正在运行,但有时它redirects
默认为Laravel login page
,尽管用户已登录,我不想要。
提供route
的代码,auth.php
和AuthController.php
。
我为不同类型的用户使用不同的AuthControllers,他们的数据库表也是分开的,例如pia
表pia
,管理员表管理员等。
routes.php
Route::group(['prefix' => 'pia'], function() {
//Login Routes...
Route::get('login','Pia\AuthController@showLoginForm');
Route::post('login','Pia\AuthController@login');
Route::get('logout','Pia\AuthController@logout');
// Registration Routes...
Route::get('register', 'Pia\AuthController@showRegistrationForm');
Route::post('register', 'Pia\AuthController@register');
});
config/auth.php
'pia' => [
'driver' => 'session',
'provider' => 'pia',
],
<?php
namespace App\Http\Controllers\Pia;
use App\Pia;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use DB;
use Mail;
use Auth;
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, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $guard = 'pia';
protected $redirectTo = '/pia/dashboard';
protected $redirectAfterLogout = '/pia/login';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
public function showLoginForm(){
if(view()->exists('auth.authenticate')){
return view('auth.authenticate');
}
return view('pia.auth.login');
}
public function showRegistrationForm(){
return view('pia.auth.register');
}
public function login(Request $request)
{
if (Auth::guard('pia')->attempt(['email' => $_POST['email'],'password'=>$_POST['password']])) {
return redirect('/pia/dashboard');
}else{
return redirect('/pia/login')->with('warning', 'Problem, try again!'); //
}
}
...
}