PlusMagi's Blog By Pitt Phunsanit Backend,Cybersecurity Laravel: authenticate users without Database

Laravel: authenticate users without Database

🚀 Laravel Authentication แบบไม่ต้องใช้ Database (No DB Auth)

ในโลกของการพัฒนาเว็บแอปพลิเคชัน การตรวจสอบสิทธิ์ (Authentication) มักจะต้องพึ่งพาฐานข้อมูล (Database) เพื่อเก็บข้อมูลผู้ใช้ (Username, Password) อย่างไรก็ตาม ในบางกรณี เช่น ระบบ Prototype, ระบบ PIN-based Access, หรือเมื่อต้องการจำลองการเข้าถึงที่มาจาก API ภายนอกที่จัดการ User Data ด้วยระบบอื่น (เช่น PostgreSQL Backend) การเขียนระบบ Login ที่ไม่ต้องมีการ Query ฐานข้อมูลเลยก็เป็นทางเลือกที่รวดเร็วและมีประสิทธิภาพ

บทความนี้จะแนะนำวิธีการสร้างระบบ Login แบบง่ายๆ ใน Laravel โดยการใช้ Environment Variables (.env) เพื่อเก็บข้อมูล Credentials แทนการใช้ตาราง `users` ในฐานข้อมูล PostgreSQL ของคุณ

⚙️ ขั้นตอนที่ 1: การตั้งค่า Environment Variables

ก่อนอื่น ให้กำหนดค่า Username และ Password สำหรับการทดสอบในไฟล์ .env ของโปรเจกต์ของคุณ:

Plain Text

# .env file

# Credentials สำหรับการ Login แบบจำลอง
LOGIN_USERNAME=pitt
LOGIN_PASSWORD=phunsanit

💡 ขั้นตอนที่ 2: ปรับปรุง LoginController

เราจะปรับปรุง LoginController เพื่อให้ทำการตรวจสอบข้อมูลที่ส่งมา (Request) เทียบกับค่าที่เก็บใน Environment Variables แทนการใช้ Auth::attempt() ซึ่งจะทำการ Query ฐานข้อมูล

คุณต้องแก้ไขเมธอด login(Request $request) ดังนี้:

PHP

namespace App\Http\Controllers\Auth;

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

class LoginController extends Controller
{
    use AuthenticatesUsers;

    // ... (ส่วนอื่นๆ ของ Controller)

    /**
     * Handle a login request to the application.
     */
    public function login(Request $request)
    {
        // 1. ตรวจสอบ Username และ Password จาก Request
        $inputUsername = $request->input('username');
        $inputPassword = $request->input('password');

        // 2. ตรวจสอบเทียบกับค่าใน .env
        $isLoginSuccess = ($inputUsername === env('LOGIN_USERNAME') && $inputPassword === env('LOGIN_PASSWORD'));

        if ($isLoginSuccess) {
            // 3. หากสำเร็จ ให้บันทึก Session เพื่อยืนยันตัวตน
            $request->session()->put('authenticated', time());
            return redirect()->intended('home');
        }

        // 4. หากไม่สำเร็จ ให้แสดงข้อความผิดพลาด
        return view('auth.login', ['message' => 'Username หรือ PIN ไม่ถูกต้อง']);
    }
}

🛡️ ขั้นตอนที่ 3: สร้าง Middleware เพื่อตรวจสอบ Session

เพื่อป้องกันไม่ให้ผู้ใช้ที่ไม่ Login เข้าถึงหน้าหลักได้ คุณต้องสร้าง Middleware ที่ตรวจสอบว่า Session authenticated มีอยู่หรือไม่ ก่อนจะอนุญาตให้เข้าถึงเส้นทางที่สำคัญ (Protected Routes)

PHP

// app/Http/Middleware/AuthenticateUser.php

use Closure;
use Illuminate\Http\Request;

class AuthenticateUser
{
    public function handle(Request $request, Closure $next)
    {
        // ตรวจสอบว่า Session 'authenticated' ถูกตั้งค่าหรือไม่
        if (!session()->has('authenticated')) {
            // ถ้าไม่มี ให้ Redirect ไปหน้า Login
            return redirect('/login')->with('error', 'กรุณาเข้าสู่ระบบเพื่อใช้งาน');
        }

        return $next($request);
    }
}

อย่าลืมลงทะเบียน Middleware นี้ใน Kernel.php และนำไปใช้กับกลุ่ม Route ที่ต้องการความปลอดภัย

✅ สรุปและข้อควรระวัง

เทคนิคการ Login แบบใช้ Environment Variables นี้เหมาะสำหรับสถานการณ์ที่ต้องการความรวดเร็วในการพัฒนา หรือใช้เป็นระบบจำลองการเข้าถึง (Mock Authentication) ที่ไม่ต้องการความซับซ้อนของฐานข้อมูล PostgreSQL จริงๆ

อย่างไรก็ตาม สำหรับระบบ Production ที่ต้องการความปลอดภัยสูง ควรใช้ระบบ Authentication ที่มีการ Hash รหัสผ่าน (Password Hashing) และใช้ฐานข้อมูลที่เชื่อถือได้เสมอ การใช้ Environment Variables ควรจำกัดเฉพาะข้อมูลที่ไม่ละเอียดอ่อนเท่านั้น