pai-sklep/app/Http/Controllers/UserController.php

92 lines
2.4 KiB
PHP
Raw Normal View History

2021-09-05 02:22:17 +02:00
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
2021-12-16 21:58:10 +01:00
use Illuminate\Validation\Rules\Password;
2021-09-05 02:22:17 +02:00
2021-12-16 21:58:10 +01:00
class UserController extends Controller
2021-09-05 02:22:17 +02:00
{
2021-11-14 14:51:49 +01:00
public function authenticate(Request $request)
{
2021-09-05 02:22:17 +02:00
$creds = $request->validate([
'email' => ['required', 'email'],
2021-12-16 21:58:10 +01:00
'password' => ['required'],
2021-09-05 02:22:17 +02:00
]);
2021-11-14 14:51:49 +01:00
if (Auth::attempt($creds)) {
2021-09-05 02:22:17 +02:00
$request->session()->regenerate();
return redirect()->intended();
}
return back()->withErrors([
'The provided credentials do not match our records.'
]);
}
2021-11-14 14:51:49 +01:00
public function register(Request $request)
{
2021-09-05 02:22:17 +02:00
Auth::logout();
$creds = $request->validate([
'name' => ['required', 'unique:users'],
2021-12-16 21:58:10 +01:00
'email' => ['required', 'email', 'unique:users'],
'password' => ['confirmed', Password::min(9)->uncompromised()],
2021-09-05 02:22:17 +02:00
]);
$creds["password"] = Hash::make($creds["password"]);
User::create($creds);
return redirect("login")->withSuccess("Success! Now login");
}
2021-12-16 21:58:10 +01:00
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();
}
2021-11-14 14:51:49 +01:00
public function logout(Request $request)
{
2021-09-05 02:22:17 +02:00
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerate();
return back();
}
2021-11-14 14:51:49 +01:00
public function view()
{
2021-09-05 02:22:17 +02:00
return view("login", []);
}
}