Skip to content

PlusMagi's Blog By Pitt Phunsanit

Plus emotional magic to the knowledge of logic.

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

Laravel: login แบบมีเงื่อนไขLaravel: login แบบมีเงื่อนไข

2013-10-052013-10-05| phunsanitphunsanit| 0 Comment | 07:00

Laravel สร้างระบบ login ได้โดยทำตามคู่มือ Authentication หรือตาม ติดตั้ง laravel ลั๊ลลาเวล และ สร้างระบบ login ใน laravel 5

แต่เมื่อใคร ๆ ก็สามารถที่จะสมัครแล้วใช้งานได้เลย ทำให้ไม่ปลอดภัย จึงควรเพิ่มเงื่อนไขเข้าไป ทำได้โดย

การแก้ไขให้ตรวจสอบเพิ่มในการ login เข้าระบบทำได้โดย

  1. เพิ่ม column active ในตาราง users โดย
    ALTER TABLE `users` ADD `active` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '0 inactive, 1 active' AFTER `id`;
  2. เปิดไฟล์ \app\Http\Controllers\Auth\AuthController.php แก้ตามตัวอย่าง
    ...
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;
    …
    
     /* redirect after login */
     protected $redirectPath = 'home';
    
     /* custom login condition */
     protected function getCredentials(Request $request)
     {
      $crendentials = $request->only($this->loginUsername(), 'password');
      $crendentials['active'] = 1;
      return $crendentials;
     }
    
     /* custom register process */
     public function postRegister(Request $request)
     {
      $validator = $this->validator($request->all());
    
      if ($validator->fails()) {
       $this->throwValidationException(
        $request, $validator
       );
      }
    
      Auth::login($this->create($request->all()));
      Auth::logout();
    
      flash('please contact system administrator for activation.');
    
      return redirect('auth/login');
     }
    ...
    

    และถ้าต้องการให้เปลี่ยนไปหน้าอื่นหลังจาก login ให้แก้

    protected $redirectPath = '/home';

    ไป route ที่ต้องการ

  3. ทดลอง logout แล้วก็ login ใหม่จะมี message These credentials do not match our records. ขึ้นมาทั้ง ๆ ที username และ password ถูกต้อง
  4. ไปแก้ record user ที่ใช้ให้ active = 1 แล้ว login ดูอีกครั้ง จะเข้าระบบได้แล้ว
  5. logout แล้วทดลองสมัครสมาชิกใหม่ จะต้องวิ่งไปในหน้าที่ต้องการโดยไม่ได้ล๊อกอิน
  6. สร้างหน้าจัดการ user โดยเปลี่ยนสถานะ
    0
    login ไม่ได้
    1
    login ได้
Read MoreRead More

Posts pagination

ก่อนหน้า 1 … 650 651 652 … 868 ถัดไป

Projects

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

Recent Posts

  • AI: ประหยัด Token โดยใช้แค่ tab
  • OrbStack: SQL Server on macOS
  • OrbStack: ติดตั้ง
  • VS Code: แทน Azure Data Studio
  • jQuery: import $ ให้เป็น global scope ?

Archives

Categories

  • AI (4)
  • Businesses (1)
  • Design (33)
    • UX/UI (4)
  • Histories (3)
  • Life (44)
    • Books (17)
      • สืบสวน (4)
    • ECO (3)
    • Sci-Fi (2)
    • พุทธ (3)
  • Network (275)
    • Android (11)
      • F-Droid (7)
    • Apache Apache HTTP Server (17)
    • Docker (27)
    • Homebrew (14)
    • IIS (7)
    • IOT (4)
    • Linux (89)
    • macOS (65)
      • OrbStack (10)
    • Nginx (26)
    • RabbitMQ (4)
    • Samba (5)
    • Shell Script (28)
    • SSH (11)
    • Windows (87)
      • PowerShell (18)
      • WSL (21)
  • Programming (610)
    • API (17)
      • REST (5)
      • Swagger (6)
    • C# (15)
      • .NET Core EF (3)
    • CI/CD (3)
    • CSS (16)
      • Bootstrap (3)
      • Tailwind CSS (3)
    • Database (162)
      • DBeaver (3)
      • MariaDB (17)
      • MySql (55)
      • Oracle (4)
        • 10g (2)
      • PostgreSQL (2)
      • SQL Server (81)
        • ADS (3)
        • SMO (4)
        • SSMS (8)
        • T-SQL (25)
    • Flutter (2)
    • GIT (21)
    • 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 (177)
      • CodeIgniter (11)
      • Laravel (36)
        • Laravel 11 (10)
        • Laravel 5 (19)
      • PrestaShop (1)
      • WordPress (18)
      • 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)
  • Programs (73)
    • Bitwarden (4)
    • Excel (8)
    • Figma (7)
    • Google Chrome (6)
    • Mozilla Firefox (9)
    • Oh My ZSH (4)
    • Stalwart (5)
    • USB Boot (7)
  • Q & A (8)
  • Security (26)
  • Tips & Tricks (70)
    • iPhone (6)
      • Jailbreaking (2)
  • กฎหมาย (2)
  • คณิตศาสตร์ (1)

Sirat WordPress Theme By VWThemes

Scroll Up