为什么我的laravel 8自定义卫士登录总是返回false



这是config/auth.php

我已经编辑了学生提供商的授权

return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords
' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'student' => [
'driver' => 'session',
'provider' => 'students',
],

],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => AppModelsUser::class,
],
'students' => [
'driver' => 'eloquent',
'model' => AppModelsStudent::class,
],
// 'users' => [
//     'driver' => 'database',
//     'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
'students' => [
'provider' => 'students',
'table' => 'password_resets',
'expire' => 15,
],
],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| times out and the user is prompted to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/
'password_timeout' => 10800,
];

这是我的学生模型我使用用户模型作为基础编辑了学生模型

<?php
namespace AppModels;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
class Student extends Authenticatable
{
use HasFactory, Notifiable;
protected $guard = 'student';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name',
'last_name',
'email',
'password',
'birthdate',
'Course',
'Year',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}

这是我的学生登录控制器我的登录控制器返回登录表单,下面是总是返回false的登录尝试,因此它总是将我重定向回登录页面。

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;

use AppHttpControllersController;
// use AppHttpControllersAuthLoginController ;
// use IlluminateFoundationAuthAuthenticatesUsers;


class Student_loginController extends Controller
{
// public function __construct()
//     {
//         $this->middleware('guest:student')->except('logout');


//     }

public function index(){

return view('auth.student_login');
}

public function login(Request $request){

$this->validate($request,
[
'email' => 'required|email',
'password' => 'required',
]);



if(Auth::guard('student')->attempt($request->only('email','password'))){

return redirect()->back()->with('failed',' student not successfully logged in');

}
return redirect('student_dashboard')->with('success','student login');






}
}

这是我的路线

<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersRegistrationController;
use AppHttpControllersDashboardController;
use AppHttpControllersLoginController;
use AppHttpControllersLogoutController;
use AppHttpControllersWelcomeController;
use AppHttpControllersStudentController;
use AppHttpControllersStudent_loginController;
use AppHttpControllersstudent_dashController;


/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/',[WelcomeController::class, 'index'])->name('welcome');
Route::get('register',[RegistrationController::class, 'index'])->name('register');
Route::get('dashboard',[DashboardController::class, 'index'])
->name('dashboard')
->middleware('auth');
Route::get('student_dashboard',[student_dashController::class, 'index'])
->name('student_dashboard');
Route::get('login',[LoginController::class, 'index'])
->name('login')
->middleware('guest');
Route::get('logout',[LogoutController::class, 'logout'])->name('logout');
Route::get('student_login',[Student_loginController::class, 'index'])->name('student_login');
Route::get('student',[StudentController::class, 'index'])->name('student')->middleware('auth');;
Route::get('student/{student_ID}', [StudentController::class, 'show'])->name('student.showstudent')->middleware('auth');;

Route::post('register',[RegistrationController::class, 'store'])->name('register.store');
Route::post('store_student', [RegistrationController::class, 'store_student'])->name('store_student')->middleware('auth');
Route::post('student_login',[Student_loginController::class, 'login'])->name('student_login.login');
Route::post('login',[LoginController::class, 'login'])->name('login.login');
Route::post('student',[StudentController::class, 'store'])->name('student.store')->middleware('auth');;

Route::delete('student/{student_ID}',[StudentController::class, 'destroy'])->name('student.destroy')->middleware('auth');;
Route::put('showstudent/{student_ID}',[StudentController::class, 'edit'])->name('showstudent.edit')->middleware('auth');;


// Route::get('/', function () {
//     return view('welcome');
// });
if(Auth::guard('student')->attempt($request->only('email','password'))){
return redirect()->back()->with('failed',' student not successfully logged in');}

当登录为true时,您的重定向返回失败消息。登录失败时,您已重定向回失败消息。你必须这样写逻辑。

if(!Auth::guard('student')->attempt($request->only('email','password'))){
return redirect()->back()->with('failed',' student not successfully logged in');}

最后,您必须使用中间件auth及其防护来保护student_dashboard路由授权:学生

Route::middleware('auth:student')->group(function(){
Route::get('student_dashboard',[student_dashController::class, 'index'])
->name('student_dashboard');}

或者返回仪表板控制器中的中间件,或者像这样的方法

public function __construct()
{
$this->middleware('auth:student');
}

相关内容

最新更新