我是laravel的新手,我在使用laravel提供的laravel Auth命令。我具有在其中重置用户密码并在电子邮件中发送令牌的功能,当我单击链接时,两个令牌都相同,但是我收到密码重置令牌无效错误,因此我检查了令牌是否相同
我已经实现了与laravel提供的功能相同的功能,并且我在重置密码控制器中的控制器上进行了一些自定义,我在URL和电子邮件中收到的令牌是相同的,但是我无法重置密码。
RegisterController.php
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Str;
use App\Mail\verifyEmail;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Auth\Events\Registered;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255',],
'type_of_user' => ['required'],
// 'contact_number' => ['required', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
],[
"first_name.required"=>"Please enter :attribute",
"last_name.required"=>"Please enter :attribute",
"type_of_user.required"=>"Please select :attribute",
// "contact_number.required"=>"Please enter :attribute",
// "contact_number.unique"=>"this :attribute is already in use",
"email.required"=>"Please enter :attribute",
"email.unique"=>"this :attribute is already in use",
"password.required"=>"Please enter :attribute",
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
Session::flash('success','you have successfully registered on the website please verify your email to activate your account!');
// session()->flash('registered','you have successfully registered on the website please verify your email to activate your account!!!');
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'contact_number' => $data['contact_number'],
'role_id' => $data['type_of_user'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'verifyToken' => Str::random(40)
]);
$thisUser = User::findOrFail($user->id);
$this->sendVerificationMail($thisUser);
return $user;
}
// public function register(Request $request)
// {
// $this->validator($request->all())->validate();
//
// event(new Registered($user = $this->create($request->all())));
//
// $this->guard()->login($user);
// Session::flash('success', 'you have successfully registered on the website please verify your email to activate your account!!!');
//
//
//
// return $this->registered($request, $user)
// ?: redirect($this->redirectPath());
// }
public function register(\Illuminate\Http\Request $request)
{
// validate the form
$this->validator($request->all())->validate();
// add the user
$this->create($request->all());
// redirect user
// return redirect($this->redirectPath());
return redirect('/login')->with("success","You have successfully registered on the website please verify your email to activate your account!!!");
}
public function sendVerificationMail($thisUser)
{
Mail::to($thisUser['email'])->send(new verifyEmail($thisUser));
}
public function verifyEmail()
{
return view('email.verifyEmailfirst');
}
public function email_verified(){
return view('email.email-verified');
}
public function sendEmailDone($email,$verifyToken){
$current_date_time = Carbon::now()->toDateTimeString();
$user = User::where(["email"=>$email,"verifyToken"=>$verifyToken])->first();
if($user)
{
$verified = User::where(["email"=>$email,"verifyToken"=>$verifyToken])->update(["status"=>1,"verifyToken"=>null,"email_verified_at"=> $current_date_time ]);
if($verified)
{
return redirect ('email-verified');
}
}
}
}
Routes.php
Route::get('/verify-email', 'Auth\RegisterController@verifyEmail')->name('verifyEmail');
Route::get('/verify/{email}/{verifyToken}', 'Auth\RegisterController@sendEmailDone')->name('sendEmailDone');
Route::get('/email-verified', 'Auth\RegisterController@email_verified')->name('email_verified');
当我提交表单时,它应该更改密码,但是说重置密码令牌无效。请让我知道我要去哪里了。
谢谢。