pai-sklep/app/Http/Controllers/UserController.php
2021-12-16 21:58:10 +01:00

91 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
class UserController extends Controller
{
public function authenticate(Request $request)
{
$creds = $request->validate([
'email' => ['required', 'email'],
'password' => ['required'],
]);
if (Auth::attempt($creds)) {
$request->session()->regenerate();
return redirect()->intended();
}
return back()->withErrors([
'The provided credentials do not match our records.'
]);
}
public function register(Request $request)
{
Auth::logout();
$creds = $request->validate([
'name' => ['required', 'unique:users'],
'email' => ['required', 'email', 'unique:users'],
'password' => ['confirmed', Password::min(9)->uncompromised()],
]);
$creds["password"] = Hash::make($creds["password"]);
User::create($creds);
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)
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerate();
return back();
}
public function view()
{
return view("login", []);
}
}