master_template/app/Http/Controllers/Auth/AuthenticationController.php
2024-06-13 21:02:05 +05:45

89 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthenticationController extends Controller
{
private $path;
public function __construct()
{
$this->path = config('app.client_path');
}
public function login()
{
return view($this->path.'.auth.login');
}
public function register()
{
return view($this->path.'.auth.registration');
}
public static function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
'username' => $request->name
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return response()->json([
'success' => true,
'message' => 'You have successfully logged in!',
'redirect_url' => route('home')
]);
}
return response()->json([
'success' => false,
'message' => 'The provided credentials do not match our records.'
]);
}
public static function authenticate(Request $request){
$credentials = $request->validate([
'email' => ['required', 'string', 'email', 'max:255'],
'password' => ['required', 'string', 'min:8'],
]);
$remember = $request->has('remember_me') ? true : false;
if (Auth::attempt($credentials, $remember)) {
$request->session()->regenerate();
return response()->json([
'success' => true,
'message' => 'You have successfully logged in!',
'redirect_url' => route('home')
]);
}
return response()->json([
'success' => false,
'message' => 'The provided credentials do not match our records.'
]);
}
public static function logout(Request $request){
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->route('userLogin');
}
}