Skip to content

PlusMagi's Blog By Pitt Phunsanit

Plus emotional magic to the knowledge of logic.

  • หน้าแรก
  • About’s Pitt
Close Button

Laravel: authenticate users without DatabaseLaravel: authenticate users without Database

2016-02-062016-02-06| phunsanitphunsanit| 0 Comment | 07:00

ต้องการเขียนระบบ login ง่าย ๆ โดยไม่ใช้ database ในการเก็บ username และ password ในการ login

เริ่มด้วยการแก้ LoginController  ให้ใช้เงื่อนไขใหม่แทนการดึงข้อมูลจากตาราง users

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class LoginController extends Controller
{
 /*
 |--------------------------------------------------------------------------
 | Login Controller
 |--------------------------------------------------------------------------
 |
 | This controller handles authenticating users for the application and
 | redirecting them to your home screen. The controller uses a trait
 | to conveniently provide its functionality to your applications.
 |
  */

 use AuthenticatesUsers;

 /**
  * Where to redirect users after login.
  *
  * @var string
  */
 protected $redirectTo = RouteServiceProvider::HOME;

 /**
  * Create a new controller instance.
  *
  * @return void
  */
 public function __construct()
 {
  $this->middleware('guest')->except('logout');
 }

 /* custom */

 // \vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php

 /**
  * Handle a login request to the application.
  *
  * @param \Illuminate\Http\Request $request
  * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  *
  * @throws \Illuminate\Validation\ValidationException
  */
 public function login(Request $request)
 {

  Log::debug('login ' . $request->input('username') . ' == ' . env('LOGIN_USERNAME') . ' && ' . $request->input('password') . ' == ' . env('LOGIN_PASSWORD'));

  if ($request->input('password') === env('LOGIN_PASSWORD') && $request->input('username') === env('LOGIN_USERNAME')) {
   $request->session()->put('authenticated', time());
   return redirect()->intended('home');
  }

  return view('auth.login', [
   'message' => 'Provided PIN is invalid. ',
  ]);
 }

}

เพิ่ม config ที่เก็บรหัสไว้ในไฟล์ .env เช่น

LOGIN_PASSWORD=phunsanit
LOGIN_USERNAME=pitt

จากนั้นเขียน Middleware มาเช็คว่า session ใน function login มีหรือเปล่า ถ้ามีก็ให้ผ่าน ไม่มีก็ให้ไปหน้า login

<?php

namespace App\Http\Middleware;

use Closure;

class AuthenticateWithSession
{
 /**
  * Handle an incoming request.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Closure $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
  if (!empty(session('authenticated'))) {
   $request->session()->put('authenticated', time());
   return $next($request);
  }

  return redirect('/login');
 }
}

จากนั้นก็เลือก routes ที่จะใช้ middleware ที่เขียนขึ้นมาใหม่ โดยเปิดไฟล์ \routes\web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */
/*
Route::get('/', function () {
return view('welcome');
});

Route::get('/home', 'HomeController@index')->name('home');
 */

Auth::routes();
Route::group(['middleware' => ['web', 'AuthenticateWithSession']], function () {
...
 Route::get('/home', 'HomeController@index')->name('home');
...
});

จากนั้นแนะนำให้ laravel รู้จัก middleware ที่สร้างขึ้นมาใหม่ โดยเปิดไฟล์ \app\Http\Kernel.php และเพิ่ม

 protected $routeMiddleware = [
...

  /* custom */
  'AuthenticateWithSession' => \App\Http\Middleware\AuthenticateWithSession::class,
...
 ];
Read MoreRead More

Posts pagination

ก่อนหน้า 1 … 551 552 553 … 894 ถัดไป

Projects

  • Statement columns mapping Helper
  • Plusmagi Search
  • jQuery Plus Repeater

Recent Posts

  • PostgreSQL: ก็ผ่านมาหลายยุคกว่าจะดัง
  • MySQL: แทรก id ที่ว่างอยู่
  • WordPress: ดูว่า post ทุก ๆ วันเสาร์ใช่มั๋ย
  • ISBN และ ISSN: ที่อยู่บนหนังสือ วารสาร
  • App: QRQR

Archives

Categories

  • AI (4)
  • Businesses (3)
  • Design (33)
    • UX/UI (4)
  • Histories (7)
  • Life (45)
    • Books (18)
      • สืบสวน (4)
    • ECO (3)
    • Sci-Fi (2)
    • พุทธ (3)
  • Network (285)
    • Android (13)
      • F-Droid (7)
    • Apache Apache HTTP Server (17)
    • Docker (28)
    • Homebrew (14)
    • IIS (7)
    • IOT (4)
    • Linux (93)
    • macOS (69)
      • OrbStack (10)
    • Nginx (26)
    • RabbitMQ (4)
    • Samba (5)
    • Shell Script (29)
    • SSH (13)
    • Windows (94)
      • PowerShell (18)
      • WSL (21)
  • Programming (624)
    • API (17)
      • REST (5)
      • Swagger (6)
    • C# (15)
      • .NET Core EF (3)
    • CI/CD (4)
    • CSS (16)
      • Bootstrap (3)
      • Tailwind CSS (3)
    • Database (171)
      • DBeaver (3)
      • RDBMS (153)
        • DB2 (2)
        • MariaDB (21)
        • MySql (60)
        • Oracle Database (6)
          • 10g (2)
        • PostgreSQL (5)
        • SQL Server (86)
          • ADS (3)
          • SMO (4)
          • SSMS (10)
          • T-SQL (26)
    • Flutter (2)
    • GIT (22)
    • HTML (23)
    • Java (44)
      • JasperReports (3)
      • Joget (19)
      • Spring Boot (14)
    • JavaScript (116)
      • AG Grid (1)
      • Angular (3)
      • jQuery (65)
        • DataTables (14)
      • React (2)
      • Tabulator (19)
      • TypeScript (11)
      • Vue.js (5)
    • Node.js (1)
    • PHP (181)
      • CodeIgniter (11)
      • Laravel (36)
        • Laravel 11 (10)
        • Laravel 5 (19)
      • PrestaShop (1)
      • WordPress (22)
      • Yii (19)
        • Yii 2 (16)
    • PowerBuilder (11)
      • PowerBuilder 8 (10)
    • Python (8)
    • Rust (1)
    • SEO (2)
    • Testing (7)
      • Automated Testing (4)
        • Playwright (2)
        • Selenium (2)
    • UML (1)
    • Version Control (1)
  • Programs (80)
    • Bitwarden (5)
    • Excel (8)
    • Figma (7)
    • Google Chrome (6)
    • Mozilla Firefox (9)
    • Oh My ZSH (4)
    • Stalwart (5)
    • USB Boot (7)
  • Q & A (8)
  • Security (27)
  • Tips & Tricks (72)
    • iPhone (7)
      • Jailbreaking (2)
  • Uncategorized (1)
  • กฎหมาย (2)
  • คณิตศาสตร์ (2)

Sirat WordPress Theme By VWThemes

Scroll Up