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

97 lines
3.1 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 12:22:40 +00:00
$ads = Advertisements::where('status', 1)->where('parent_advertisement', 0)->get();
2024-06-20 10:57:33 +00:00
$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()
{
2024-06-20 12:22:40 +00:00
return view($this->path . '.auth.login');
2024-06-13 15:17:05 +00:00
}
public function register()
{
2024-06-20 12:22:40 +00:00
return view($this->path . '.auth.registration');
2024-06-13 15:17:05 +00:00
}
public static function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
2024-06-20 12:22:40 +00:00
'password' => ['required', 'string', 'min:8'],
'phone' => ['required', 'integer'],
2024-06-13 15:17:05 +00:00
]);
2024-06-23 11:17:56 +00:00
$user = User::create([
2024-06-13 15:17:05 +00:00
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
2024-06-20 12:22:40 +00:00
'username' => $request->name,
'phone' => $request->phone,
'status' => 1,
'email_verified_at' => now()
])->assignRole('user');
2024-06-23 11:17:56 +00:00
dd($user);
2024-06-20 12:22:40 +00:00
return response()->json(['success' => true, 'message' => 'User created successfully!', 'redirect_url' => route('userLogin')]);
}
2024-06-13 15:17:05 +00:00
2024-06-20 12:22:40 +00:00
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)) {
2024-06-13 15:17:05 +00:00
$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.'
]);
}
2024-06-20 12:22:40 +00:00
public static function logout(Request $request)
{
2024-06-13 15:17:05 +00:00
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->route('userLogin');
}
}