Viv*_*vek 2 php function laravel laravel-5
我正在尝试覆盖登录控制器中的经过身份验证的方法,但是不知何故它无法正常工作。我只是尝试简单地dd(); 但仍然不起作用。
下面是我的功能代码:
public function authenticated(Request $request, $user)
{
dd("hi");
}
Run Code Online (Sandbox Code Playgroud)
我实际上希望做的如下,但是为了简单起见,我有dd(); 在功能上。
public function authenticated(Request $request, $user)
{
if (!$user->verified) {
auth()->logout();
return back()->with('warning', 'You need to confirm your account. We have sent you an activation code, please check your email.');
}
return redirect()->intended($this->redirectPath());
}
Run Code Online (Sandbox Code Playgroud)
整个控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function login(Request $request)
{
if (Auth::attempt(['email' => $request->email, 'password' => $request->password, 'isActive' => '1']))
{
return view('homepage');
}
else
{
return $this->sendFailedLoginResponse($request, 'auth.failed_status');
}
}
protected function authenticated(Request $request, $user)
{
dd("HI");
// auth()->logout();
return back()->with('warning', 'You need to confirm your account. We have sent you an activation code, please check your email.');
// if(!$user->verified)
// {
// auth()->logout();
// // Auth::logout();
// // \Auth::guard('web')->logout();
// // added logout here
// return back()->with('warning', 'You need to confirm your account. We have sent you an activation code, please check your email.');
// }
// return redirect()->intended($this->redirectPath());
}
}
Run Code Online (Sandbox Code Playgroud)
请忽略控制器中经过身份验证的函数中的多余注释代码。
Chi*_*ung 11
那是因为您要覆盖登录功能,因此永远不会调用经过身份验证的功能。
如果您看一下特征:
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,该函数sendLoginResponse就是调用该authenticated函数的函数。
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
Run Code Online (Sandbox Code Playgroud)
因此,在您的情况下,应该重新生成会话并清除尝试:
return $this->sendLoginResponse($request);
Run Code Online (Sandbox Code Playgroud)
或者,如果您想直接跳到已验证的功能,请执行以下操作:
return $this->authenticated($request, auth()->user());
Run Code Online (Sandbox Code Playgroud)
您的函数应如下所示:
public function login(Request $request)
{
if (Auth::attempt(['email' => $request->email, 'password' => $request->password, 'isActive' => '1']))
{
// Updated this line
return $this->sendLoginResponse($request);
// OR this one
// return $this->authenticated($request, auth()->user());
}
else
{
return $this->sendFailedLoginResponse($request, 'auth.failed_status');
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2913 次 |
| 最近记录: |