pai-sklep/app/Http/Controllers/LoginController.php
2021-11-14 14:51:49 +01:00

55 lines
1.3 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;
class LoginController 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([
'email' => ['required', 'email', 'unique:users'],
'name' => ['required', 'unique:users'],
'password' => ['required']
]);
$creds["password"] = Hash::make($creds["password"]);
User::create($creds);
return redirect("login")->withSuccess("Success! Now login");
}
public function logout(Request $request)
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerate();
return back();
}
public function view()
{
return view("login", []);
}
}