118 lines
2.9 KiB
PHP
Executable File
118 lines
2.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Webkul\User\Http\Controllers;
|
|
|
|
use Illuminate\Foundation\Auth\ResetsPasswords;
|
|
use Illuminate\Support\Facades\Password;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Auth\Events\PasswordReset;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Admin reset password controller
|
|
*
|
|
* @author Jitendra Singh <jitendra@webkul.com>
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
*/
|
|
class ResetPasswordController extends Controller
|
|
{
|
|
use ResetsPasswords;
|
|
|
|
/**
|
|
* Contains route related configuration
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $_config;
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->_config = request('_config');
|
|
}
|
|
|
|
/**
|
|
* Display the password reset view for the given token.
|
|
*
|
|
* If no token is present, display the link request form.
|
|
*
|
|
* @param string|null $token
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function create($token = null)
|
|
{
|
|
return view($this->_config['view'])->with(
|
|
['token' => $token, 'email' => request('email')]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store()
|
|
{
|
|
try {
|
|
$this->validate(request(), [
|
|
'token' => 'required',
|
|
'email' => 'required|email',
|
|
'password' => 'required|confirmed|min:6',
|
|
]);
|
|
|
|
$response = $this->broker()->reset(
|
|
request(['email', 'password', 'password_confirmation', 'token']), function ($admin, $password) {
|
|
$this->resetPassword($admin, $password);
|
|
}
|
|
);
|
|
|
|
if ($response == Password::PASSWORD_RESET) {
|
|
return redirect()->route($this->_config['redirect']);
|
|
}
|
|
|
|
return back()
|
|
->withInput(request(['email']))
|
|
->withErrors([
|
|
'email' => trans($response)
|
|
]);
|
|
} catch(\Exception $e) {
|
|
session()->flash('error', trans($e->getMessage()));
|
|
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reset the given admin's password.
|
|
*
|
|
* @param \Illuminate\Contracts\Auth\CanResetPassword $admin
|
|
* @param string $password
|
|
* @return void
|
|
*/
|
|
protected function resetPassword($admin, $password)
|
|
{
|
|
$admin->password = Hash::make($password);
|
|
|
|
$admin->setRememberToken(Str::random(60));
|
|
|
|
$admin->save();
|
|
|
|
event(new PasswordReset($admin));
|
|
|
|
auth()->guard('admin')->login($admin);
|
|
}
|
|
|
|
/**
|
|
* Get the broker to be used during password reset.
|
|
*
|
|
* @return \Illuminate\Contracts\Auth\PasswordBroker
|
|
*/
|
|
public function broker()
|
|
{
|
|
return Password::broker('admins');
|
|
}
|
|
} |