我想在每次用户登录帐户时创建一个 2 因素身份验证。为此,我希望添加 OTP 功能,将 OTP 发送到用户注册的电子邮件 ID,然后重定向到仪表板。 我的路线文件:
'''
<?php
/*
|--------------------------------------------------------------------------
| Backpack\Base Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are
| handled by the Backpack\Base package.
|
*/
Route::group(
[
'namespace' => 'Backpack\CRUD\app\Http\Controllers',
'middleware' => config('backpack.base.web_middleware', 'web'),
'prefix' => config('backpack.base.route_prefix'),
],
function () {
// if not otherwise configured, setup the auth routes
if (config('backpack.base.setup_auth_routes')) {
// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('backpack.auth.login');
Route::post('login', 'Auth\LoginController@login');
//Route for otp form -by harshita aggarwal (26-June-2021)
Route::get('otp', 'Auth\LoginController@otpForm')->name('admin.otp');
Route::post('otp','Auth\LoginController@otpLogin')->name('backpack.auth.login.otp');
Route::get('logout', 'Auth\LoginController@logout')->name('backpack.auth.logout');
Route::post('logout', 'Auth\LoginController@logout');
// Registration Routes...
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('backpack.auth.register');
Route::post('register', 'Auth\RegisterController@register');
// if not otherwise configured, setup the password recovery routes
if (config('backpack.base.setup_password_recovery_routes', true)) {
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('backpack.auth.password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('backpack.auth.password.reset.token');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('backpack.auth.password.email');
}
}
// if not otherwise configured, setup the dashboard routes
if (config('backpack.base.setup_dashboard_routes')) {
Route::get('dashboard', 'AdminController@dashboard')->name('backpack.dashboard');
Route::get('/', 'AdminController@redirect')->name('backpack');
}
// if not otherwise configured, setup the "my account" routes
if (config('backpack.base.setup_my_account_routes')) {
Route::get('edit-account-info', 'MyAccountController@getAccountInfoForm')->name('backpack.account.info');
Route::post('edit-account-info', 'MyAccountController@postAccountInfoForm')->name('backpack.account.info.store');
Route::post('change-password', 'MyAccountController@postChangePasswordForm')->name('backpack.account.password');
}
});'''
我的登录控制器:
'''
<?php
namespace Backpack\CRUD\app\Http\Controllers\Auth;
use Backpack\CRUD\app\Library\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use App\Mail\otpLogin;
use Illuminate\Support\Facades\Mail;
use App\User;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
protected $data = []; // the information we send to the view
/*
|--------------------------------------------------------------------------
| 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 {
logout as defaultLogout;
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$guard = backpack_guard_name();
$this->middleware("guest:$guard", ['except' => 'logout']);
// ----------------------------------
// Use the admin prefix in all routes
// ----------------------------------
// If not logged in redirect here.
$this->loginPath = property_exists($this, 'loginPath') ? $this->loginPath
: backpack_url('login');
// Redirect here after successful login.
$this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo
: backpack_url('dashboard');
// Redirect here after logout.
$this->redirectAfterLogout = property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout
: backpack_url('login');
}
/**
* Return custom username for authentication.
*
* @return string
*/
public function username()
{
return backpack_authentication_column();
}
/**
* The user has logged out of the application.
*
* @param \Illuminate\Http\Request $request
*
* @return mixed
*/
protected function loggedOut(Request $request)
{
return redirect($this->redirectAfterLogout);
}
/**
* Get the guard to be used during logout.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return backpack_auth();
}
/**
* Function attempLogin added by Harsh Agarwal on 10-Apr-2021 to add user status check in login attempt
* This login is a default function to Auth class which is being overridden from here
*/
protected function attemptLogin(Request $request)
{
$credentials = $this->credentials($request);
$credentials['status'] = 1; //Adding User Status column in login attempt to verify that user is enabled
return $this->guard()->attempt(
$credentials, $request->filled('remember')
);
}
/*
* @purpose: to handle login redirections and send otp to user via mail
* @author: Harshita Aggarwal
* @Date: 26-June-2021
*/
public function login(Request $request){
$this->validateLogin($request);
//check whether the user entered an email id or not
if(!empty($request->email)){
$user = User::where('email',$request->email)->pluck('status')->toArray();
if($user !=null){//if user is registered
//if status of user is active or 1
if($user[0]==1){
$otp = rand(1000, 9999);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
//Mail::to($request->email)->send(new otpLogin($otp));
//User::where('email',$request->email)->update(['otp'=>$otp]);
// Authentication passed...
//return redirect(backpack_url('otp'))->withInput();
return redirect()->route('admin.otp');
}else{
\Alert::error(trans('base.invalid_credentials'))->flash();
//Redirect to the page where request has been raised
return redirect()->back();
}
}
else{
\Alert::error(trans('base.user_status_deactive'))->flash();
//Redirect to the page where request has been raised
return redirect()->back();
}
}
else{
\Alert::error(trans('base.user_not_registered'))->flash();
//Redirect to the page where request has been raised
return redirect()->back();
}
}
}
/*
* @purpose: to redirect to otp form for login verification
* @author: Harshita Aggarwal
* @Date:26-June-2021
*/
public function otpForm(Request $request){
$val = $request->old('email');
$val1 = $request->old('password');
return view(backpack_view('auth.otp'),['val'=>$val,'val1'=>$val1]);
}
/*
*
*/
public function otpLogin(Request $request){
$otp = User::where('email',$request->email)->pluck('otp')->toArray();
if($request->otp == $otp[0]){
User::where('email',$request->email)->update(['otp'=>0]);
// var_dump('done');die;
//return redirect()->intended('dashboard');
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
return redirect()->intended('dashboard');
}
}
else{
dd('no');
}
}
}
'''
重定向到 otp 刀片文件失败。因为它显示了 404 状态代码。可能的解决方案是什么?