1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- namespace App\Http\Controllers\Auth;
- use App\Http\Controllers\Controller;
- use Illuminate\Auth\Events\PasswordReset;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Password;
- use Illuminate\Support\Str;
- use Illuminate\Validation\Rules;
- use Illuminate\Validation\ValidationException;
- class NewPasswordController extends Controller
- {
- /**
- * Handle an incoming new password request.
- *
- * @throws \Illuminate\Validation\ValidationException
- */
- public function store(Request $request): JsonResponse
- {
- $request->validate([
- 'token' => ['required'],
- 'email' => ['required', 'email'],
- 'password' => ['required', 'confirmed', Rules\Password::defaults()],
- ]);
- // 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('email', 'password', 'password_confirmation', 'token'),
- function ($user) use ($request) {
- $user->forceFill([
- 'password' => Hash::make($request->string('password')),
- 'remember_token' => Str::random(60),
- ])->save();
- event(new PasswordReset($user));
- }
- );
- if ($status != Password::PASSWORD_RESET) {
- throw ValidationException::withMessages([
- 'email' => [__($status)],
- ]);
- }
- return response()->json(['status' => __($status)]);
- }
- }
|