Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User and admin Multi guard authentication #12

Merged
merged 10 commits into from
Jan 16, 2024
12 changes: 6 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=intern_project
DB_USERNAME=postgres
DB_PASSWORD=password

BROADCAST_DRIVER=log
CACHE_DRIVER=file
Expand Down
44 changes: 44 additions & 0 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\LoginRequest;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
//display login form
public function index(){
return view('backend.admin-login');
}

//login module
/**
* Rename LoginFormValidator to LoginRequest
*
*/
public function login(LoginRequest $request){
try{
if(auth()->guard('admin')->attempt($request->only(['username', 'password']))):
return redirect(route('admin.dashboard'));
endif;
return redirect()->back()->with('message', 'Invalid Credentials');
/**
*
* if there is already return function then no need to write else part.
*
*/
}catch(\Exception $e){
return redirect()->back()->with('message', $e->getMessage());
}
}
Slymee marked this conversation as resolved.
Show resolved Hide resolved


//logout module
public function logout(Request $request){
Auth::guard('admin')->logout();
return redirect('/admin-login');
}
}
92 changes: 0 additions & 92 deletions app/Http/Controllers/AdminDataDetailsController.php

This file was deleted.

22 changes: 19 additions & 3 deletions app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Controllers;


use App\Http\Requests\CategoryFormValidator;
use App\Http\Requests\CategoryRequest;
use App\Models\Category;
use Illuminate\Http\Request;

Expand All @@ -23,16 +23,25 @@ public function index(Category $category)
*/
public function create(Category $category)
{

/**
* Alwyas try to paginate
*
* Write query in avariable instead of directly passing
*/
return view('backend.modals.admin-add-category', ['datas' => $category->whereNull('parent_id')
->orWhereHas('parent', fn ($query) => $query->whereNull('parent_id'))
->get()
]);
}

Slymee marked this conversation as resolved.
Show resolved Hide resolved
/**
* Rename CategoryFormValidator to CategoryFormRequest
*
*
* Store a newly created resource in storage.
*/
public function store(CategoryFormValidator $request)
public function store(CategoryRequest $request)
{
try{
Category::create([
Expand Down Expand Up @@ -60,6 +69,13 @@ public function show(Category $category)
public function edit(string $id)
{
try{

/**
* Always try to paginate instead of get();
* rename datas to data
*
*
*/
Slymee marked this conversation as resolved.
Show resolved Hide resolved
$editableData = Category::select('id', 'category_name','parent_id')->findOrFail($id);
$datas = Category::whereNull('parent_id')
->orWhereHas('parent', fn ($query) => $query->whereNull('parent_id'))
Expand All @@ -75,7 +91,7 @@ public function edit(string $id)
/**
* Update the specified resource in storage.
*/
public function update(CategoryFormValidator $request)
public function update(CategoryRequest $request)
{
try{
Category::where('id', $request->category_id)->update([
Expand Down
39 changes: 11 additions & 28 deletions app/Http/Controllers/PasswordResetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace App\Http\Controllers;

use App\Http\Requests\ForgotPasswordMailValidator;
use App\Http\Requests\ResetPasswordValidator;
use App\Models\User;
use App\Http\Requests\ForgotPasswordRequest;
use App\Http\Requests\ResetPasswordRequest;
use App\Models\Admin;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
Expand All @@ -22,65 +21,49 @@ class PasswordResetController extends Controller
*/
public function index()
{
//
return view('backend.forgot-password');
}


//send reset mail to the user
public function sendResetMail(ForgotPasswordMailValidator $request)
public function sendResetMail(ForgotPasswordRequest $request)
{
$token = Str::random(64);

try{
DB::table('password_reset_tokens')->insert([
'email' => $request->validated()['email'],
'token' => $token,
"created_at" => Carbon::now(),
]);

Mail::send('backend.resetPasswordLink', ['token' => $token], function($message) use($request){
Mail::send('backend.reset-password-link', ['token' => $token], function($message) use($request){
$message->to($request->validated()['email']);
$message->subject('Reset Password');
});

return back()->with('message', 'Your password reset link has been sent to your email.');


}catch(\Exception $e){

return back()->with('message', $e->getMessage());
}
}


public function showNewPasswordForm(string $token){
return view('backend.passwordReset', ['token'=> $token]);
return view('backend.password-reset', ['token'=> $token]);
}


public function submitResetPasswordForm(ResetPasswordValidator $request){
public function submitAdminNewPassword(ResetPasswordRequest $request){
try{
$tokenData = DB::table('password_reset_tokens')->where('token', $request->validated()['token'])->first();

if(!$tokenData):
return back()->with(['message' => 'Invalid token id!!']);
endif;


User::where('email', $tokenData->email)->first()->update([
Admin::where('email', $tokenData->email)->first()->update([
'password' => Hash::make($request->validated()['new-password']),
]);

DB::table('password_reset_tokens')->where('email', $tokenData->email)->delete();

return redirect()->route('login')->with('message', 'Password successfully updated!!');
return redirect()->route('admin.login')->with('message', 'Password successfully updated!!');
}catch(\Exception $e){
// dd($e);
return $e->getMessage();
}
}





}
59 changes: 59 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\LoginRequest;
use App\Http\Requests\RegisterUserRequest;
use App\Models\User;
use Illuminate\Contracts\Session\Session;
use Illuminate\Support\Facades\Session as LaravelSession;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

class UserController extends Controller
{
public function userLoginForm(){
Redirect::setIntendedUrl(url()->previous());
return view('userend.login');
}


//User Registration module
public function registerUser(RegisterUserRequest $request){
try{
User::create([
'name' => $request->name,
'username' => $request->username,
'email' => $request->email,
'password' => $request->password,
]);
return redirect()->back()->with('message', 'User Registered.');
}catch(\Exception $e){
return redirect()->back()->with('message', $e->getMessage());
}
}

//User Login module
public function loginUser(LoginRequest $request){
try{
if(Auth::guard('web')->attempt(['username' => $request->username, 'password' => $request->password])):
return redirect()->intended();
endif;
return redirect()->back()->with('message', 'Invalid Credentials');

}catch(\Exception $e){
Slymee marked this conversation as resolved.
Show resolved Hide resolved
return redirect()->back()->with('message', $e->getMessage());
}
}

//User logout module
public function logoutUser(){
Auth::guard('web')->logout();
return redirect()->back();
}

public function index(){
return view('userend.index');
}
}
Loading
Loading