Skip to content

PlusMagi's Blog By Pitt Phunsanit

Plus emotional magic to the knowledge of logic.

  • About’s Pitt
Close Button
PlusMagi's Blog By Pitt Phunsanit API,Authentication,Backend,Cybersecurity,Laravel,PHP Laravel: authenticate users without Database

Laravel: authenticate users without Database

2003-11-212003-11-21| phunsanitphunsanit| 0 Comment | 10:00
Categories:
  • API
  • Authentication
  • Backend
  • Cybersecurity
  • Laravel
  • PHP

ต้องการเขียนระบบ 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,
... ];
Tags: .web, [emerg], Ajax, Authentication, backend, Custom, env, htaccess, Laravel, Login, ปัญญาประดิษฐ์

แนะแนวเรื่อง

PREVIOUS Previous post: การปฏิบัติตามกฎหมายและการกำกับดูแล (Compliance & Asset Governance)
NEXT Next post: Belief System & Worldview (ระบบความเชื่อและโครงสร้างชีวทัศน์ของมนุษย์)

Projects

  • Statement Columns Mapping Helper
  • PlusMagi Blocks
  • PlusMagi Site Search
  • PlusMagi Tags Reindex
  • jQuery Plus Repeater

Recent Posts

  • มือถือไม่เปิดเครื่องเอง เมื่อถึงเวลาปลุก
  • A-10 Thunderbolt II: ตำนานหมูป่าหุ้มเกราะ ผู้ครองน่านฟ้าสนับสนุนภาคพื้นดิน
  • เจาะลึก Lockheed Martin F-35 Lightning II: พลิกโฉมสงครามทางอากาศยุคที่ 5 และการประชันสมรรถนะของ 3 รุ่นย่อย
  • สถิติไร้พ่ายบนขอบอวกาศ: เจาะลึกตำนาน Lockheed SR-71 Blackbird
  • ย้อนรอย Lockheed Martin F-22 Raptor: ราชาแห่งการครองอากาศยุคที่ 5 ที่ไม่มีวันส่งออก

Archives

Categories

  • applied mathematics (302)
    • agriculture (20)
    • Astronomy (27)
    • Botany (93)
    • chemistry (42)
    • Cosmology (4)
    • Environmental Science (22)
    • physics (3)
    • statistics (2)
  • business (2,129)
    • Accounting (136)
    • Business Analysis (912)
    • collaboration (120)
    • Corporate Governance (4)
    • Finance (107)
    • Governance (10)
    • Marketing (67)
    • Operations Management (2)
    • Real Estate (31)
    • risk management (2)
  • computer science (3,067)
    • AI (494)
    • Biology (211)
      • Environment (54)
    • Cloud Computing (1,265)
    • Computer Engineering (29)
    • Data Transformation (208)
    • Data Visualization (66)
    • information retrieval (2)
    • Information Science (37)
    • Information Technology (88)
    • library science (3)
    • Mathematics (47)
    • technology (9)
    • เทคโนโลยี (194)
  • Design (337)
    • UX/UI (53)
  • engineering (1,293)
    • architecture (1,045)
    • Automotive (59)
    • Bioengineering (34)
    • Building Engineering (135)
    • Civil Engineering (94)
    • Electrical Engineering (10)
    • Mechanical Engineering (3)
    • Safety Engineering (3)
    • transportation (3)
    • urban planning (9)
  • Humanities (1,130)
    • Academia (709)
    • Archaeology (28)
    • art (160)
    • Communication (158)
    • Culture (183)
    • Education (4)
    • history (115)
    • Linguistics (20)
    • Literature (101)
    • media studies (3)
    • military history (3)
    • Religion (4)
    • social science (4)
    • Sociology (2)
    • Spirituality (2)
  • Law (314)
  • Life (2,163)
    • Accessibility (32)
    • Cartoon (146)
    • D.I.Y (78)
    • Health (203)
      • Safety (127)
    • Mindset (311)
    • Movies (200)
    • Philosophy (246)
    • Psychology (1,377)
      • Behavioral Science (1,249)
      • Cognitive Science (501)
    • Sci-Fi (93)
    • Tips and Tricks (60)
    • พุทธ (7)
  • management (875)
    • knowledge management (278)
      • Documentation (81)
    • productivity (5)
    • Project Management (16)
    • strategy (4)
  • Network (437)
    • IOT (27)
    • Nginx (27)
  • Operating Systems (613)
    • Linux (206)
    • macOS (151)
      • Homebrew (25)
    • Shell Script (45)
      • Oh My ZSH (5)
    • Windows (98)
      • PowerShell (25)
  • Programming (2,853)
    • .NET (199)
      • .NET Core EF (12)
      • C# (147)
    • API (838)
      • REST (8)
    • Database (705)
      • MariaDB (34)
      • MySql (66)
      • Oracle Database (24)
      • PostgreSQL (21)
      • RDBMS (55)
      • SQL Server (76)
        • T-SQL (26)
    • Programming Language (7)
    • Programming Languages (391)
      • Java (96)
      • PHP (238)
      • Python (13)
      • Rust (31)
    • Software Architecture (9)
    • Software Engineering (84)
    • System Analyst (SA) (66)
    • system architecture (5)
    • system design (7)
    • System Modeling (3)
    • Systems Programming (2)
    • Testing (140)
      • Automated Testing (127)
    • Web (2,566)
      • Apache HTTP Server (58)
      • Backend (2,363)
        • Laravel (54)
        • Spring Boot (15)
      • Frontend (382)
        • Angular (16)
        • CSS (67)
        • JavaScript (194)
        • jQuery (57)
        • Tabulator (24)
        • Tailwind CSS (4)
        • Vue.js (6)
      • WordPress (40)
  • SecDevOps (1,106)
    • automation (496)
    • CI/CD (349)
    • Docker (119)
    • GIT (56)
    • Hardware (84)
    • SVN (6)
    • system (248)
      • System Administration (37)
  • Security (629)
    • Authentication (248)
    • Cryptography (43)
    • Cybersecurity (349)
  • Uncategorized (1)

Sirat WordPress Theme By VWThemes

Scroll Up
Go to mobile version