Skip to content

how to change password in laravel 4 with auth

Authentication on laravel libraries can indeed be called a poweful. With easy implementation we are presented with the availability of complete auth configuration. The authentication configuration file is located at app/config/auth.php, which contains several well documented options for tweaking the behavior of the authentication facilities.

With this literature, we can make a variety of implementations. such as storing passwords, authenticating users, protecting the routes, up reminders and reset passwords.

But in the documentation laravel there a way to change the password for the user or admin. therefore, in this tutorial I will give an example of how to change the password on laravel 4 using the auth library.

 

create a route to the change password page

Route::any('user/changepassword', 'Meniqa\\Employee\\Controllers\\[email protected]');

 

on the controller class for a function to display the change password page then pass it to view page

public function changePassword() {
    if (Request::isMethod('post')){
        
    }else{
        $this->layout->content = View::make('employee::user.changePassword');
    }
}

 

@section('content')
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                <a href="{{ url('admin/dashboard') }}"><i class="fa fa-home"></i></a> /
                Ubah Sandi
            </header>
        </section>
    </div>
</div>

@include('layouts.backend.messageAlert')

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                Ubah Sandi
            </header>
            <div class="panel-body">
                {{ Form::open(array('url' => Request::url(), 'class' => 'form-horizontal tasi-form', 'role' => 'form', 'method'=>'post')) }}
                <div class="form-group">
                    <label for="last_password" class="col-lg-2 col-sm-2 control-label">Sandi lama</label>
                    <div class="col-md-3 col-xs-11">
                        <input class="form-control form-control-inline input-medium" type="password" value="" name="last_password" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="new_password" class="col-lg-2 col-sm-2 control-label">Sandi Baru</label>
                    <div class="col-md-3 col-xs-11">
                        <input class="form-control form-control-inline input-medium" type="password" value="" name="new_password" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="verify_password" class="col-lg-2 col-sm-2 control-label">Konfirmasi Sandi Baru</label>
                    <div class="col-md-3 col-xs-11">
                        <input class="form-control form-control-inline input-medium" type="password" value="" name="verify_password" />
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-offset-2 col-lg-10">
                        <button type="submit" class="btn btn-success">Submit</button>
                        <button type="reset" class="btn btn-default">Reset</button>
                    </div>
                </div>
                {{ Form::close() }}
            </div>
        </section>
    </div>
</div>
@stop

 

update the class controller to handle post action from view

public function changePassword() {
    if (Request::isMethod('post')){
        $rules = array(
            'last_password'     => 'required',
            'new_password'      => 'required',
            'verify_password'   => 'required|same:new_password',
        );

        $attributes = array(
            'last_password'     => 'Sandi Lama',
            'new_password'      => 'Sandi Baru',
            'verify_password'   => 'Konfirmasi Sandi',
        );

        $validator = Validator::make(Input::all(), $rules, array(), $attributes);

        if($validator->fails()){
            return Redirect::back()->withErrors($validator);
        }else{
            //check last password is true
            $userName = Auth::user()->user_name;

            $userdata = array(
                'user_name' => $userName,
                'password' => Input::get('last_password')
            );

            if(Auth::validate($userdata)){
                $user = WUser::find(Auth::user()->id_user);
                $user->passnew = Hash::make(Input::get('new_password'));
                $user->save();

                Auth::logout();
                return Redirect::to('/')->with('flashMessage', 'Ubah password berhasil. Silahkan login kembali menggunakan password baru');

            }else{
                return Redirect::back()->with('flashErrors', 'salah memasukkan password lama')->withInput();
            }
        }
    }else{
        $this->layout->content = View::make('employee::user.changePassword');
    }
}

 

2 Comments

    • masbenx masbenx

      hasuk konangan.. -_-

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: