Skip to content

Commit

Permalink
Feat : Profile and Recaptcha V3
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen Tan Dung committed Dec 12, 2023
1 parent c42480e commit cc3eeba
Show file tree
Hide file tree
Showing 27 changed files with 723 additions and 74 deletions.
4 changes: 1 addition & 3 deletions app/Http/Controllers/Admin/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\SettingRequest;
use App\Http\Requests\admin\SettingRequest;
use App\Http\Services\Admin\SettingService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class SettingController extends Controller
Expand Down
39 changes: 39 additions & 0 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\admin\ProfileRequest;
use App\Http\Services\Admin\ProfileService;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ProfileController extends Controller
{
protected ProfileService $profileService;

public function __construct()
{
$this->profileService = new ProfileService();
}

public function index(): View
{
$user = auth()->user();
return view("admin.profile", compact("user"));
}

public function save(ProfileRequest $request)
{
$data = $request->validated();

$result = $this->profileService->save($data);

if ($result)
return redirect()
->route("admin.index")
->with("success", trans("Update Successfully"));
$errors = session("errors");
$errors[] = "Update Failed";
return redirect()->back()->withErrors($errors);
}
}
2 changes: 1 addition & 1 deletion app/Http/Middleware/CheckAdminMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CheckAdminMiddleware
*/
public function handle(Request $request, Closure $next): Response
{
if (Auth::user()->level !== 4)
if (Auth::guest() || Auth::user()->level !== 4)
return redirect()->route('admin.login')->withErrors('You must login account admin to continue');

return $next($request);
Expand Down
32 changes: 32 additions & 0 deletions app/Http/Repositories/Admin/ProfileRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Repositories\Admin;


use App\Models\User;
use function Laravel\Prompts\password;

class ProfileRepository
{
protected User $user;

public function __construct()
{
$this->user = new User();
}

public function save(array $data): bool
{
try {
$user = $this->user::query()
->updateOrCreate([
"email" => auth()->user()->email,
], $data);

$user->save();
return true;
} catch (\Exception $e) {
return false;
}
}
}
68 changes: 68 additions & 0 deletions app/Http/Requests/admin/ProfileRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Http\Requests\admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Lunaweb\RecaptchaV3\Facades\RecaptchaV3;

class ProfileRequest extends FormRequest
{
public function validationData(): array
{
$original = parent::ValidationData();

if (isset($original["g-recaptcha-response"])){
$original["score"] = RecaptchaV3::verify($original["g-recaptcha-response"], 'register');
}

return $original;
}

/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Auth::check();
}

/**
* 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" => ["required", "string", "min:5"],
"phone" => ["required", "string", "min:5", "max:15"],
"id_card" => ["required", "string", "min:5", "max:32"],
"birthday" => ["nullable", "date",],
"gender" => ["required", "integer", "min:0", "max:1"],
"old_password" => ["nullable", "string", "min:3", "max:32"],
"new_password" => ["nullable", "string", "min:3", "max:32", "different:old_password"],
"g-recaptcha-response" => ["required"],
"score" => ["required", "numeric", "min:0.5", "max:0.99"],
];
}

public function messages(): array
{
return [
"string" => trans("Unknown error please try again"),
"min:5" => trans("Fields must be more than 5 characters"),
"phone.max:15" => trans("Fields must be less than 15 characters"),
"max:32" => trans("Fields must be less than 32 characters"),
"date" => trans("Fields must be a date"),
"integer" => trans("Unknown error please try again"),
"gender.min:0" => trans("Unknown error please try again"),
"gender.max:1" => trans("Unknown error please try again"),
"new_password.different" => trans("New password must be different from old password"),
"score.required" => "Looks like you're spamming",
"score.numeric" => "Looks like you are spamming",
"score.min:0.5" => "Looks like you are spamming",
"score.max:0.99" => "Looks like you are spamming",
];
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Requests;
namespace App\Http\Requests\admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -44,7 +44,7 @@ public function messages(): array
"email" => trans("Unknown error please try again"),
"string" => trans("Unknown error please try again"),
"min:4" => trans("Fields must be more than 4 characters"),
"phone.max:15" => trans("Fields must be less than 32 characters"),
"phone.max:15" => trans("Fields must be less than 15 characters"),
"numeric" => trans("Unknown error please try again"),
];
}
Expand Down
38 changes: 38 additions & 0 deletions app/Http/Services/Admin/ProfileService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Services\Admin;

use App\Http\Repositories\Admin\ProfileRepository;
use Illuminate\Support\Facades\Hash;

class ProfileService {
protected ProfileRepository $profileRepository;

public function __construct()
{
$this->profileRepository = new ProfileRepository();
}

public function save(array $data): bool
{
try {
if (isset($data['old_password']) && isset($data['new_password']))
{
if(! Hash::check($data['old_password'], auth()->user()->password))
{
session()->flash('errors', ['Old password is incorrect']);

return false;
}

$data["password"] = Hash::make($data['new_password']);
}

$this->profileRepository->save($data);

return true;
} catch (\Exception $e) {
return false;
}
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"bensampo/laravel-enum": "^6.7",
"diglactic/laravel-breadcrumbs": "^8.1",
"guzzlehttp/guzzle": "^7.2",
"josiasmontag/laravel-recaptchav3": "^1.0",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.3",
"laravel/tinker": "^2.8"
Expand Down
68 changes: 67 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions config/recaptchav3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
return [
'origin' => env('RECAPTCHAV3_ORIGIN', 'https://www.google.com/recaptcha'),
'sitekey' => env('RECAPTCHAV3_SITEKEY', ''),
'secret' => env('RECAPTCHAV3_SECRET', ''),
'locale' => env('RECAPTCHAV3_LOCALE', '')
];
33 changes: 24 additions & 9 deletions resources/js/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
LogOut, User, Dot, Moon, Sun, ChevronUp, MapPin, Newspaper, LayoutGrid,
Settings2, ListOrdered, ChevronDown, Users, Hotel, ShoppingBag, Wallet,
UserCog, Headphones, ChevronRight, HardDriveDownload, HardDriveUpload,
CircleUser, Home
CircleUser, Home, Eye, EyeOff
} from "lucide";

createIcons({
Expand Down Expand Up @@ -38,6 +38,8 @@ createIcons({
HardDriveUpload,
CircleUser,
Home,
Eye,
EyeOff,
}
});
//endregion
Expand All @@ -55,21 +57,34 @@ window.ResizeObserver = ResizeObserver;
document.querySelector('[data-toggle="fullscreen"]')?.addEventListener("click", (e) => {
e.preventDefault();
document.body.classList.toggle("fullscreen-enable");
// @ts-ignore
if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement) {
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
} else { // @ts-ignore
if (document.documentElement.mozRequestFullScreen) {
// @ts-ignore
document.documentElement.mozRequestFullScreen();
} else { // @ts-ignore
if (document.documentElement.webkitRequestFullscreen) {
// @ts-ignore
document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
}
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
} else { // @ts-ignore
if (document.mozCancelFullScreen) {
// @ts-ignore
document.mozCancelFullScreen();
} else { // @ts-ignore
if (document.webkitCancelFullScreen) {
// @ts-ignore
document.webkitCancelFullScreen();
}
}
}
}
});
Loading

0 comments on commit cc3eeba

Please sign in to comment.