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.

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line