63 lines
2.1 KiB
PHP
63 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Auth\Events\PasswordReset;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Password;
|
|
use Illuminate\Support\Str;
|
|
|
|
class NewPasswordController extends Controller
|
|
{
|
|
/**
|
|
* Display the password reset view.
|
|
*
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
public function create(Request $request)
|
|
{
|
|
return view('auth.reset-password', ['request' => $request]);
|
|
}
|
|
|
|
/**
|
|
* Handle an incoming new password request.
|
|
*
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'token' => 'required',
|
|
'username' => 'required|string',
|
|
'password' => 'required|string|confirmed|min:8',
|
|
]);
|
|
|
|
// Here we will attempt to reset the user's password. If it is successful we
|
|
// will update the password on an actual user model and persist it to the
|
|
// database. Otherwise we will parse the error and return the response.
|
|
$status = Password::reset(
|
|
$request->only('username', 'password', 'password_confirmation', 'token'),
|
|
function ($user) use ($request) {
|
|
$user->forceFill([
|
|
'password' => Hash::make($request->password),
|
|
'remember_token' => Str::random(60),
|
|
])->save();
|
|
|
|
event(new PasswordReset($user));
|
|
}
|
|
);
|
|
|
|
// If the password was successfully reset, we will redirect the user back to
|
|
// the application's home authenticated view. If there is an error we can
|
|
// redirect them back to where they came from with their error message.
|
|
return $status == Password::PASSWORD_RESET
|
|
? redirect()->route('login')->with('status', tr($status))
|
|
: back()->withInput($request->only('username'))
|
|
->withErrors(['username' => tr($status)]);
|
|
}
|
|
}
|