Halnex
Halnex

Reputation: 4526

Laravel 5: password gets changed (or blanked out) everytime I edit my profile

Everytime I edit my profile, while leaving out the password fields empty, on submit my password changes to a different Hash value.

If I submit the password in plain text to the database and try to edit my profile while leaving the password fields empty, it will delete my current password and leave it blank.

public function update(Request $request, User $user)
{
    $user = User::where('id', '=', Auth::id())->first();
    $user->name = Input::get('name');
    $user->email = Input::get('email');
    $user->password = Hash::make(Input::get('password'));
    $user->save();
}

{!! Form::model($user, ['method' => 'PATCH', 'action' => ['ProfilesController@update', $user->id]]) !!}
<fieldset>
    <div class="control-group">
        <!-- Username -->
        <label class="control-label"  for="name">Username</label>
        <div class="controls">
            <input type="text" id="name" name="name" placeholder="" value="{{ $user->name }}" class="form-control">
            <p class="help-block">Username can contain any letters or numbers, without spaces</p>
        </div>
    </div>

    <div class="control-group">
        <!-- E-mail -->
        <label class="control-label" for="email">E-mail</label>
        <div class="controls">
            <input type="text" id="email" name="email" placeholder="" value="{{ $user->email }}" class="form-control">
            <p class="help-block">Please provide your E-mail</p>
        </div>
    </div>

    <div class="control-group">
        <!-- Password-->
        <label class="control-label" for="password">Password</label>
        <div class="controls">
            <input type="password" id="password" name="password" placeholder="" class="form-control">
            <p class="help-block">Password should be at least 4 characters</p>
        </div>
    </div>

    <div class="control-group">
        <!-- Password -->
        <label class="control-label"  for="password_confirmation">Password (Confirm)</label>
        <div class="controls">
            <input type="password" id="password_confirmation" name="password_confirmation" placeholder="" class="form-control">
            <p class="help-block">Please confirm password</p>
        </div>
    </div>

    <div class="control-group">
        <!-- Button -->
        <div class="controls">
            <button class="btn btn-success">Register</button>
        </div>
    </div>
</fieldset>
{!! Form::close() !!}

Upvotes: 1

Views: 83

Answers (1)

cre8
cre8

Reputation: 13562

You have to check if the input field is empty:

if(!empty(Input::get('password')) {
    $user->password = Hash::make(Input::get('password'));
}

For more information how the hash function works look at this page: https://mnshankar.wordpress.com/2014/03/29/laravel-hash-make-explained/

Upvotes: 3

Related Questions