You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
5.5 KiB
PHTML
207 lines
5.5 KiB
PHTML
2 years ago
|
<?php
|
||
|
|
||
|
|
||
|
namespace App\Libs;
|
||
|
|
||
|
|
||
|
use Illuminate\Auth\Events\PasswordReset;
|
||
|
use Illuminate\Foundation\Auth\RedirectsUsers;
|
||
|
use Illuminate\Foundation\Auth\ResetsPasswords;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Illuminate\Support\Facades\Auth;
|
||
|
use Illuminate\Support\Facades\Hash;
|
||
|
use Illuminate\Support\Facades\Password;
|
||
|
use Illuminate\Support\Str;
|
||
|
use Illuminate\Support\Facades\DB;
|
||
|
use Carbon\Carbon;
|
||
|
|
||
|
trait UserResetsPasswords
|
||
|
{
|
||
|
use RedirectsUsers;
|
||
|
|
||
|
/**
|
||
|
* Where to redirect users after resetting their password.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
protected $redirectTo = '/password/reset-completed';
|
||
|
|
||
|
/**
|
||
|
* Display the password reset view for the given token.
|
||
|
*
|
||
|
* If no token is present, display the link request form.`
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @param string|null $token
|
||
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||
|
*/
|
||
|
public function showResetForm(Request $request, $token = null)
|
||
|
{
|
||
|
$expire = config('auth.passwords.users.expire');
|
||
|
$row = DB::table('password_resets')
|
||
|
->where('email', $request->email)
|
||
|
->where('created_at','>', Carbon::now()->subMinutes($expire))
|
||
|
->first();
|
||
|
|
||
|
return view('auth.passwords.reset')->with(
|
||
|
[
|
||
|
'token' => $token,
|
||
|
'email' => $request->email,
|
||
|
'expire' => $expire,
|
||
|
'row' => $row
|
||
|
]
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reset the given user's password.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||
|
*/
|
||
|
public function reset(Request $request)
|
||
|
{
|
||
|
$request->validate($this->rules($request), $this->validationErrorMessages());
|
||
|
|
||
|
// validate custom 수정
|
||
|
$this->broker()->validator(function (array $credentials)
|
||
|
{
|
||
|
[$password, $confirm] = [
|
||
|
$credentials['password'],
|
||
|
$credentials['password_confirmation'],
|
||
|
];
|
||
|
|
||
|
return $password === $confirm && mb_strlen($password) >= 6 && mb_strlen($password) <= 20;
|
||
|
});
|
||
|
|
||
|
$response = $this->broker()->reset(
|
||
|
$this->credentials($request), function ($user, $password) {
|
||
|
$this->resetPassword($user, $password);
|
||
|
}
|
||
|
);
|
||
|
|
||
|
// 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 $response == Password::PASSWORD_RESET
|
||
|
? $this->sendResetResponse($request, $response)
|
||
|
: $this->sendResetFailedResponse($request, $response);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the password reset validation rules.
|
||
|
*
|
||
|
* @return array
|
||
|
*/
|
||
|
protected function rules(Request $request)
|
||
|
{
|
||
|
$rules = [
|
||
|
'token' => 'required',
|
||
|
'email' => 'required|email',
|
||
|
'password' => 'required|confirmed|min:6|max:20',
|
||
|
];
|
||
|
|
||
|
return $rules;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the password reset validation error messages.
|
||
|
*
|
||
|
* @return array
|
||
|
*/
|
||
|
protected function validationErrorMessages()
|
||
|
{
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the password reset credentials from the request.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return array
|
||
|
*/
|
||
|
protected function credentials(Request $request)
|
||
|
{
|
||
|
$credentials = $request->only('email', 'password', 'password_confirmation', 'token');
|
||
|
|
||
|
return $credentials;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reset the given user's password.
|
||
|
*
|
||
|
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
|
||
|
* @param string $password
|
||
|
* @return void
|
||
|
*/
|
||
|
protected function resetPassword($user, $password)
|
||
|
{
|
||
|
$user->password = Hash::make($password);
|
||
|
|
||
|
$user->setRememberToken(Str::random(60));
|
||
|
|
||
|
$user->save();
|
||
|
|
||
|
event(new PasswordReset($user));
|
||
|
|
||
|
$this->guard()->login($user);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the response for a successful password reset.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @param string $response
|
||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||
|
*/
|
||
|
protected function sendResetResponse(Request $request, $response)
|
||
|
{
|
||
|
return redirect($this->redirectPath())
|
||
|
->with('status', trans($response));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the response for a failed password reset.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @param string $response
|
||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||
|
*/
|
||
|
protected function sendResetFailedResponse(Request $request, $response)
|
||
|
{
|
||
|
$input = $request->only('email');
|
||
|
|
||
|
$errors = ['email' => trans($response)];
|
||
|
|
||
|
if ($response == 'passwords.token') {
|
||
|
$errors['token'] = trans($response);
|
||
|
|
||
|
}
|
||
|
logger($errors);
|
||
|
|
||
|
return redirect()->back()
|
||
|
->withInput($input)
|
||
|
->withErrors($errors);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the broker to be used during password reset.
|
||
|
*
|
||
|
* @return \Illuminate\Contracts\Auth\PasswordBroker
|
||
|
*/
|
||
|
public function broker()
|
||
|
{
|
||
|
return Password::broker();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the guard to be used during password reset.
|
||
|
*
|
||
|
* @return \Illuminate\Contracts\Auth\StatefulGuard
|
||
|
*/
|
||
|
protected function guard()
|
||
|
{
|
||
|
return Auth::guard();
|
||
|
}
|
||
|
}
|