- vừa được xem lúc

Sử dụng multiple authentication login trong laravel

0 0 7

Người đăng: Gấu con

Theo Viblo Asia

Hello mọi người, trong bài viết này mình sẽ chia sẻ cách sử dụng multiple auth login sử dụng middleware với một table là users.
Ở ví dụ này mình sẽ demo ở trên laravel 8, các bạn hãy theo dõi các bước bên dưới để hiểu hơn nhé.

Step 1: Cập nhật file Migration và Model user

Trong bước này mình sẽ thêm row "is_admin" vào table users và model
database/migrations/000_create_users_table.php

<?php use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; class CreateUsersTable extends Migration
{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email'); $table->timestamp('email_verified_at')->nullable(); $table->boolean('is_admin')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); }
} 

app/Models/User.php

<?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable; class User extends Authenticatable
{ use HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'is_admin' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ];
}

Tiếp theo chạy migration bằng command bên dưới.

php artisan migrate

Step 2: Create Auth using scaffold

Bước này mình sẽ tạo sẵn một số files cho các màn hình login, register, dashboard bằng command bên dưới.
Laravel 8 UI Package

Laravel 8 UI Package

Generate auth

php artisan ui bootstrap --auth 
npm install
npm run dev

Step 3: Create IsAdmin Middleware

Trong bước này, mình sẽ tạo middleware cho admin, middleware này chỉ cho phép admin user mới có quyền truy cập .

php artisan make:middleware IsAdmin

app/Http/middleware/IsAdmin.php


<?php namespace App\Http\Middleware; use Closure; class IsAdmin
{ /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if(auth()->user()->is_admin == 1){ return $next($request); } return redirect(‘home’)->with(‘error’,"You don't have admin access."); }
}

Thêm đoạn 'is_admin' => \App\Http\Middleware\IsAdmin::class vào file bên dưới.
app/Http/Kernel.php

 protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'is_admin' => \App\Http\Middleware\IsAdmin::class, ];

Step 4: Create Route

Ở bước này mình sẽ tạo một route cho màn hình trang chủ của admin user, normal user.
routes/web.php

<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\HomeController; /*
|--------------------------------------------------------------------------
| 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('admin/home', [HomeController::class, 'adminHome'])->name('admin.home')->middleware('is_admin');
Route::get('home', [HomeController::class, 'index'])->name('home');

Step 5: Add Method on Controller

Bước này mình sẽ thêm 2 methods là index(), adminHome() trong HomeController.
Khi admin user login thành công thì sẽ redirect vào method adminHome().
Khi normal user login thành công thì sẽ redirect vào method index().
Các bạn hãy cập nhật code như bên dưới nhé.
app/Http/Controllers/HomeController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller
{ /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { return view('home'); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function adminHome() { return view('adminHome'); } }

Step 6: Create Blade file

Màn hình này sẽ hiển thị khi normal user login thành công.
resources/views/home.blade.php

@extends('layouts.app') @section('content')
<div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Dashboard</div> <div class="card-body"> You are normal user. </div> </div> </div> </div>
</div>
@endsection

Màn hình này sẽ hiển thị khi admin user login thành công.
resources/views/adminHome.blade.php

@extends('layouts.app') @section('content')
<div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Dashboard</div> <div class="card-body"> You are Admin. </div> </div> </div> </div>
</div>
@endsection

Thêm code bên dưới vào file layout để style cho giao diện nhé.
resources\views\layouts\app.blade.php

 <link href="https://cdn.jsdelivr.net/npm/_@.com/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/_@.com/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>

Step 7: Update on LoginController

Trong method login này, nếu normal user login thành công thì sẽ redirect to home route và nếu admin user login thành công thì redirect về admin route.
Các bạn hãy cập nhật code như bên dưới .
app/Http/Controllers/Auth/LoginController.php

<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request; 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 = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } public function login(Request $request) { $input = $request->all(); $this->validate($request, [ 'email' => 'required|email', 'password' => 'required', ]); if(auth()->attempt(array('email' => $input['email'], 'password' => $input['password']))) { if (auth()->user()->is_admin == 1) { return redirect()->route('admin.home'); }else{ return redirect()->route('home'); } }else{ return redirect()->route('login') ->with('error','Email-Address And Password Are Wrong.'); } }
}

Step 8: Create Seeder

Mình sẽ sử dụng seeder để tạo mới account admin user và normal user như bên dưới.

php artisan make:seeder CreateUsersSeeder

database/seeds/CreateUsersSeeder.php

<?php use Illuminate\Database\Seeder;
use App\Models\User; class CreateUsersSeeder extends Seeder
{ /** * Run the database seeds. * * @return void */ public function run() { $user = [ [ 'name'=>'Admin', 'email'=>'_@.com', 'is_admin'=>'1', 'password'=> bcrypt('123456'), ], [ 'name'=>'User', 'email'=>'_@.com', 'is_admin'=>'0', 'password'=> bcrypt('123456'), ], ]; foreach ($user as $key => $value) { User::create($value); } }
}

Bây giờ chúng ta hãy chạy seeder:

php artisan db:seed --class=CreateUsersSeeder

Cuối cùng mình sẽ chạy chương trình để kiểm tra code của mình đã chạy hay chưa nhé.
Screen Login:

Admin User

Email: _@.com
Password: 123456

Output:

Normal User

Email: _@.com
Password: 123456

Output:

Hy vọng bài viết này giúp ích cho các bạn!

Bình luận

Bài viết tương tự

- vừa được xem lúc

Tìm hiểu về Resource Controller trong Laravel

Giới thiệu. Trong laravel, việc sử dụng các route post, get, group để gọi đến 1 action của Controller đã là quá quen đối với các bạn sử dụng framework này.

0 0 335

- vừa được xem lúc

Phân quyền đơn giản với package Laravel permission

Như các bạn đã biết, phân quyền trong một ứng dụng là một phần không thể thiếu trong việc phát triển phần mềm, dù đó là ứng dụng web hay là mobile. Vậy nên, hôm nay mình sẽ giới thiệu một package có thể giúp các bạn phân quyền nhanh và đơn giản trong một website được viết bằng PHP với framework là L

0 0 421

- vừa được xem lúc

Sử dụng Swagger để xây dựng API documentation

Giới thiệu về Swagger. RESTful API là một tiêu chuẩn dùng trong việc thiết kế API cho các ứng dụng web (thiết kế Web services) để tiện cho việc quản lý các resource.

0 0 1k

- vừa được xem lúc

Ví dụ CRUD với Laravel và Vuejs.

1. Cài đặt Laravel. composer create-project --prefer-dist laravel/laravel vuelaravelcrud. .

0 0 141

- vừa được xem lúc

Một số tips khi dùng laravel (Part 1)

1. Show database query in raw SQL format. DB::enableQueryLog(); // Bật tính năng query logging. DB::table('users')->get(); // Chạy truy vấn bạn muốn ghi log.

0 0 69

- vừa được xem lúc

Inertiajs - Xây dựng Single Page App không cần API

Tiêu đề là mình lấy từ trang chủ của https://inertiajs.com/ chứ không phải mình tự nghĩ ra đâu nhé :v. Lâu lâu rồi chưa động tới Laravel (dự án cuối cùng mình code là ở ver 5.8), thế nên một ngày đẹp trời lượn vào đọc docs ver 8.

0 0 227