<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use App\User;
use Mail;

class resetPasswordController extends Controller
{
	public function create()
   	{
   	    return view('forgottenPassword');
   	}

   	public function reset()
   	{

        $this->validate(request(), [
            'email' => 'required|email',
        ],
            [
                'required' => ':attribute jest wymagany.',
                'email' => 'Niepoprawny adres e-mail.'
            ]);

        $request = request();
        $user = DB::table('users')->where('email', '=', $request->email)
    ->first();

		if($user){
			$password = str_random(10);
			User::where('email', '=', $request->email)
          ->update(['password' => Hash::make($password)]);
    		Mail::send('emails.passwordReset', compact('password'), function($message) {
            $message->to(Input::get('email'))->subject('Zmiana hasła');
        	});
		}

		return redirect()->to('/login')->with('success','Jeśli podany adres email jest poprawny, nowe hasło zostało wysłane');

   	}

}