Skip to content

Commit

Permalink
prepare password reset
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Dec 3, 2023
1 parent a907307 commit 5cf0394
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 4 deletions.
32 changes: 32 additions & 0 deletions resources/views/auth/forgot-password.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@extends('root::auth.layout')

{{-- Title --}}
@section('title', __('Forgot Password'))

{{-- Content --}}
@section('content')
<form method="POST" action="{{ URL::route('root.auth.password.email') }}">
@csrf
<div class="form-group-stack">
<div class="form-group">
<label class="form-label" for="email">{{ __('Email') }}</label>
<input
@class(['form-control', 'form-control--lg', 'form-control--invalid' => $errors->has('email')])
id="email"
type="email"
name="email"
required
value="{{ Request::old('email') }}"
>
@error('email')
<span class="field-feedback field-feedback--invalid">{{ $message }}</span>
@enderror
</div>
<div class="form-group">
<button class="btn btn--primary btn--lg btn--block btn--primary-shadow">
{{ __('Send Password Reset Email') }}
</button>
</div>
</div>
</form>
@endsection
5 changes: 3 additions & 2 deletions resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{{-- Content --}}
@section('content')
<p>Hey there, welcome back!</p>
<p>{{ __('Hey there, welcome back!') }}</p>
<form method="POST" action="{{ URL::route('root.auth.login') }}">
@csrf
<div class="form-group-stack">
Expand All @@ -25,7 +25,8 @@
</div>
<div class="form-group">
<label class="form-label form-label--space-between" for="password">
{{ __('Password') }} <a href="#">{{ __('Forgot your password?') }}</a>
{{ __('Password') }}
<a href="{{ URL::route('root.auth.password.request') }}">{{ __('Forgot your password?') }}</a>
</label>
<input
@class(['form-control', 'form-control--lg', 'form-control--invalid' => $errors->has('password')])
Expand Down
9 changes: 9 additions & 0 deletions routes/auth.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<?php

use Cone\Root\Http\Controllers\Auth\ForgotPasswordController;
use Cone\Root\Http\Controllers\Auth\LoginController;
use Cone\Root\Http\Controllers\Auth\ResetPasswordController;
use Illuminate\Support\Facades\Route;

// Login
Route::get('login', [LoginController::class, 'show'])->name('login');
Route::post('login', [LoginController::class, 'login']);
Route::post('logout', [LoginController::class, 'logout'])->name('logout');

// Reset
Route::get('/password/reset', [ForgotPasswordController::class, 'show'])->name('password.request');
Route::post('/password/email', [ForgotPasswordController::class, 'send'])->name('password.email');
Route::get('/password/reset/{token}/{email}', [ResetPasswordController::class, 'show'])->name('password.reset');
Route::post('/password/reset', [ResetPasswordController::class, 'reset'])->name('password.update');
14 changes: 12 additions & 2 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ public function toInput(Request $request, Model $model): array
}

/**
* Get the sub resource representation of the
* Get the sub resource representation of the relation
*/
public function toSubResource(Request $request, Model $model): array
{
Expand All @@ -645,7 +645,17 @@ public function toSubResource(Request $request, Model $model): array
}

/**
* Get the index representation of the
* Get the fragment representation of the relation.
*/
public function toFragment(Request $request, Model $model): array
{
return array_merge($this->toSubResource($request, $model), [
//
]);
}

/**
* Get the index representation of the relation.
*/
public function toIndex(Request $request, Model $model): array
{
Expand Down
34 changes: 34 additions & 0 deletions src/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Cone\Root\Http\Controllers\Auth;

use Cone\Root\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response as ResponseFactory;

class ForgotPasswordController extends Controller
{
/**
* Display the form to request a password reset link.
*/
public function show(): Response
{
return ResponseFactory::view('root::auth.forgot-password');
}

/**
* Send a reset link to the given user.
*/
public function send(Request $request): RedirectResponse
{
$data = $request->validate(['email' => ['required', 'string', 'email']]);

Password::broker()->sendResetLink($data);

return Redirect::back()->with('status', __(Password::RESET_LINK_SENT));
}
}
69 changes: 69 additions & 0 deletions src/Http/Controllers/Auth/ResetPasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Cone\Root\Http\Controllers\Auth;

use App\Http\Requests\ResetPasswordRequest;
use App\Models\User;
use Cone\Root\Http\Controllers\Controller;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response as ResponseFactory;
use Illuminate\Support\Str;

class ResetPasswordController extends Controller
{
/**
* Display the password reset view for the given token.
*/
public function show(Request $request): Response
{
return ResponseFactory::view('root::auth.reset-password', [
'email' => $request->route()->parameter('email'),
'token' => $request->route()->parameter('token'),
]);
}

/**
* Reset the given user's password.
*/
public function reset(ResetPasswordRequest $request): RedirectResponse
{
$response = Password::broker()->reset(
$request->only(['email', 'password', 'password_confirmation', 'token']),
function (User $user, string $password): void {
$this->resetPassword($user, $password);

if (! $user->hasVerifiedEmail()) {
$user->markEmailAsVerified();
}
}
);

return $response == Password::PASSWORD_RESET
? Redirect::route('root.dashboard')->with('message', __($response))
: Redirect::back()->withInput($request->only(['email']))->withErrors(['email' => __($response)]);
}

/**
* Reset the given user's password.
*/
protected function resetPassword(User $user, string $password): void
{
$user->setAttribute('password', Hash::make($password));

$user->setRememberToken(Str::random(60));

$user->save();

Event::dispatch(new PasswordReset($user));

Auth::guard()->login($user);
}
}

0 comments on commit 5cf0394

Please sign in to comment.