gpt4 book ai didi

Laravel 5 中间件身份验证总是失败并重定向到登录

转载 作者:行者123 更新时间:2023-12-04 18:01:34 25 4
gpt4 key购买 nike

我正在对我的用户进行身份验证,如果凭据正确,我会将他重定向到仪表板。我想保护仪表板路由并添加中间件身份验证,但现在它总是重定向到登录页面。

Routes.php

Route::get('login', array('uses' => 'HomeController@showLogin'));
Route::post('login', array('uses' => 'HomeController@doLogin'));
Route::get('logout', array('uses' => 'HomeController@doLogout'));

Route::group(['middleware' => 'auth'], function() {
Route::get('/', function () {
return view('dashboard');
});
Route::get('dashboard', function () {
return view('dashboard');
});
});

HomeController.php

public function showLogin(){
return View::make('login');
}

public function doLogin(Request $request){
$rules = array(
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
);
$validator = Validator::make($request::all(), $rules);
if ($validator->fails()) {
return Redirect::to('login')
->withErrors($validator)
->withRequest($request::except('password'));
}
else {
$userdata = array(
'email' => $request::get('email'),
'password' => $request::get('password')
/*'password' => Hash::make($request::get('password'))*/
);
if (Auth::attempt($userdata)) {
$userid = Auth::id();
return redirect()->intended('/');
} else {
return Redirect::to('login');
}
}
}

public function doLogout()
{
Auth::logout();
return Redirect::to('login');
}

中间件Authenticate.php

public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}

return $next($request);
}

中间件RedirectIfAuthenticated.php

public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/');
}

return $next($request);
}

最佳答案

我不确定为什么你的代码不起作用,但你可以尝试替换:

if (Auth::attempt($userdata)) {
$userid = Auth::id();
return redirect()->intended('/');
}

与:

if (Auth::attempt($userdata)) {
$userid = Auth::id();
return redirect('dashboard');
}

来自API Docs of intended method :

Create a new redirect response to the previously intended location.

给你一些错误,因为它返回到以前的位置而不是下一个位置。


更新 1:

我会采用以下方法。

制作你自己的名为 UserAlreadyLoggedIn 的中间件

php artisan make:middleware UserAlreadyLoggedIn

打开 UserAlreadyLoggedIn.php 并使用以下代码更新句柄方法:

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(auth()->check()) {
return $next($request);
}

return redirect('login');
}

$routeMiddleware 数组中的 app/Http/Kernel.php 文件中注册:

$routeMiddleware = [
'user_already_logged_in' => \App\Http\Middleware\UserAlreadyLoggedIn::class,
];

通过制作UserSessionsController

在 Controller 中分离已经登录的用户
php artisan make:controller UserSessionsController --plain

UserSessionsController 中放置 __constructor 方法:

/**
* Check if the user is already logged in.
*
* @return void
*/
public function __construct()
{
$this->middleware('user_already_logged_in');
}

routes.php

Route::get('login', 'HomeController@showLogin');
Route::post('login', 'HomeController@doLogin');
Route::get('logout', 'HomeController@doLogout');

// Replace the dashboard code inside the dashboard method..
Route::get('dashboard', 'UserSessionsController@dashboard');

同样,我会创建一个名为 UserIsAGuest 的中间件,并且会替换 handle 方法中的 if block :

if(auth()->guest()) {
return $next($request);
}
return redirect('dashboard');

然后在 HomeController__construct 方法中:

/**
* Check if the user is already logged in.
*
* @return void
*/
public function __construct()
{
// Register the middleware in Kernel.php file
$this->middleware('user_is_guest');
}

希望对您有所帮助。快乐编码。干杯。

关于Laravel 5 中间件身份验证总是失败并重定向到登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34527141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com