<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input as input;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\User;

class ChangePasswordController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
   
    /**
     * Display form to change password
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('auth.changePassword');
    }

    /**
     * Check new password and change it
     *
     * @return \Illuminate\Http\Response
     */
    public function changePassword()
    {
        $User = User::find(Auth::user()->id);
        if(Hash::check(Input::get('passwordold'), $User['password']) && Input::get('password') == Input::get('password_confirmation')){
            $User->password = bcrypt(Input::get('password'));
            $User->save();
            return back()->with('success', 'Password changed');
        } else{
            return back()->with('error', 'Password not changed');
        }
    }

}
