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

56 lines
1.3 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;
class LoginController extends Controller
{
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'],
'password' => ['required']
]);
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([
'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");
}
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", []);
}
}