Skip to content

Commit

Permalink
User Registration: complete, Login: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Slymee committed Jan 11, 2024
2 parents 54c98a7 + 806393b commit 975d257
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 70 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
27 changes: 21 additions & 6 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Controllers;

use App\Http\Requests\LoginFormValidator;
use App\Http\Requests\LoginRequest;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
Expand All @@ -15,13 +15,23 @@ public function index(){
}

//login module
public function login(LoginFormValidator $request){
/**
* Rename LoginFormValidator to LoginRequest
*
*/
public function login(LoginRequest $request){
try{
if(auth()->guard('admin')->attempt($request->only(['username', 'password']))):
return redirect()->intended('/admin/dashboard');
else:
return redirect()->back()->with('message', 'Invalid Credentials');
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());
}
Expand All @@ -31,8 +41,13 @@ public function login(LoginFormValidator $request){
//logout module
public function logout(Request $request){
Auth::guard('admin')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();

/**
* remove Unnecesary codes
*
* $request->session()->invalidate();
* $request->session()->regenerateToken();
*/
return redirect('/admin-login');
}
}
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()
]);
}

/**
* 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
*
*
*/
$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
23 changes: 11 additions & 12 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 @@ -28,7 +27,7 @@ public function index()


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

Expand All @@ -39,7 +38,7 @@ public function sendResetMail(ForgotPasswordMailValidator $request)
"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');
});
Expand All @@ -48,17 +47,17 @@ public function sendResetMail(ForgotPasswordMailValidator $request)


}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();

Expand All @@ -67,15 +66,15 @@ public function submitResetPasswordForm(ResetPasswordValidator $request){
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();
}
}

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

namespace App\Http\Controllers;

use App\Http\Requests\LoginRequest;
use App\Http\Requests\RegisterUserRequest;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
public function userLoginForm(){
return view('userend.login');
}


//User Registration
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
public function loginUser(LoginRequest $request){
// dd($request->validated());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Http\FormRequest;

class CategoryFormValidator extends FormRequest
class CategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Http\FormRequest;

class ForgotPasswordMailValidator extends FormRequest
class ForgotPasswordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Http\FormRequest;

class LoginFormValidator extends FormRequest
class LoginRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
Expand Down
32 changes: 32 additions & 0 deletions app/Http/Requests/RegisterUserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RegisterUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => ['bail','required'],
'username' => ['required', 'unique:users'],
'email' => ['required', 'email', 'unique:users'],
'password' => ['required'],
'confirm_password' => ['required', 'same:password'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Http\FormRequest;

class ResetPasswordValidator extends FormRequest
class ResetPasswordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
Expand Down
Binary file added public/images/holding-phone.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 975d257

Please sign in to comment.