help.txt 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. -------------------------------------------------
  2. ✅ Install Laravel
  3. -------------------------------------------------
  4. composer create-project laravel/laravel multiauth
  5. cd multiauth
  6. php artisan serve
  7. ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  8. -------------------------------------------------
  9. ✅ config > auth.php
  10. -------------------------------------------------
  11. 'guards' => [
  12. 'web' => [
  13. 'driver' => 'session',
  14. 'provider' => 'users',
  15. ],
  16. 'admin' => [
  17. 'driver' => 'session',
  18. 'provider' => 'admins',
  19. ],
  20. ],
  21. 'providers' => [
  22. 'users' => [
  23. 'driver' => 'eloquent',
  24. 'model' => env('AUTH_MODEL', App\Models\User::class),
  25. ],
  26. 'admins' => [
  27. 'driver' => 'eloquent',
  28. 'model' => env('AUTH_MODEL', App\Models\Admin::class),
  29. ],
  30. ],
  31. ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  32. -------------------------------------------------
  33. ✅ Migrations
  34. -------------------------------------------------
  35. php artisan make:migration create_admins_table
  36. -------------------------------------------------
  37. ✅ Migration > users table
  38. -------------------------------------------------
  39. $table->id();
  40. $table->string('name');
  41. $table->string('email')->unique();
  42. $table->string('photo')->nullable();
  43. $table->string('password');
  44. $table->string('phone')->nullable();
  45. $table->string('country')->nullable();
  46. $table->string('address')->nullable();
  47. $table->string('state')->nullable();
  48. $table->string('city')->nullable();
  49. $table->string('zip')->nullable();
  50. $table->string('token')->nullable();
  51. $table->string('status')->default(0)->comment('0=pending, 1=active, 2=suspended');
  52. $table->timestamps();
  53. -------------------------------------------------
  54. ✅ Migration > admins table
  55. -------------------------------------------------
  56. $table->id();
  57. $table->string('name');
  58. $table->string('email')->unique();
  59. $table->string('photo')->nullable();
  60. $table->string('password');
  61. $table->string('token')->nullable();
  62. $table->timestamps();
  63. -------------------------------------------------
  64. ✅ Run Migration
  65. -------------------------------------------------
  66. php artisan migrate
  67. ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  68. -------------------------------------------------
  69. ✅ Models
  70. -------------------------------------------------
  71. php artisan make:model Admin
  72. -------------------------------------------------
  73. ✅ app/Models/User.php
  74. -------------------------------------------------
  75. use Illuminate\Contracts\Auth\MustVerifyEmail;
  76. use Illuminate\Database\Eloquent\Factories\HasFactory;
  77. use Illuminate\Foundation\Auth\User as Authenticatable;
  78. use Illuminate\Notifications\Notifiable;
  79. class User extends Authenticatable implements MustVerifyEmail
  80. use HasFactory, Notifiable;
  81. protected $fillable = [
  82. 'name',
  83. 'email',
  84. 'photo',
  85. 'password',
  86. 'phone',
  87. 'country',
  88. 'address',
  89. 'state',
  90. 'city',
  91. 'zip',
  92. 'token',
  93. 'status',
  94. ];
  95. -------------------------------------------------
  96. ✅ app/Models/Admin.php
  97. -------------------------------------------------
  98. use Illuminate\Database\Eloquent\Factories\HasFactory;
  99. use Illuminate\Database\Eloquent\Model;
  100. use Illuminate\Foundation\Auth\User as Authenticatable;
  101. use Illuminate\Notifications\Notifiable;
  102. class Admin extends authenticatable
  103. use HasFactory, Notifiable;
  104. ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  105. -------------------------------------------------
  106. ✅ Seeder
  107. -------------------------------------------------
  108. php artisan make:seeder AdminSeeder
  109. -------------------------------------------------
  110. ✅ seeders/AdminSeeder.php
  111. -------------------------------------------------
  112. use App\Models\Admin;
  113. use Hash;
  114. public function run(): void
  115. {
  116. $obj = new Admin;
  117. $obj->name = "Admin";
  118. $obj->email = "admin@gmail.com";
  119. $obj->photo = "";
  120. $obj->password = Hash::make('1234');
  121. $obj->token = "";
  122. $obj->save();
  123. }
  124. -------------------------------------------------
  125. ✅ seeders/DatabaseSeeder.php
  126. -------------------------------------------------
  127. public function run(): void
  128. {
  129. $this->call([AdminSeeder::class]);
  130. }
  131. -------------------------------------------------
  132. ✅ Run Seeder
  133. -------------------------------------------------
  134. php artisan db:seed
  135. ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  136. -------------------------------------------------
  137. ✅ Email
  138. -------------------------------------------------
  139. php artisan make:mail Websitemail
  140. -------------------------------------------------
  141. ✅ app/Mail/Websitemail.php
  142. -------------------------------------------------
  143. public $subject, $body;
  144. public function __construct($subject,$body)
  145. {
  146. $this->subject = $subject;
  147. $this->body = $body;
  148. }
  149. public function envelope(): Envelope
  150. {
  151. return new Envelope(
  152. subject: $this->subject,
  153. );
  154. }
  155. public function content(): Content
  156. {
  157. return new Content(
  158. view: 'email'
  159. );
  160. }
  161. -------------------------------------------------
  162. ✅ resources/views/email.blade.php
  163. -------------------------------------------------
  164. {!! $body !!}
  165. ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  166. -------------------------------------------------
  167. ✅ Middlewares
  168. -------------------------------------------------
  169. php artisan make:middleware User
  170. php artisan make:middleware Admin
  171. -------------------------------------------------
  172. ✅ app/Http/Middleware/User.php
  173. -------------------------------------------------
  174. use Auth;
  175. public function handle(Request $request, Closure $next): Response
  176. {
  177. if(!Auth::guard('web')->check()) {
  178. return redirect()->route('login');
  179. }
  180. return $next($request);
  181. }
  182. -------------------------------------------------
  183. ✅ app/Http/Middleware/Admin.php
  184. -------------------------------------------------
  185. use Auth;
  186. public function handle(Request $request, Closure $next): Response
  187. {
  188. if(!Auth::guard('admin')->check()) {
  189. return redirect()->route('admin_login');
  190. }
  191. return $next($request);
  192. }
  193. -------------------------------------------------
  194. ✅ bootstrap/app.php
  195. -------------------------------------------------
  196. ->withMiddleware(function (Middleware $middleware) {
  197. $middleware->alias([
  198. 'admin' => \App\Http\Middleware\Admin::class,
  199. 'auth' => \App\Http\Middleware\User::class,
  200. ]);
  201. })
  202. ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  203. -------------------------------------------------
  204. ✅ routes/web.php
  205. -------------------------------------------------
  206. use App\Http\Controllers\FrontController;
  207. use App\Http\Controllers\User\UserController;
  208. use App\Http\Controllers\Admin\AdminController;
  209. // Front
  210. Route::get('/', [FrontController::class, 'home'])->name('home');
  211. Route::get('/about', [FrontController::class, 'about'])->name('about');
  212. // User
  213. Route::middleware('auth')->group(function () {
  214. Route::get('/dashboard',[UserController::class,'dashboard'])->name('dashboard');
  215. });
  216. Route::get('/register',[UserController::class,'register'])->name('register');
  217. Route::post('/register_submit',[UserController::class,'register_submit'])->name('register_submit');
  218. Route::get('/register_verify/{token}/{email}',[UserController::class,'register_verify'])->name('register_verify');
  219. Route::get('/login',[UserController::class,'login'])->name('login');
  220. Route::post('/login',[UserController::class,'login_submit'])->name('login_submit');
  221. Route::get('/logout',[UserController::class,'logout'])->name('logout');
  222. Route::get('/forget_password',[UserController::class,'forget_password'])->name('forget_password');
  223. Route::post('/forget_password',[UserController::class,'forget_password_submit'])->name('forget_password_submit');
  224. Route::get('/reset-password/{token}/{email}',[UserController::class,'reset_password'])->name('reset_password');
  225. Route::post('/reset-password/{token}/{email}',[UserController::class,'reset_password_submit'])->name('reset_password_submit');
  226. // Admin
  227. Route::middleware('admin')->prefix('admin')->group(function () {
  228. Route::get('/dashboard',[AdminController::class,'dashboard'])->name('admin_dashboard');
  229. Route::get('/profile',[AdminController::class,'profile'])->name('admin_profile');
  230. Route::post('/profile',[AdminController::class,'profile_update'])->name('admin_profile_update');
  231. });
  232. Route::prefix('admin')->group(function () {
  233. Route::get('/', function () {return redirect('/admin/login');});
  234. Route::get('/login',[AdminController::class,'login'])->name('admin_login');
  235. Route::post('/login',[AdminController::class,'login_submit'])->name('admin_login_submit');
  236. Route::get('/logout',[AdminController::class,'logout'])->name('admin_logout');
  237. Route::get('/forget-password',[AdminController::class,'forget_password'])->name('admin_forget_password');
  238. Route::post('/forget_password_submit',[AdminController::class,'forget_password_submit'])->name('admin_forget_password_submit');
  239. Route::get('/reset-password/{token}/{email}',[AdminController::class,'reset_password'])->name('admin_reset_password');
  240. Route::post('/reset-password/{token}/{email}',[AdminController::class,'reset_password_submit'])->name('admin_reset_password_submit');
  241. });
  242. ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  243. -------------------------------------------------
  244. ✅ Views
  245. -------------------------------------------------
  246. php artisan make:view front/home
  247. php artisan make:view front/about
  248. php artisan make:view admin/login
  249. php artisan make:view admin/dashboard
  250. php artisan make:view admin/forget-password
  251. php artisan make:view admin/reset-password
  252. php artisan make:view user/register
  253. php artisan make:view user/login
  254. php artisan make:view user/dashboard
  255. php artisan make:view user/forget-password
  256. php artisan make:view user/reset-password
  257. -------------------------------------------------
  258. ✅ views/front/home.blade.php
  259. -------------------------------------------------
  260. <a href="{{ route('home') }}">Home</a> | <a href="{{ route('about') }}">About</a> | <a href="{{ route('login') }}">Login</a> | <a href="{{ route('register') }}">Register</a>
  261. <h2>Home Page</h2>
  262. -------------------------------------------------
  263. ✅ views/front/about.blade.php
  264. -------------------------------------------------
  265. <a href="{{ route('home') }}">Home</a> | <a href="{{ route('about') }}">About</a> | <a href="{{ route('login') }}">Login</a> | <a href="{{ route('register') }}">Register</a>
  266. <h2>About Page</h2>
  267. -------------------------------------------------
  268. ✅ views/admin/login.blade.php
  269. -------------------------------------------------
  270. <h2>Admin Login</h2>
  271. @if($errors->any())
  272. @foreach ($errors->all() as $error)
  273. {{ $error }}<br>
  274. @endforeach
  275. @endif
  276. @if(session('success'))
  277. {{ session('success') }}
  278. @endif
  279. @if(session('error'))
  280. {{ session('error') }}
  281. @endif
  282. <form action="{{ route('admin_login_submit') }}" method="post">
  283. @csrf
  284. <table>
  285. <tr>
  286. <td>Email: </td>
  287. <td><input type="text" name="email" placeholder="Email"></td>
  288. </tr>
  289. <tr>
  290. <td>Password: </td>
  291. <td><input type="password" name="password" placeholder="Password"></td>
  292. </tr>
  293. <tr>
  294. <td></td>
  295. <td>
  296. <button type="submit">Login</button>
  297. <div><a href="{{ route('admin_forget_password') }}">Forget Password?</a></div>
  298. </td>
  299. </tr>
  300. </table>
  301. </form>
  302. -------------------------------------------------
  303. ✅ views/admin/dashboard.blade.php
  304. -------------------------------------------------
  305. <h2>Admin Dashboard</h2>
  306. <a href="{{ route('admin_logout') }}">Logout</a>
  307. -------------------------------------------------
  308. ✅ views/admin/forget-password.blade.php
  309. -------------------------------------------------
  310. <h2>Admin Forget Password</h2>
  311. @if($errors->any())
  312. @foreach ($errors->all() as $error)
  313. {{ $error }}<br>
  314. @endforeach
  315. @endif
  316. @if(session('success'))
  317. {{ session('success') }}
  318. @endif
  319. @if(session('error'))
  320. {{ session('error') }}
  321. @endif
  322. <form action="{{ route('admin_forget_password_submit') }}" method="post">
  323. @csrf
  324. <table>
  325. <tr>
  326. <td>Email: </td>
  327. <td><input type="text" name="email" placeholder="Email"></td>
  328. </tr>
  329. <tr>
  330. <td></td>
  331. <td>
  332. <button type="submit">Submit</button>
  333. <div><a href="{{ route('admin_login') }}">Back to login page</a></div>
  334. </td>
  335. </tr>
  336. </table>
  337. </form>
  338. -------------------------------------------------
  339. ✅ views/admin/reset-password.blade.php
  340. -------------------------------------------------
  341. <h2>Admin Reset Password</h2>
  342. @if($errors->any())
  343. @foreach ($errors->all() as $error)
  344. {{ $error }}<br>
  345. @endforeach
  346. @endif
  347. @if(session('success'))
  348. {{ session('success') }}
  349. @endif
  350. @if(session('error'))
  351. {{ session('error') }}
  352. @endif
  353. <form action="{{ route('admin_reset_password_submit',[$token,$email]) }}" method="post">
  354. @csrf
  355. <table>
  356. <tr>
  357. <td>Password: </td>
  358. <td><input type="password" name="password"></td>
  359. </tr>
  360. <tr>
  361. <td>Confirm Password: </td>
  362. <td><input type="password" name="confirm_password"></td>
  363. </tr>
  364. <tr>
  365. <td></td>
  366. <td>
  367. <button type="submit">Submit</button>
  368. </td>
  369. </tr>
  370. </table>
  371. </form>
  372. -------------------------------------------------
  373. ✅ views/user/register.blade.php
  374. -------------------------------------------------
  375. <a href="{{ route('home') }}">Home</a> | <a href="{{ route('about') }}">About</a> | <a href="{{ route('login') }}">Login</a> | <a href="{{ route('register') }}">Register</a>
  376. <h2>User Registration</h2>
  377. @if($errors->any())
  378. @foreach ($errors->all() as $error)
  379. {{ $error }}<br>
  380. @endforeach
  381. @endif
  382. @if(session('success'))
  383. {{ session('success') }}
  384. @endif
  385. @if(session('error'))
  386. {{ session('error') }}
  387. @endif
  388. <form action="{{ route('register_submit') }}" method="post">
  389. @csrf
  390. <table>
  391. <tr>
  392. <td>Name: </td>
  393. <td><input type="text" name="name" placeholder="Name"></td>
  394. </tr>
  395. <tr>
  396. <td>Email: </td>
  397. <td><input type="text" name="email" placeholder="Email"></td>
  398. </tr>
  399. <tr>
  400. <td>Password: </td>
  401. <td><input type="password" name="password" placeholder="Password"></td>
  402. </tr>
  403. <tr>
  404. <td>Confirm Password: </td>
  405. <td><input type="password" name="confirm_password" placeholder="Confirm Password"></td>
  406. </tr>
  407. <tr>
  408. <td></td>
  409. <td><button type="submit">Submit</button></td>
  410. </tr>
  411. </table>
  412. </form>
  413. -------------------------------------------------
  414. ✅ views/user/login.blade.php
  415. -------------------------------------------------
  416. <a href="{{ route('home') }}">Home</a> | <a href="{{ route('about') }}">About</a> | <a href="{{ route('login') }}">Login</a> | <a href="{{ route('register') }}">Register</a>
  417. <h2>User Login</h2>
  418. @if($errors->any())
  419. @foreach ($errors->all() as $error)
  420. {{ $error }}<br>
  421. @endforeach
  422. @endif
  423. @if(session('success'))
  424. {{ session('success') }}
  425. @endif
  426. @if(session('error'))
  427. {{ session('error') }}
  428. @endif
  429. <form action="{{ route('login_submit') }}" method="post">
  430. @csrf
  431. <table>
  432. <tr>
  433. <td>Email: </td>
  434. <td><input type="text" name="email" placeholder="Email"></td>
  435. </tr>
  436. <tr>
  437. <td>Password: </td>
  438. <td><input type="password" name="password" placeholder="Password"></td>
  439. </tr>
  440. <tr>
  441. <td></td>
  442. <td>
  443. <button type="submit">Login</button>
  444. <div><a href="{{ route('forget_password') }}">Forget Password?</a></div>
  445. </td>
  446. </tr>
  447. </table>
  448. </form>
  449. -------------------------------------------------
  450. ✅ views/user/dashboard.blade.php
  451. -------------------------------------------------
  452. <h2>User Dashboard</h2>
  453. <a href="{{ route('logout') }}">Logout</a>
  454. -------------------------------------------------
  455. ✅ views/user/forget-password.blade.php
  456. -------------------------------------------------
  457. <a href="{{ route('home') }}">Home</a> | <a href="{{ route('about') }}">About</a> | <a href="{{ route('login') }}">Login</a> | <a href="{{ route('register') }}">Register</a>
  458. <h2>User Forget Password</h2>
  459. @if($errors->any())
  460. @foreach ($errors->all() as $error)
  461. {{ $error }}<br>
  462. @endforeach
  463. @endif
  464. @if(session('success'))
  465. {{ session('success') }}
  466. @endif
  467. @if(session('error'))
  468. {{ session('error') }}
  469. @endif
  470. <form action="{{ route('forget_password_submit') }}" method="post">
  471. @csrf
  472. <table>
  473. <tr>
  474. <td>Email: </td>
  475. <td><input type="text" name="email" placeholder="Email"></td>
  476. </tr>
  477. <tr>
  478. <td></td>
  479. <td>
  480. <button type="submit">Submit</button>
  481. <div><a href="{{ route('login') }}">Back to login page</a></div>
  482. </td>
  483. </tr>
  484. </table>
  485. </form>
  486. -------------------------------------------------
  487. ✅ views/user/reset-password.blade.php
  488. -------------------------------------------------
  489. <a href="{{ route('home') }}">Home</a> | <a href="{{ route('about') }}">About</a> | <a href="{{ route('login') }}">Login</a> | <a href="{{ route('register') }}">Register</a>
  490. <h2>User Reset Password</h2>
  491. @if($errors->any())
  492. @foreach ($errors->all() as $error)
  493. {{ $error }}<br>
  494. @endforeach
  495. @endif
  496. @if(session('success'))
  497. {{ session('success') }}
  498. @endif
  499. @if(session('error'))
  500. {{ session('error') }}
  501. @endif
  502. <form action="{{ route('reset_password_submit',[$token,$email]) }}" method="post">
  503. @csrf
  504. <table>
  505. <tr>
  506. <td>Password: </td>
  507. <td><input type="password" name="password"></td>
  508. </tr>
  509. <tr>
  510. <td>Confirm Password: </td>
  511. <td><input type="password" name="confirm_password"></td>
  512. </tr>
  513. <tr>
  514. <td></td>
  515. <td>
  516. <button type="submit">Submit</button>
  517. </td>
  518. </tr>
  519. </table>
  520. </form>
  521. ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  522. -------------------------------------------------
  523. ✅ Controllers
  524. -------------------------------------------------
  525. php artisan make:controller FrontController
  526. php artisan make:controller Admin/AdminController
  527. php artisan make:controller User/UserController
  528. -------------------------------------------------
  529. ✅ app\Http\Controllers\FrontController.php
  530. -------------------------------------------------
  531. public function home()
  532. {
  533. return view('front.home');
  534. }
  535. public function about()
  536. {
  537. return view('front.about');
  538. }
  539. -------------------------------------------------
  540. ✅ app\Http\Controllers\Admin\AdminController.php
  541. -------------------------------------------------
  542. use Auth;
  543. use Hash;
  544. use App\Models\Admin;
  545. use App\Mail\Websitemail;
  546. public function dashboard()
  547. {
  548. return view('admin.dashboard');
  549. }
  550. public function login()
  551. {
  552. return view('admin.login');
  553. }
  554. public function login_submit(Request $request)
  555. {
  556. $request->validate([
  557. 'email' => ['required', 'email'],
  558. 'password' => ['required'],
  559. ]);
  560. $check = $request->all();
  561. $data = [
  562. 'email' => $check['email'],
  563. 'password' => $check['password']
  564. ];
  565. if(Auth::guard('admin')->attempt($data)) {
  566. return redirect()->route('admin_dashboard');
  567. } else {
  568. return redirect()->route('admin_login')->with('error','The information you entered is incorrect! Please try again!');
  569. }
  570. }
  571. public function logout()
  572. {
  573. Auth::guard('admin')->logout();
  574. return redirect()->route('admin_login')->with('success','Logout is successful!');
  575. }
  576. public function forget_password()
  577. {
  578. return view('admin.forget-password');
  579. }
  580. public function forget_password_submit(Request $request)
  581. {
  582. $request->validate([
  583. 'email' => ['required', 'email'],
  584. ]);
  585. $admin = Admin::where('email',$request->email)->first();
  586. if(!$admin) {
  587. return redirect()->back()->with('error','Email is not found');
  588. }
  589. $token = hash('sha256',time());
  590. $admin->token = $token;
  591. $admin->update();
  592. $reset_link = url('admin/reset-password/'.$token.'/'.$request->email);
  593. $subject = "Password Reset";
  594. $message = "To reset password, please click on the link below:<br>";
  595. $message .= "<a href='".$reset_link."'>Click Here</a>";
  596. \Mail::to($request->email)->send(new Websitemail($subject,$message));
  597. return redirect()->back()->with('success','We have sent a password reset link to your email. Please check your email. If you do not find the email in your inbox, please check your spam folder.');
  598. }
  599. public function reset_password($token,$email)
  600. {
  601. $admin = Admin::where('email',$email)->where('token',$token)->first();
  602. if(!$admin) {
  603. return redirect()->route('admin_login')->with('error','Token or email is not correct');
  604. }
  605. return view('admin.reset-password', compact('token','email'));
  606. }
  607. public function reset_password_submit(Request $request, $token, $email)
  608. {
  609. $request->validate([
  610. 'password' => ['required'],
  611. 'confirm_password' => ['required','same:password'],
  612. ]);
  613. $admin = Admin::where('email',$request->email)->where('token',$request->token)->first();
  614. $admin->password = Hash::make($request->password);
  615. $admin->token = "";
  616. $admin->update();
  617. return redirect()->route('admin_login')->with('success','Password reset is successful. You can login now.');
  618. }
  619. -------------------------------------------------
  620. ✅ app\Http\Controllers\User\UserController.php
  621. -------------------------------------------------
  622. use Auth;
  623. use Hash;
  624. use App\Models\User;
  625. use App\Mail\Websitemail;
  626. public function dashboard()
  627. {
  628. return view('user.dashboard');
  629. }
  630. public function login()
  631. {
  632. return view('user.login');
  633. }
  634. public function login_submit(Request $request)
  635. {
  636. $request->validate([
  637. 'email' => ['required', 'email'],
  638. 'password' => ['required'],
  639. ]);
  640. $check = $request->all();
  641. $data = [
  642. 'email' => $check['email'],
  643. 'password' => $check['password'],
  644. 'status' => 1,
  645. ];
  646. if(Auth::guard('web')->attempt($data)) {
  647. return redirect()->route('dashboard');
  648. } else {
  649. return redirect()->route('login')->with('error','The information you entered is incorrect! Please try again!');
  650. }
  651. }
  652. public function logout()
  653. {
  654. Auth::guard('web')->logout();
  655. return redirect()->route('login')->with('success','Logout is successful!');
  656. }
  657. public function register()
  658. {
  659. return view('user.register');
  660. }
  661. public function register_submit(Request $request)
  662. {
  663. $request->validate([
  664. 'name' => ['required'],
  665. 'email' => ['required', 'email', 'unique:users'],
  666. 'password' => ['required'],
  667. 'confirm_password' => ['required','same:password'],
  668. ]);
  669. $user = new User();
  670. $user->name = $request->name;
  671. $user->email = $request->email;
  672. $user->password = Hash::make($request->password);
  673. $token = hash('sha256',time());
  674. $user->token = $token;
  675. $user->save();
  676. $verification_link = url('register_verify/'.$token.'/'.$request->email);
  677. $subject = "Registration Verification";
  678. $message = "To complete registration, please click on the link below:<br>";
  679. $message .= "<a href='".$verification_link."'>Click Here</a>";
  680. \Mail::to($request->email)->send(new Websitemail($subject,$message));
  681. return redirect()->back()->with('success','Your registration is completed. Please check your email for verification. If you do not find the email in your inbox, please check your spam folder.');
  682. }
  683. public function register_verify($token,$email)
  684. {
  685. $user = User::where('token',$token)->where('email',$email)->first();
  686. if(!$user) {
  687. return redirect()->route('login');
  688. }
  689. $user->token = '';
  690. $user->status = 1;
  691. $user->update();
  692. return redirect()->route('login')->with('success', 'Your email is verified. You can login now.');
  693. }
  694. public function forget_password()
  695. {
  696. return view('user.forget-password');
  697. }
  698. public function forget_password_submit(Request $request)
  699. {
  700. $request->validate([
  701. 'email' => ['required', 'email'],
  702. ]);
  703. $user = User::where('email',$request->email)->first();
  704. if(!$user) {
  705. return redirect()->back()->with('error','Email is not found');
  706. }
  707. $token = hash('sha256',time());
  708. $user->token = $token;
  709. $user->update();
  710. $reset_link = url('reset-password/'.$token.'/'.$request->email);
  711. $subject = "Password Reset";
  712. $message = "To reset password, please click on the link below:<br>";
  713. $message .= "<a href='".$reset_link."'>Click Here</a>";
  714. \Mail::to($request->email)->send(new Websitemail($subject,$message));
  715. return redirect()->back()->with('success','We have sent a password reset link to your email. Please check your email. If you do not find the email in your inbox, please check your spam folder.');
  716. }
  717. public function reset_password($token,$email)
  718. {
  719. $user = User::where('email',$email)->where('token',$token)->first();
  720. if(!$user) {
  721. return redirect()->route('login')->with('error','Token or email is not correct');
  722. }
  723. return view('user.reset-password', compact('token','email'));
  724. }
  725. public function reset_password_submit(Request $request, $token, $email)
  726. {
  727. $request->validate([
  728. 'password' => ['required'],
  729. 'confirm_password' => ['required','same:password'],
  730. ]);
  731. $user = User::where('email',$request->email)->where('token',$request->token)->first();
  732. $user->password = Hash::make($request->password);
  733. $user->token = "";
  734. $user->update();
  735. return redirect()->route('login')->with('success','Password reset is successful. You can login now.');
  736. }