Skip to content

Commit

Permalink
Web > User change password işlemi eklendi.
Browse files Browse the repository at this point in the history
- php artisan make:request Web/UserUpdatePasswordRequest
- php artisan make:component Web/Errors --view
  • Loading branch information
berkanumutlu committed Jul 17, 2024
1 parent 365abc3 commit 78440af
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 19 deletions.
35 changes: 30 additions & 5 deletions project/app/Http/Controllers/Web/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Http\Requests\Web\UserUpdatePasswordRequest;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserController extends Controller
Expand All @@ -24,7 +26,7 @@ public function profile()
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
public function update(Request $request, string $id): \Illuminate\Http\RedirectResponse
{
$user = User::findOrFail($id);
$user_image = $user->avatar;
Expand All @@ -51,10 +53,33 @@ public function update(Request $request, string $id)
}
}
} catch (\Exception $e) {
return redirect()->route('user.profile')->with('error',
'Your information could not be saved.')->exceptInput('_token', 'image');
return redirect()->route('user.profile')->with('error', 'Your information could not be saved.')
->exceptInput('_token', 'image');
}
return redirect()->route('user.profile')->with('success',
'Your information has been updated successfully.')->onlyInput();
return redirect()->route('user.profile')->with('success', 'Your information has been updated successfully.')
->onlyInput();
}

/**
* @param UserUpdatePasswordRequest $request
* @param User $user
* @return \Illuminate\Http\RedirectResponse
*/
public function update_password(UserUpdatePasswordRequest $request, User $user): \Illuminate\Http\RedirectResponse
{
if (!Hash::check($request->current_password, $user->password)) {
return redirect()->route('user.profile')->with('error_password', 'The current password is incorrect.')
->exceptInput('_token', 'image');
}
try {
$user->password = Hash::make($request->new_password);
$user->save();
Auth::guard('web')->logout();
} catch (\Exception $e) {
return redirect()->route('user.profile')->with('error_password', 'Your password could not be saved.')
->exceptInput('_token', 'image');
}
return redirect()->route('login.index')->with('success', 'Your password has been updated successfully.')
->onlyInput();
}
}
30 changes: 30 additions & 0 deletions project/app/Http/Requests/Web/UserUpdatePasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests\Web;

use Illuminate\Foundation\Http\FormRequest;

class UserUpdatePasswordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'id' => ['exists:users,id'],
'current_password' => ['required', 'string'],
'new_password' => ['required', 'string', 'different:current_password', 'confirmed', \Illuminate\Validation\Rules\Password::min(8)->letters()->numbers()->symbols()->mixedCase()],
];
}
}
9 changes: 9 additions & 0 deletions project/resources/views/components/web/errors.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@if($errors->any())
<div class="alert alert-danger {{ $alertClass ?? '' }}">
<ul class="list-group list-unstyled {{ $ulClass ?? '' }}">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
40 changes: 26 additions & 14 deletions project/resources/views/web/user/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
<div class="container container-page">
<h1>Update Profile</h1>
<hr>
@if(session('success'))
<div class="w-100">
<div class="alert alert-success">{{ session('success') }}</div>
</div>
@endif
@if(session('error'))
<div class="w-100">
<div class="alert alert-danger">{{ session('error') }}</div>
</div>
@endif
<div class="my-2">
<form action="{{ route('user.profile.edit', ['user' => $user->id]) }}" method="POST">
<form action="{{ route('user.profile.edit', ['user' => $user]) }}" method="POST">
@csrf
@if(session('success'))
<div class="w-100">
<div class="alert alert-success">{{ session('success') }}</div>
</div>
@endif
@if(session('error'))
<div class="w-100">
<div class="alert alert-danger">{{ session('error') }}</div>
</div>
@endif
<div class="row">
<div class="col-md-6">
<div class="form-floating mb-3">
Expand Down Expand Up @@ -61,8 +61,20 @@
</div>
<div class="my-5">
<hr>
<h2>Change Password</h2>
<form>
<h2 class="mb-4">Change Password</h2>
<x-web.errors :errors="$errors"></x-web.errors>
@if(session('success_password'))
<div class="w-100">
<div class="alert alert-success">{{ session('success_password') }}</div>
</div>
@endif
@if(session('error_password'))
<div class="w-100">
<div class="alert alert-danger">{{ session('error_password') }}</div>
</div>
@endif
<form action="{{ route('user.password.edit', ['user' => $user]) }}" method="POST">
@csrf
<div class="row">
<div class="col-md-4">
<div class="form-floating mb-3">
Expand All @@ -87,7 +99,7 @@ class="form-control" placeholder="Confirm New Password" required>
</div>
<div class="col-12">
<div class="d-flex align-items-center justify-content-md-end">
<button type="button" class="btn btn-primary">Change Password</button>
<button type="submit" class="btn btn-primary">Change Password</button>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions project/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Route::name('user.')->controller('\App\Http\Controllers\Web\UserController')->middleware('auth:web')->group(function () {
Route::get('profile', "profile")->name('profile');
Route::post('profile/edit/{user:id}', "update")->name('profile.edit')->whereNumber('id');
Route::post('profile/change-password/{user:id}', "update_password")->name('password.edit')->whereNumber('id');
});
Route::name('article.')->controller('\App\Http\Controllers\Web\ArticleController')->group(function () {
Route::get('about-us', "show_article_page")->name('about_us');
Expand Down

0 comments on commit 78440af

Please sign in to comment.