我遇到一个问题,登录页面会将我重定向回登录而不是转到主页。
我的路线档案:
Route::group(['middleware' => ['auth']], function () {
Route::get('/', function () {
return redirect()->route('dash');
});
Route::get('dash', 'DashController@index')->name('dash');
});
Route::get('login', ['as' => 'login', 'uses' => 'LDAPAuthController@index']);
Route::post('login', 'LDAPAuthController@login');
其他所有内容都设置为正常的默认设置:
RedirectIfAuthenticated Middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
我的登录控制器:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use App\Services\CakePHPHash;
use App\Users;
use Redirect;
use Auth;
use View;
class LDAPAuthController extends Controller {
public function login(Request $request) {
$user = Users::where('email', strtolower($request->email))->first();
$hash = new CakePHPHash;
if(($hash->hash($request->password, null, true)) !== $user->password) {
return back()->with('error', 'Username and/or password are incorrect.');
}
Auth::login($user);
return Redirect::to('/');
}
public function logout() {
Auth::logout();
Session::flush();
return back()->with('error', 'You just logged out.');
}
public function index(Request $request) {
return View::make('auth.login');
}
}
我的样本短跑控制器:
<?php
namespace App\Http\Controllers;
use \App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Auth;
class DashController extends Controller {
public function index() {
return view('Home.home');
}
}
我简化了我的用户模型,只更改了主要名称和表格名称:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Session;
use Auth;
class Users extends Authenticatable
{
protected $table = 'users';
protected $primaryKey = 'user_id';
public function getRememberToken()
{
return null; // not supported
}
public function setRememberToken($value)
{
// not supported
}
public function getRememberTokenName()
{
return null; // not supported
}
/**
* Overrides the method to ignore the remember token.
*/
public function setAttribute($key, $value)
{
$isRememberTokenAttribute = $key == $this->getRememberTokenName();
if (!$isRememberTokenAttribute)
{
parent::setAttribute($key, $value);
}
}
}
这不是我第一次在Laravel中制作应用程序,但这让我头疼不前。我关注的另一个信息是$ user提供了正确的用户和user_id,但login_web_ session正在从debugbar中发出一个不同的(不是正确的user_id)。但是,Auth :: check()返回true。