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 แบบมีเงื่อนไข

2014-02-082014-02-08| phunsanitphunsanit| 0 Comment | 00: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 … 649 650 651 … 893 ถัดไป

Projects

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

Recent Posts

  • MariaDB: การแปลง Encoding
  • WordPress: แก้ Double / Triple Escaping
  • MariaDB: Character Set และ Collation
  • WinGet: การติดตั้ง TortoiseSVN
  • laravel: breeze, Jetstream, spatie ต่างกันอย่างไร

Archives

Categories

  • AI (4)
  • Businesses (3)
  • Design (30)
    • UX/UI (3)
  • Histories (8)
  • Life (47)
    • Books (20)
      • สืบสวน (4)
    • ECO (3)
    • Sci-Fi (2)
    • พุทธ (3)
  • Network (292)
    • Android (13)
      • F-Droid (7)
    • Apache Apache HTTP Server (17)
    • Docker (28)
    • Homebrew (14)
    • IIS (7)
    • IOT (4)
    • Linux (92)
    • macOS (67)
      • OrbStack (10)
    • Nginx (26)
    • RabbitMQ (4)
    • Samba (5)
    • Shell Script (28)
    • SSH (11)
    • Windows (102)
      • PowerShell (22)
      • WinGet (10)
      • WSL (21)
  • Programming (615)
    • API (18)
      • REST (5)
      • Swagger (6)
    • C# (15)
      • .NET Core EF (3)
    • CI/CD (7)
    • Database (165)
      • DBeaver (3)
      • RDBMS (147)
        • DB2 (2)
        • MariaDB (21)
        • MySql (59)
        • Oracle Database (4)
          • 10g (2)
        • PostgreSQL (3)
        • SQL Server (83)
          • ADS (3)
          • SMO (4)
          • SSMS (8)
          • T-SQL (26)
    • Flutter (2)
    • GIT (23)
    • Java (44)
      • JasperReports (3)
      • Joget (19)
      • Spring Boot (14)
    • Node.js (1)
    • PowerBuilder (11)
      • PowerBuilder 8 (10)
    • Python (8)
    • Rust (1)
    • SVN (1)
    • Testing (7)
      • Automated Testing (4)
        • Playwright (2)
        • Selenium (2)
    • UML (1)
    • Version Control (1)
    • Web (286)
      • CSS (16)
        • Bootstrap (3)
        • Tailwind CSS (3)
      • HTML (23)
      • JavaScript (108)
        • AG Grid (1)
        • Angular (1)
        • jQuery (64)
          • DataTables (14)
        • React (2)
        • Tabulator (11)
        • TypeScript (10)
        • Vue.js (3)
      • PHP (166)
        • CodeIgniter (11)
        • Laravel (33)
          • Laravel 11 (10)
          • Laravel 5 (19)
        • PrestaShop (1)
        • Yii (19)
          • Yii 2 (16)
      • SEO (2)
      • WordPress (22)
  • Programs (82)
    • 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 (33)
  • Tips & Tricks (72)
    • iPhone (7)
      • Jailbreaking (2)
  • กฎหมาย (3)
  • คณิตศาสตร์ (4)

Sirat WordPress Theme By VWThemes

Scroll Up