master_template/app/Http/Controllers/Auth/AuthenticationController.php
2024-06-23 17:02:56 +05:45

97 lines
3.1 KiB
PHP

<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\Advertisements;
use App\Models\Menuitems;
use App\Models\User;
use App\Models\News;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
class AuthenticationController extends Controller
{
private $path;
public function __construct()
{
$this->path = config('app.client_path');
$ads = Advertisements::where('status', 1)->where('parent_advertisement', 0)->get();
$headerMenuItems = Menuitems::where(['parent_menu' => 0, "status" => 1, "menulocations_id" => 1])->with('children')->orderBy('display_order')->get();
$footerMenuItems = Menuitems::where(['parent_menu' => 0, "status" => 1, "menulocations_id" => 2])->with('children')->orderBy('display_order')->get();
$latestNews = News::where('status', 1)->inRandomOrder()->limit(4)->get();
View::share([
'ads' => $ads,
'headerMenuItems' => $headerMenuItems,
'footerMenuItems' => $footerMenuItems,
'latestNews' => $latestNews
]);
}
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'],
'phone' => ['required', 'integer'],
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
'username' => $request->name,
'phone' => $request->phone,
'status' => 1,
'email_verified_at' => now()
])->assignRole('user');
dd($user);
return response()->json(['success' => true, 'message' => 'User created successfully!', 'redirect_url' => route('userLogin')]);
}
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');
}
}