Implement custom login, register, logout, and delete account functionalities

This commit is contained in:
Nima8FT 2025-04-23 11:18:30 +03:30
parent d62f3cafc7
commit a36c5315b4
10 changed files with 423 additions and 0 deletions

View File

@ -0,0 +1,39 @@
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Requests\LoginRequest;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
public function create()
{
return view("auth.login");
}
public function store(LoginRequest $request)
{
try {
$credentials = $request->only("email", "password");
$remember = $request->has('remember');
if (!Auth::attempt($credentials, $remember)) {
return back()->withErrors([
'email' => 'The provided credentials do not match our records.',
])->onlyInput('email');
}
$request->session()->regenerate();
return redirect()->route('dashboard')->with("success", "login successfully");
} catch (\Exception $e) {
return redirect()
->route('login.create')
->with('error', 'not login please try again' . $e->getMessage());
}
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class LogoutController extends Controller
{
public function logout(Request $request)
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->route('login')->with('success', 'logout successfully');
}
public function deleteAccount(Request $request)
{
$user = Auth::user();
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
$user->delete();
return redirect()->route('login')->with('success', 'Your account has been deleted successfully.');
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Http\Controllers\Auth;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Http\Requests\RegisterRequest;
class RegisterController extends Controller
{
public function create()
{
return view("auth.register");
}
public function store(RegisterRequest $request)
{
try {
$inputs = $request->only('name', 'email', 'password');
$inputs['password'] = Hash::make($inputs['password']);
$user = User::create($inputs);
Auth::login($user);
return redirect()->route('dashboard')->with('success', 'register successfully');
} catch (\Exception $e) {
return redirect()
->route('register.create')
->with('error', 'not register please try again' . $e->getMessage());
}
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class DashboardController extends Controller
{
public function index()
{
$user = Auth::user();
return view("dashboard", compact("user"));
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class LoginRequest 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 [
'email' => 'required|email',
'password' => 'required|min:8',
];
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class RegisterRequest 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' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:8|confirmed',
];
}
}

View File

@ -0,0 +1,63 @@
@extends('layouts.app')
@section('content')
<div class="bg-[#0d1b2a] h-screen flex justify-center items-center">
<div
class="flex justify-center items-center p-4 flex-wrap shadow-lg rounded-xl login-form size-150 max-w-[500px] bg-[#1b263b] h-auto text-center">
<h1 class="w-full font-bold text-4xl text-[#e0e1dd]">Login Page</h1>
<div class="w-[90%] p-5">
<form method="POST" action="{{route('login.store')}}">
@csrf
<div class="text-left my-5">
<label for="username" class="font-bold text-[#e0e1dd]">Email:</label>
<input name="email" type="email" placeholder="Enter your text"
class="w-full my-2 px-4 py-2 border border-[#e0e1dd] text-[#e0e1dd] rounded-md shadow-sm focus:outline-none" />
<p class="w-full text-red-500">@error('email') {{ $message }} @enderror</p>
</div>
<div class="text-left my-5">
<label for="username" class="font-bold text-[#e0e1dd]">Password:</label>
<input name="password" type="password" placeholder="Enter your text"
class="w-full my-2 px-4 py-2 border border-[#e0e1dd] text-[#e0e1dd] rounded-md shadow-sm focus:outline-none" />
<p class="w-full text-red-500">@error('password') {{ $message }} @enderror</p>
</div>
<div class="text-left my-5 flex justify-start items-center">
<input id="remember" type="checkbox" name="remember"
class="accent-blue-500 w-4 h-4 rounded-md focus:outline-none transition ease-in-out border-none" />
<label for="remember" class="font-bold text-[#e0e1dd] px-2 cursor-pointer">
Remember me
</label>
</div>
<div class="text-[#e0e1dd] flex justify-between">
<a href="#ss"
class="cursor-pointer font-medium underline hover:opacity-85 transition duration-300 ease-in-out">Forgot
Password</a>
<a href="{{route('register.store')}}"
class="cursor-pointer font-medium underline hover:opacity-85 transition duration-300 ease-in-out">Register</a>
</div>
{{-- captcha scction --}}
<div class="flex justify-center mt-4">
<div class="h-captcha" data-sitekey="2bf85600-17f6-47f4-86ac-2e6a4720a818"></div>
</div>
<p class="w-full text-center text-red-500 mt-2">
@error('h-captcha-response')
{{ $message }}
@enderror
</p>
<div class="mt-8">
<button type="submit"
class="border border-[#e0e1dd] font-medium w-full p-2 rounded-md bg-[#e0e1dd] cursor-pointer hover:opacity-85 transition duration-300 ease-in-out text-black">Login</button>
</div>
</form>
<div class="mt-4 flex justify-between items-center gap-4">
<form action="#" method="GET" class="w-full">
<button
class="border border-[#e0e1dd] text-[#e0e1dd] w-full p-2 rounded-md font-medium cursor-pointer hover:opacity-85 transition duration-300 ease-in-out hover:bg-[#e0e1dd] hover:text-[#1b263b]">With
Github</button>
</form>
</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,58 @@
@extends('layouts.app')
@section('content')
<div class="bg-[#0d1b2a] h-screen flex justify-center items-center">
<div
class="flex justify-center items-center flex-wrap shadow-lg rounded-xl login-form size-150 max-w-[500px] bg-[#1b263b] h-auto text-center p-4">
<h1 class="w-full font-bold text-4xl text-[#e0e1dd] mt-3">Register Page</h1>
<div class="w-[90%] p-5">
<form method="POST" action="{{ route(name: 'register.store') }}">
@csrf
<div class="text-left my-5">
<label for="username" class="font-bold text-[#e0e1dd]">Name:</label>
<input name="name" type="text" placeholder="Enter your text"
class="w-full my-2 px-4 py-2 border border-[#e0e1dd] text-[#e0e1dd] rounded-md shadow-sm focus:outline-none" />
<p class="w-full text-red-500">@error('name') {{ $message }} @enderror</p>
</div>
<div class="text-left my-5">
<label for="username" class="font-bold text-[#e0e1dd]">Email:</label>
<input name="email" type="email" placeholder="Enter your text"
class="w-full my-2 px-4 py-2 border border-[#e0e1dd] text-[#e0e1dd] rounded-md shadow-sm focus:outline-none" />
<p class="w-full text-red-500">@error('email') {{ $message }} @enderror</p>
</div>
<div class="text-left my-5">
<label for="username" class="font-bold text-[#e0e1dd]">Password:</label>
<input name="password" type="password" placeholder="Enter your text"
class="w-full my-2 px-4 py-2 border border-[#e0e1dd] text-[#e0e1dd] rounded-md shadow-sm focus:outline-none" />
<p class="w-full text-red-500">@error('password') {{ $message }} @enderror</p>
</div>
<div class="text-left my-5">
<label for="username" class="font-bold text-[#e0e1dd]">Confirm Password:</label>
<input name="password_confirmation" type="password" placeholder="Enter your text"
class="w-full my-2 px-4 py-2 border border-[#e0e1dd] text-[#e0e1dd] rounded-md shadow-sm focus:outline-none" />
<p class="w-full text-red-500">@error('password_confirmation') {{ $message }} @enderror</p>
</div>
<div class="mt-8">
<button
class="border border-[#e0e1dd] font-medium w-full p-2 rounded-md bg-[#e0e1dd] cursor-pointer hover:opacity-85 transition duration-300 ease-in-out text-black">Register</button>
</div>
</form>
<div class="mt-4 flex justify-between items-center gap-4">
<form action="#" method="GET" class="w-full">
<button
class="border border-[#e0e1dd] text-[#e0e1dd] w-full p-2 rounded-md font-medium cursor-pointer hover:opacity-85 transition duration-300 ease-in-out hover:bg-[#e0e1dd] hover:text-[#1b263b]">With
Github</button>
</form>
</div>
<div class="text-[#e0e1dd] mt-4">
<a href="{{route('login')}}">
Already have an account?
<span
class="cursor-pointer font-medium underline hover:opacity-85 transition duration-300 ease-in-out">Login</span>
</a>
</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,101 @@
@extends('layouts.app')
@section('content')
<div class="bg-[#0d1b2a] min-h-screen flex justify-center items-center space-x-4">
<aside class="bg-[#1b263b] h-screen w-[300px] rounded-xl flex flex-col justify-between p-5 text-white shadow-lg">
<div class="mt-5">
<div class="text-center mb-6">
<h1 class="font-bold text-3xl">{{$user->name}} Panel</h1>
<p class="text-gray-300 mt-1">You are logged in!</p>
</div>
<nav class="space-y-2">
<a href="{{ route('dashboard') }}" class="block px-4 py-2 rounded-lg hover:bg-[#415a77] transition">
📊 Dashboard
</a>
<a href="#" class="block px-4 py-2 rounded-lg hover:bg-[#415a77] transition">👤
Profile</a>
<a href="#" class="block px-4 py-2 rounded-lg hover:bg-[#415a77] transition">
🔐 Change Password
</a>
<a href="#" class="block px-4 py-2 rounded-lg hover:bg-[#415a77] transition">
🖥️ Browse Session
</a>
</nav>
</div>
<div class="mt-6 space-y-4">
<form action="{{route('logout')}}" method="POST">
@csrf
<button type="submit"
class="block text-center w-full border border-red-500 text-red-500 hover:bg-red-500 hover:text-white transition-all duration-300 rounded-lg py-2 cursor-pointer">Logout</button>
</form>
<form action="{{route('delete.account')}}" method="POST">
@csrf
<button type="submit"
class="block text-center w-full border border-red-500 text-red-500 hover:bg-red-500 hover:text-white transition-all duration-300 rounded-lg py-2 cursor-pointer">Delete
Account</button>
</form>
</div>
</aside>
<main class="min-h-screen w-full flex items-center justify-center bg-[#0d1b2a] overflow-auto py-5">
<div class="bg-[#1b263b] p-8 rounded-xl shadow-2xl max-w-[800px] w-full transition-all duration-300">
<h1 class="text-3xl font-semibold text-center text-[#e0e1dd] mb-10">
{{ $user->email }}
</h1>
@if (session('success'))
<div
class="mb-4 px-4 py-3 rounded-md bg-green-600/20 text-green-300 border border-green-500 flex items-center justify-between shadow">
<span>{{ session('success') }}</span>
<button onclick="this.parentElement.remove()"
class="text-green-300 hover:text-white transition duration-200 cursor-pointer">&times;</button>
</div>
@endif
<div class="flex flex-col md:flex-row gap-6 justify-center items-start flex-wrap">
{{-- Email Verification --}}
@if (!$user->email_verified_at)
<form action="#" method="POST" class="mb-3 w-full">
@csrf
<button type="submit"
class="px-6 cursor-pointer py-3 bg-blue-600 text-white rounded-lg font-semibold hover:bg-blue-700 transition">
Verify Your Email
</button>
</form>
<p class="text-sm text-red-300">Your email is not verified.</p>
@else
<div class="px-5 py-3 bg-green-600/20 text-green-400 border border-green-600 rounded-md shadow-sm">
Your Email Is Verified
</div>
@endif
{{-- Two Factor Auth --}}
@if (!$user->google2fa_secret)
<form method="GET" action="#">
@csrf
<button type="submit"
class="inline-block cursor-pointer px-6 py-3 border border-blue-600 text-white font-semibold rounded-lg hover:bg-blue-700 transition ease-in-out duration-300">
Enable Two Factor Authentication
</button>
</form>
@else
<form method="POST" action="#">
@csrf
<button type="submit"
class="inline-block cursor-pointer px-6 py-3 border border-red-600 text-white font-semibold rounded-lg hover:bg-red-700 transition ease-in-out duration-300">
Disable Two Factor Authentication
</button>
</form>
@endif
</div>
</div>
</main>
</div>
@endsection

View File

@ -1,7 +1,24 @@
<?php
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\LogoutController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\DashboardController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
})->middleware('auth');
Route::get('register', [RegisterController::class, 'create'])->name('register');
Route::post('register', [RegisterController::class, 'store'])->name('register.store');
Route::get('login', [LoginController::class, 'create'])->name('login');
Route::post('login', [LoginController::class, 'store'])->name('login.store');
Route::group(['middleware' => 'auth'], function () {
Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard');
Route::post('logout', action: [LogoutController::class, 'logout'])->name('logout');
Route::post('delete-account', action: [LogoutController::class, 'deleteAccount'])->name('delete.account');
});