Laravel 5.4 - 电子邮件验证在单击电子邮件中的验证链接时,未传递或发送令牌令牌。

时间:2018-04-04 18:40:10

标签: php laravel laravel-5.4 email-verification

我只想将验证电子邮件发送给管理员注册的用户。只有管​​理员才能注册新用户。但是每次我注册时都会发送电子邮件,但是当你点击验证链接时会出现routecollection错误我猜想当每次在系统中注册新用户时,点击都没有通过或发送了令牌。

用户控制器     UserController.php     命名空间App \ Http \ Controllers;

import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { JhiPaginationUtil } from '.';

@Injectable()
export class JhiResolvePagingParams implements Resolve<any> {

    constructor(private paginationUtil: JhiPaginationUtil) { }

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        const page = route.queryParams['page'] ? route.queryParams['page'] : '1';
        const defaultSort = route.data['defaultSort'] ? route.data['defaultSort'] : 'id,asc';
        const sort = route.queryParams['sort'] ? route.queryParams['sort'] : defaultSort;
        return {
            page: this.paginationUtil.parsePage(page),
            predicate: this.paginationUtil.parsePredicate(sort),
            ascending: this.paginationUtil.parseAscending(sort)
        };
    }
}

邮件
    verifyEmail.php - laravel中的邮件功能

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Mail;
use App\Mail\verifyEmail;
//imageuplod
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::all();

        //Load all users on the table and pass the users
        $users = User::where(['archive'=>1])->orderBy('id')->get();
        return view('usercrud.index')->with('users', $users);      
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
        $usersC = new User;
        $url = '';
        $usersC->position = $request->companyPos;
        $usersC->empid = $request->employeeid;
        $usersC->name = $request->fullname;
        $usersC->email = $request->email;
        $usersC->password = bcrypt($request->password);
        $usersC->roles = $request->role_id;
        //token and status for image upload
        $usersC->verifyToken = Str::random(40);
        $usersC->status = 0;

        //image uplod
        if(Input::hasFile('images')){

            $file = Input::file('images');
            $file->move(public_path(). '/uploads/', 
            $file->getClientOriginalName());

            $url = '/uploads/'. $file->getClientOriginalName();


        } 
        $usersC-> image = $url;
        $usersC -> save();


        $users = User::all();
         $users = User::where(['archive'=>1])->orderBy('id')->first();

        //email verification


        $thisUser = User::findOrFail($users->id);
         $this->sendEmail($thisUser);
        //$users = User::where(['archive'=>1])->orderBy('id')->get();

        return redirect()->to('userIndex')->with('users', $users);


    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $users = User::where(['id'=>$id])->first();


        // dd($users);

         return view('usercrud.edit',compact('users'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $usersUp = new User;
        $url = '';
        $password = bcrypt($request->editpassword);
        if(Input::hasFile('images')){

            $file = Input::file('images');
            $file->move(public_path(). '/uploads/', $file- 
            >getClientOriginalName());
            $url = '/uploads/'. $file->getClientOriginalName();


        } 


        // dd($request->editcompanyPos);

        $usersUp = User::where('id',$id)
            ->update(['position'=>$request- 
            >editcompanyPos,'empid'=>$request->editemployeeid, 
            'name'=>$request->editfullname,'email'=>$request- 
            >editemail,'password'=>$password,'roles'=>$request->editrole_id, 
            'image'=>$url]);

        $users = User::all();
        $users = User::where(['archive'=>1])->orderBy('id')->get();

        return redirect()->to('userIndex')->with('users', $users);
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $userDel = User::where('id',$id)->update(['archive'=>0]);

        $users = User::all();
        $users = User::where(['archive'=>1])->orderBy('id')->get();

        return redirect()->to('userIndex')->with('users', $users);

    }
    public function verifyEmailFirst(Request $request)
    {
        //
        return view('usercrud.verifyEmailFirst');
    }
    public function sendEmail($thisUser)
    {
        Mail::to($thisUser['email'])->send(new verifyEmail($thisUser));
    }

    public function sendEmailDone($email, $verifyToken)
    {
        $user = User::where(['email'=>$email, 'verifyToken'=>$verifyToken])- 
    >first();
        //return $user;
        if ($user){
            return user::where(['email'=>$email, 
    'verifyToken'=>$verifyToken])- 
     >update(['status'=>'1','verifyToken'=>NULL]);

        }else{
            return 'user not found';
        }

    }



}

路线
    web.php - route

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;


class verifyEmail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $user;
    public function __construct(User $user)
    {
        //
        $this->user=$user;

    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('usercrud.sendView');
    }
}

homecontroller - 处理帐户登录等的那个

    Route::get('/', function () {
    return view('bgcbus');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/home/submitsurvey',
['uses'=>'HomeController@submitsurvey','as'=>'surveysubmit']);


Route::resource('/users', 'UserController');

//UserCreate 
Route::post('/create','UserController@create');
Route::get('/userIndex','UserController@index');
//User Update
Route::match(['get','post'],'/userUpdate/{id}','UserController@update');
//User Edit 
Route::get('/userEdit/{id}','UserController@edit');
//User Delete
Route::get('/userDelete/{id}','UserController@destroy');

//Bus 
Route::resource('/buses', 'BusController');
//Bus Create 
Route::post('/buscreate','BusController@create');
Route::get('/busindex','BusController@index');
//Bus Edt
Route::get('/busEdit/{id}','BusController@edit');
//Bus Update
Route::match(['get','post'],'/busUpdate/{id}','BusController@update');
//Bus Delete
Route::get('/busDelete/{id}','BusController@destroy');


//Bus Routes
Route::resource('/bgcroutes', 'BgcrouteController');
//Bus Route create
Route::post('/bgcroutecreate', 'BgcrouteController@create');
Route::get('/bgcrouteindex', 'BgcrouteController@index');
//Bus Route Edit
Route::get('/bgcrouteEdit/{id}','BgcrouteController@edit');
//Bus Route update
Route::match(['get','post'],'/bgcrouteUpdate/{id}',
'BgcrouteController@update');
//Bus Route Delete
Route::get('/bgcrouteDelete/{id}','BgcrouteController@destroy');

//Questions
Route::resource('/questions', 'SurveyquestionController');
//Questions create
Route::post('/questioncreate', 'SurveyquestionController@create');
Route::get('/questionindex', 'SurveyquestionController@index');
//Questions Edit
Route::get('/questionEdit/{id}','SurveyquestionController@edit');
//Questions update
Route::match(['get','post'],'/questionUpdate/{id}',
'SurveyquestionController@update');
//Questions Delete
Route::get('/questionDelete/{id}','SurveyquestionController@destroy');

//Applying Active Value
Route::post('/question/apply',

// Selecting Emoji
Route::get('/home/surveyvote',
['uses'=>'HomeController@surveyvote','as'=>'surveyvoting']);



//2ndQuestions
Route::resource('/scndquestions', 'QuestionsurveyController');
//2ndQuestions create
Route::post('/scndquestionscreate', 'QuestionsurveyController@create');
Route::get('/scndquestionsindex', 'QuestionsurveyController@index');
//2ndQuestions Edit
Route::get('/scndquestionsEdit/{id}','QuestionsurveyController@edit');
//2ndQuestions update
Route::match(['get','post'],'/scndquestionsUpdate/{id}',
'QuestionsurveyController@update');
//2ndQuestions Delete
Route::get('/scndquestionsDelete/{id}','QuestionsurveyController@destroy');

//Applying Active Value in 2nd Question
Route::post('/scndquestion/setapply', ['uses' =>
 'QuestionsurveyController@setapply','as'=>'setapplyscndQuestion']);



//Dashboard

Route::get('/driverDashboard', 'DashboardfirstController@index');

Route::post('/driverDashboard/data',

Route::get('/driverDashboardAggregated', 
'DashboardfirstController@secondindex');

Route::post('/piechartaggregated', 
['uses'=>'ChartsController@getaggregated','as'=>'getaggregateddata']);

//History

Route::get('/history', 'HistoryController@index');

//verification email
Route::get('verify/{email}/{verifyToken}','UserController@sendEmailDone')
->name('sendEmailDone');

浏览

<?php

namespace App\Http\Controllers;

use App\Answer;
use App\User;
use Auth;
use Illuminate\Http\Request;
use Controller\UserController;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::where('ID', Auth::id())->first();

        //if ($users->status==1){
                if ($users->roles == 1){
                return view('admins');

                }elseif ($users->roles == 2) {
                     return view('dispatcher');
                }
                return view('survey');

            //}
            //    return view('usercrud.verifyEmailFirst');


        // return view('home');
    }

    function surveyvote(Request $request){
        Answer::insert([
            'driverid'=>$request->driverid,
            'busid'=>$request->busid,
            'routeid'=>$request->routeid,
            'questionid'=>$request->questionid,
            'responses'=>$request->responseid,
            'created_at'=>date('Y-m-d h:i:s')
        ]);
        return 'Successful';
    }

}

User.php
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Str;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'position', 'roles', 'verifyToken'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

1 个答案:

答案 0 :(得分:0)

您只需要像这样更改sendEmailDone功能: -

public function sendEmailDone($email, $verifyToken)
{
    $user = User::where(['email'=>$email,'verifyToken'=>$verifyToken])->first();
    $user->status = 1;
    $user->verifyToken = Null;
    if($user->save())
    {
         return view('xyz_view',['user'=>$user]);
    }
    else
    {
        return 'user not found';
    }

}