doushai7225 2015-06-25 11:09
浏览 16
已采纳

Laravel在电子邮件确认视图无效后立即更改密码

When a user is created a random password (and a auth code) will be created and send with an email to the user.

When clicked on the link in the email the user status will be 1 (so active) and the user will be able to change his password right away.

Now it doesn't work as I want to.

UserController:

public function store(CreateUserRequest $request, User $user, Attribute $attribute)
// some unnecessary code

if ((Input::get('usertype_id')) > 1) {
    $randomPassword = str_random(8);
    $user->password = Hash::make($randomPassword);

    $authentication_code = str_random(12);
    $user->authentication_code = $authentication_code;

    $user->active = 0;
};

$user->save();

if ((Input::get('usertype_id')) > 1) {
    // Email sturen met verficatie code
    $email = Input::get('email');

    Mail::send('emails.user', ['user' => $user, 'password' => $randomPassword, 'authentication_code' => $authentication_code], function ($message) use ($email) {
        $message->to($email, 'Lilopel')->subject('Lilopel: Verify your account!');
    });
};


public function confirmUser($authentication_code)
{
    if (!$authentication_code)
    {
        return 'auth code not found!';
    }

    $user = User::where('authentication_code', '=', $authentication_code)->first();

    if (!$user)
    {
        return 'user not found!';
    }

    $user->active = 1;
    $user->save();

    Session::put('user_id', $user->id);

    return view('user.setpassword', ['user' => $user]);
    //return redirect()->route('user.setPassword', [$user_id]);
}


public function setPassword(SetPasswordRequest $request)
{
    $user_id = Session::get('user_id');


    $user = $this->user->find($user_id);

    $user->fill($request->only('password'));

    $user->save();
}

Route:

Route::get('user/verify/{authenticationCode}', 'UserController@confirmUser');

Route::get('user/password', 'UserController@setPassword');

View:

{!! Form::model($user, ["route"=>['user.setPassword', $user->id] , "method" => 'PATCH']) !!}

    <div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}">
        {!! Form::label('password', trans('common.password'), ['class' => 'form-label col-sm-3 control-label
        text-capitalize']) !!}
        <div class="col-sm-6">
            {!! Form::password('password', ['name' => 'password', "class"=>"form-control","placeholder" =>
            trans('common.password') ]) !!}
            {!! $errors->first('password', '<span class="help-block">:message</span>') !!}
        </div>
    </div>
    <div class="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}">
        {!! Form::label('password_confirmation', trans('common.confirmpassword'), ['class' => 'form-label
        col-sm-3 control-label text-capitalize']) !!}
        <div class="col-sm-6">
            {!! Form::password('password_confirmation', ['name' => 'password_confirmation',
            "class"=>"form-control","placeholder" => trans('common.confirmpassword') ]) !!}
            {!! $errors->first('password_confirmation', '<span class="help-block">:message</span>') !!}
        </div>
    </div>

{!! Form::submit( trans('common.edit'), ["class"=>"btn btn-primary text-capitalize center-block   "]) !!}

{!! Form::close() !!}

Email links works, the status of the user gets active, but then the .blade will give a Route [user.setPassword] not defined. (View: public_html/server2/resources/views/user/setpassword.blade.php) error.

  • 写回答

1条回答 默认 最新

  • duanhuancong1969 2015-06-25 11:34
    关注

    work togetherTo use the route as you do, you need a named route.

    Change this

    Route::get('user/password', 'UserController@setPassword');
    

    to this

    Route::get('user/password', [
        'as' => 'user.setPassword',
        'uses' => 'UserController@showProfile'
    ]);
    

    Also, make sure the HTTP verbs of the route and your form's method work together.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源