User account updating
This commit is contained in:
parent
39feadab32
commit
5349f0578e
4 changed files with 69 additions and 13 deletions
|
@ -6,14 +6,15 @@ use App\Models\User;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Validation\Rules\Password;
|
||||||
|
|
||||||
class LoginController extends Controller
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
public function authenticate(Request $request)
|
public function authenticate(Request $request)
|
||||||
{
|
{
|
||||||
$creds = $request->validate([
|
$creds = $request->validate([
|
||||||
'email' => ['required', 'email'],
|
'email' => ['required', 'email'],
|
||||||
'password' => ['required']
|
'password' => ['required'],
|
||||||
]);
|
]);
|
||||||
if (Auth::attempt($creds)) {
|
if (Auth::attempt($creds)) {
|
||||||
$request->session()->regenerate();
|
$request->session()->regenerate();
|
||||||
|
@ -29,9 +30,9 @@ class LoginController extends Controller
|
||||||
Auth::logout();
|
Auth::logout();
|
||||||
|
|
||||||
$creds = $request->validate([
|
$creds = $request->validate([
|
||||||
'email' => ['required', 'email', 'unique:users'],
|
|
||||||
'name' => ['required', 'unique:users'],
|
'name' => ['required', 'unique:users'],
|
||||||
'password' => ['required']
|
'email' => ['required', 'email', 'unique:users'],
|
||||||
|
'password' => ['confirmed', Password::min(9)->uncompromised()],
|
||||||
]);
|
]);
|
||||||
$creds["password"] = Hash::make($creds["password"]);
|
$creds["password"] = Hash::make($creds["password"]);
|
||||||
User::create($creds);
|
User::create($creds);
|
||||||
|
@ -39,6 +40,41 @@ class LoginController extends Controller
|
||||||
return redirect("login")->withSuccess("Success! Now login");
|
return redirect("login")->withSuccess("Success! Now login");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function update(Request $request) {
|
||||||
|
$user = Auth::user();
|
||||||
|
|
||||||
|
$creds = $request->validate([
|
||||||
|
'name' => ['string'],
|
||||||
|
'email' => ['email', 'string'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
if($creds["name"]) $user->name = $creds["name"];
|
||||||
|
if($creds["email"]) $user->email = $creds["email"];
|
||||||
|
$user->save();
|
||||||
|
return back();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updatePassword(Request $request) {
|
||||||
|
$user = Auth::user();
|
||||||
|
|
||||||
|
$creds = $request->validate([
|
||||||
|
'old_password' => ['required'],
|
||||||
|
'password' => ['required', 'confirmed', Password::min(9)->uncompromised()],
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (Hash::check($creds["old_password"], $user->password)) {
|
||||||
|
$creds["password"] = Hash::make($creds["password"]);
|
||||||
|
$user->password = $creds["password"];
|
||||||
|
} else {
|
||||||
|
return back()->withErrors([
|
||||||
|
'Incorrect password'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->save();
|
||||||
|
return back();
|
||||||
|
}
|
||||||
|
|
||||||
public function logout(Request $request)
|
public function logout(Request $request)
|
||||||
{
|
{
|
||||||
Auth::logout();
|
Auth::logout();
|
|
@ -25,7 +25,7 @@
|
||||||
<input type="email" name="email" placeholder="email">
|
<input type="email" name="email" placeholder="email">
|
||||||
<input type="text" name="name" placeholder="Imie">
|
<input type="text" name="name" placeholder="Imie">
|
||||||
<input type="password" name="password" placeholder="Hasło">
|
<input type="password" name="password" placeholder="Hasło">
|
||||||
<input type="password" name="repeat-password" placeholder="Powtórz hasło">
|
<input type="password" name="password_confirmation" placeholder="Powtórz hasło">
|
||||||
<input type="submit">
|
<input type="submit">
|
||||||
@csrf
|
@csrf
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -7,8 +7,26 @@
|
||||||
@section('title', "Sklep")
|
@section('title', "Sklep")
|
||||||
|
|
||||||
@section('main')
|
@section('main')
|
||||||
<p>
|
<div>
|
||||||
<b>Welcome, you are logged in as {{ $user->name }}</b>
|
<div>
|
||||||
<a href="{{route("logout")}}"> Logout </a>
|
<b>Welcome, you are logged in as {{ $user->name }}</b>
|
||||||
</p>
|
<a href="{{route("logout")}}"> Logout </a>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<form method="post" action="{{ route("user.update") }}">
|
||||||
|
<!-- TODO validation in js !-->
|
||||||
|
<input type="email" name="email" placeholder="email" value="{{ $user->email }}">
|
||||||
|
<input type="text" name="name" placeholder="Imie" value="{{ $user->name }}">
|
||||||
|
<input type="submit">
|
||||||
|
@csrf
|
||||||
|
</form>
|
||||||
|
<form action="{{ route("user.updatepassword") }}" method="post">
|
||||||
|
<input type="password" name="old_password" placeholder="Obecne hasło">
|
||||||
|
<input type="password" name="password" placeholder="Nowe hasło">
|
||||||
|
<input type="password" name="password_confirmation" placeholder="Powtórz nowe hasło">
|
||||||
|
<input type="submit">
|
||||||
|
@csrf
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@endsection()
|
@endsection()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Http\Controllers\ImageController;
|
use App\Http\Controllers\ImageController;
|
||||||
use App\Http\Controllers\LoginController;
|
use App\Http\Controllers\UserController;
|
||||||
use App\Http\Controllers\MainPageController;
|
use App\Http\Controllers\MainPageController;
|
||||||
use App\Http\Controllers\CartController;
|
use App\Http\Controllers\CartController;
|
||||||
use App\Http\Controllers\CategoryController;
|
use App\Http\Controllers\CategoryController;
|
||||||
|
@ -26,12 +26,14 @@ Route::get('/user', function () {
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
return view("user", ["user"=>$user]);
|
return view("user", ["user"=>$user]);
|
||||||
})->middleware("auth")->name("user");
|
})->middleware("auth")->name("user");
|
||||||
|
Route::post('/user', [UserController::class, "update"])->name("user.update");
|
||||||
|
Route::post('/user/password', [UserController::class, "updatePassword"])->name("user.updatepassword");
|
||||||
|
|
||||||
Route::view("/login", "login");
|
Route::view("/login", "login");
|
||||||
|
|
||||||
Route::post('/login', [LoginController::class, "authenticate"])->name("login");
|
Route::post('/login', [UserController::class, "authenticate"])->name("login");
|
||||||
Route::post('/register', [LoginController::class, "register"])->name("register");
|
Route::post('/register', [UserController::class, "register"])->name("register");
|
||||||
Route::any("/logout", [LoginController::class, "logout"])->name("logout");
|
Route::any("/logout", [UserController::class, "logout"])->name("logout");
|
||||||
|
|
||||||
Route::get("/cart", [CartController::class, "show"])->name("cart")->middleware("auth");
|
Route::get("/cart", [CartController::class, "show"])->name("cart")->middleware("auth");
|
||||||
Route::post("/product/{product}/addToCart", [CartController::class, "addToCart"])->name("addToCart")->middleware("auth");
|
Route::post("/product/{product}/addToCart", [CartController::class, "addToCart"])->name("addToCart")->middleware("auth");
|
||||||
|
|
Loading…
Reference in a new issue