简体   繁体   中英

Laravel change password right after email confirmation view not working

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) Route [user.setPassword] not defined. (View: public_html/server2/resources/views/user/setpassword.blade.php) error.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM