Skip to content

Commit

Permalink
Merge pull request #12 from A4family/10-user-login-and-registration
Browse files Browse the repository at this point in the history
User and admin Multi guard authentication
  • Loading branch information
aashish-t-magar authored Jan 16, 2024
2 parents bbc903b + 5fc1d73 commit a5f0df5
Show file tree
Hide file tree
Showing 36 changed files with 1,276 additions and 171 deletions.
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
35 changes: 35 additions & 0 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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
public function login(LoginRequest $request){
try{
if(auth()->guard('admin')->attempt($request->only(['username', 'password']))){
return redirect(route('admin.dashboard'));
}
return redirect()->back()->with('message', 'Invalid Credentials');
}catch(\Exception $e){
return redirect()->back()->with('message', $e->getMessage());
}
}


//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: 11 additions & 11 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,23 +23,24 @@ public function index(Category $category)
*/
public function create(Category $category)
{
return view('backend.modals.admin-add-category', ['datas' => $category->whereNull('parent_id')
$data = $category->whereNull('parent_id')
->orWhereHas('parent', fn ($query) => $query->whereNull('parent_id'))
->get()
]);
->paginate(10);
return view('backend.modals.admin-add-category', ['datas' => $data]);
}

/**
*
*
* Store a newly created resource in storage.
*/
public function store(CategoryFormValidator $request)
public function store(CategoryRequest $request)
{
try{
Category::create([
'category_name' => $request->category_name,
'parent_id' => $request->parent_id,
]);

return redirect()->back()->with('message', 'Category Inserted.');
}catch(\Exception $e){
return redirect()->back()->with('message', $e->getMessage());
Expand All @@ -61,11 +62,11 @@ public function edit(string $id)
{
try{
$editableData = Category::select('id', 'category_name','parent_id')->findOrFail($id);
$datas = Category::whereNull('parent_id')
$data = Category::whereNull('parent_id')
->orWhereHas('parent', fn ($query) => $query->whereNull('parent_id'))
->get();
->paginate(10);
return view('backend.modals.admin-edit-category', ['editableData' => $editableData],
['datas' => $datas]);
['datas' => $data]);
}catch(\Exception $e){

}
Expand All @@ -75,7 +76,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 All @@ -85,7 +86,6 @@ public function update(CategoryFormValidator $request)
return redirect()->back()->with('message', 'Edit Success!');


// return redirect()->back()->with('message', 'Edit Failed!');
}catch(\Exception $e){
return redirect()->back()->with('message', $e->getMessage());
}
Expand Down
43 changes: 13 additions & 30 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):
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();
}
return redirect()->back()->with('message', 'Invalid Credentials');

}catch(\Exception $e){
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

0 comments on commit a5f0df5

Please sign in to comment.