Skip to content

Instantly share code, notes, and snippets.

@naumanahmed19
Created November 20, 2016 16:20
Show Gist options
  • Save naumanahmed19/fb047fd60bad1dfd7ed3290865a09830 to your computer and use it in GitHub Desktop.
Save naumanahmed19/fb047fd60bad1dfd7ed3290865a09830 to your computer and use it in GitHub Desktop.
Adding a change password functionality in Laravel
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Change Password</div>
<div class="panel-body">
{!! Form::model(Auth::user(),['route' => ['change.update',Auth::user()->id], 'method' => 'PATCH']) !!}
<div class="form-group{{ $errors->has('old_password') ? ' has-error' : '' }}">
{!! Form::label('old_password','Old Password') !!}
{!! Form::password('old_password', ['class'=>'form-control']) !!}
{!! $errors->first('old_password','<span class ="help-block">:message</span>') !!}
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
{!! Form::label('password','New Password') !!}
{!! Form::password('password', ['class'=>'form-control']) !!}
{!! $errors->first('password','<span class ="help-block">:message</span>') !!}
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
{!! Form::label('password_confirmation','Confirm Password') !!}
{!! Form::password('password_confirmation', ['class'=>'form-control']) !!}
{!! $errors->first('password_confirmation','<span class ="help-block">:message</span>') !!}
</div>
{!! Form::submit('Save') !!}
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
@endsection
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Hash;
class ChangePasswordController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('auth.passwords.changePassword');
}
public function update(Request $request)
{
$this->validate($request,[
'old_password' => 'required',
'password' => 'required|min:6|confirmed',
]);
$input = Input::except(['_method', '_token']);
$current_password = Auth::user()->password;
if(Hash::check($request['old_password'], $current_password))
{
if ($input['password'] != "") {
$input['password'] = bcrypt($input['password']);
}
Auth::user()->update($input);
}
return redirect()->back();
}
}
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index');
Route::resource('/password/change', 'Auth\ChangePasswordController',
['only' => ['index','update']]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment