master_template/app/Http/Controllers/Auth/AuthenticationController.php

103 lines
3.3 KiB
PHP
Raw Normal View History

2024-06-13 15:17:05 +00:00
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
2024-06-20 10:57:33 +00:00
use App\Models\Advertisements;
use App\Models\Menuitems;
2024-06-13 15:17:05 +00:00
use App\Models\User;
2024-06-20 10:57:33 +00:00
use App\Models\News;
2024-06-13 15:17:05 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
2024-06-20 10:57:33 +00:00
use Illuminate\Support\Facades\View;
2024-06-13 15:17:05 +00:00
class AuthenticationController extends Controller
{
private $path;
public function __construct()
{
$this->path = config('app.client_path');
2024-06-20 10:57:33 +00:00
$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
]);
2024-06-13 15:17:05 +00:00
}
public function login()
{
return view($this->path.'.auth.login');
}
public function register()
{
return view($this->path.'.auth.registration');
}
public static function store(Request $request)
{
2024-06-20 10:57:33 +00:00
dd($request->all());
2024-06-13 15:17:05 +00:00
$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');
}
}