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

2003-08-052003-08-05| 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 … 4,227 4,228 4,229 … 5,603 ถัดไป

Projects

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

Recent Posts

  • Apple แต่งตั้ง จอห์น เทอร์นัส (John Ternus) ขึ้นเป็น CEO โดยไม่ใช่ โจนี ไอฟ์ (Jony Ive)
  • Microsoft Coreutils for Windows: เมื่อคำสั่ง Linux ยอดฮิตพร้อมใช้งานแบบ Native บน Windows
  • Browser and profile synchronization (e.g., Firefox, Chrome)
  • สร้าง grid ใน laravel แบบ advance
  • User Permissions and Administrative Rights (sudo)

Archives

Categories

  • .net core (43)
  • Accounting (1)
  • Action Genre (1)
  • Archaeology (1)
  • art (81)
  • Artificial Intelligence (1)
  • Astronomy (2)
  • Automotive History (1)
  • Bioengineering (1)
  • business (905)
    • Business Analysis (348)
    • Finance (33)
    • Real Estate (27)
  • cd (112)
  • chemistry (1)
  • ci (112)
  • Civil Engineering (2)
  • CMS (30)
  • collaboration (2)
  • Comics (1)
  • Command Line Interface (4)
  • Communication (153)
  • Community Governance (1)
  • Community Management (1)
  • compliance (1)
  • Computer Hardware (59)
  • computer science (1,928)
    • AI (390)
    • Biology (61)
      • Environment (34)
    • Cloud Computing (344)
    • Data Visualization (63)
    • Mathematics (44)
  • container (63)
  • Corporate Governance (3)
  • Corporate Law (1)
  • Cosmology (2)
  • Culture (150)
  • data engineering (1)
  • data management (1)
  • Data Modeling (1)
  • Data Privacy (80)
  • Data Transformation (171)
  • Database Management (2)
  • Design (333)
    • UX/UI (45)
  • development (1)
  • devops (19)
  • Digital Electronics (1)
  • digital marketing (1)
  • e-learning (1)
  • Earth Science (1)
  • Ecology (1)
  • Education (3)
  • Embedded Systems (1)
  • engineering (1,124)
    • architecture (1,004)
    • Building Engineering (99)
  • Entertainment (1)
  • Environmental Science (1)
  • Environmentalism (1)
  • Fantasy Literature (1)
  • Film (2)
  • film studies (2)
  • Forensic Science (1)
  • front-end development (1)
  • Frontend Development (3)
  • Geology (1)
  • Governance (6)
  • Grammar (1)
  • graph theory (1)
  • Hardware (2)
  • Health (159)
    • Safety (127)
  • Health Science (1)
  • Human Resources (1)
  • Humanities (401)
    • Academia (168)
    • history (112)
    • Linguistics (16)
    • Literature (98)
  • information retrieval (2)
  • Information Science (3)
  • International Relations (1)
  • Internet of Things (1)
  • IT Operations (1)
  • Language Arts (1)
  • Law (302)
  • Law and Regulation (1)
  • Legal (1)
  • legal affairs (1)
  • Legal Compliance (3)
  • Legal Framework (1)
  • legal studies (1)
  • legal_affairs (1)
  • library science (1)
  • Life (1,508)
    • Cartoon (41)
    • D.I.Y (62)
    • Mindset (311)
    • Movies (57)
    • Philosophy (229)
    • Psychology (1,079)
      • Behavioral Science (299)
      • Cognitive Science (262)
    • Sci-Fi (65)
    • Tips and Tricks (32)
    • พุทธ (7)
  • logistics (1)
  • management (890)
    • knowledge management (295)
      • Documentation (98)
    • productivity (5)
    • Project Management (13)
    • strategy (3)
  • Marketing (1)
  • Markup Languages (1)
  • Mechanical Engineering (2)
  • Media (1)
  • media studies (3)
  • military history (1)
  • Military Technology (1)
  • Mystery (1)
  • Mythology (1)
  • Network (388)
    • IOT (24)
    • Nginx (26)
  • networking (46)
  • Neuroscience (1)
  • Object-Relational Mapping (1)
  • operating system (16)
  • Operating Systems (574)
    • Linux (207)
    • macOS (141)
      • Homebrew (25)
    • Shell Script (38)
      • Oh My ZSH (5)
    • Windows (99)
      • PowerShell (26)
  • Operations Management (1)
  • physics (1)
  • Pop Culture (1)
  • popular culture (1)
  • Process Management (1)
  • Process Modeling (1)
  • Programming (2,175)
    • .NET (116)
      • .NET Core EF (9)
      • C# (80)
    • API (477)
      • REST (8)
    • Database (699)
      • MariaDB (38)
      • MySql (69)
      • Oracle Database (24)
      • PostgreSQL (14)
      • RDBMS (55)
      • SQL Server (76)
        • T-SQL (26)
    • Programming Languages (368)
      • Java (95)
      • PHP (216)
      • Python (13)
      • Rust (31)
    • Software Architecture (7)
    • Software Engineering (65)
    • System Analyst (SA) (56)
    • Testing (124)
      • Automated Testing (110)
    • Web (1,438)
      • Apache HTTP Server (34)
      • Backend (1,146)
        • Laravel (54)
        • Spring Boot (15)
      • Frontend (309)
        • Angular (11)
        • CSS (67)
        • JavaScript (188)
        • jQuery (57)
        • Tabulator (24)
        • Tailwind CSS (4)
        • Vue.js (6)
      • WordPress (32)
  • Programming Language (5)
  • Property Law (2)
  • Property Management (3)
  • Public Policy (1)
  • Religion (1)
  • risk management (2)
  • Safety Engineering (3)
  • Science (1)
  • Science Fiction (9)
  • SecDevOps (792)
    • automation (255)
    • CI/CD (137)
    • Docker (89)
    • GIT (56)
    • SVN (6)
    • system (232)
      • System Administration (22)
  • Security (468)
    • Authentication (89)
    • Cryptography (42)
    • Cybersecurity (289)
  • Sensory Technology (1)
  • SEO (1)
  • Server Infrastructure (1)
  • Sociology (2)
  • Software Design (1)
  • Software Development (6)
  • software development lifecycle (1)
  • Software Development Practices (1)
  • Software Licensing (2)
  • Software Testing (1)
  • Space Exploration (1)
  • Spirituality (1)
  • sports (1)
  • SQL (1)
  • storage (1)
  • Storage Systems (1)
  • Storytelling (1)
  • structural engineering (1)
  • Supply Chain Management (1)
  • system architecture (1)
  • system design (4)
  • System Modeling (3)
  • Systems Modeling (1)
  • Systems Programming (2)
  • Technical Support (1)
  • technology (3)
  • Technology History (1)
  • transportation (1)
  • UI (1)
  • Uncategorized (665)
  • urban planning (7)
  • user experience (3)
  • User Experience Design (2)
  • User Interface Design (1)
  • UX Design (1)
  • Version Control (3)
  • visualization (1)
  • Web Design (2)
  • web development (55)
  • Web Standards (2)
  • web technology (2)
  • wellness (2)
  • กฎหมาย (3)
  • การขนส่ง (1)
  • การพัฒนาอสังหาริมทรัพย์ (1)
  • จราจร (1)
  • นวนิยายสืบสวน (1)
  • ผังเมือง (1)
  • วรรณกรรม (1)
  • สถาปัตยกรรม (1)
  • อสังหาริมทรัพย์ (2)
  • เรื่องเล่า (1)

Sirat WordPress Theme By VWThemes

Scroll Up
Go to mobile version