Hello Dev.
In this article, I would love proportion with you build a multiple guards authentication api in laravel 8. we will create multiple authentication api in laravel 8. this article will provide you multiple authentication guard drivers (including API) in laravel 8. a way to setup multi-auth for laravel 8 apis. we can display how to use multiple authentication guards in a laravel 8 api.
However, Laravel offer easy way to create api. when you have authentication in your mobile app than you could effortlessly do it the usage of passport. Laravel 8 Passport offer manner to create auth token for validating users.
in case you want to have an API running in the same app, and it makes use of JSON web tokens (or a few different stateless, non-session authentication mechanism)? in the past you'll have to jump through lots of hoops to have multiple authentication drivers running at the same time.
so that you also need to create rest api to your mobile software than you may observe this article for the way to use multiple authentication guards in a laravel 8 api. here i am create admin and user guards.
First, you need to download the laravel fresh setup. Use this command then download laravel project setup
composer create-project --prefer-dist laravel/laravel blog
After successfully install laravel 8 Application, Go to your project .env file and set up database credential and move next step
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name
DB_USERNAME=here database username
DB_PASSWORD=here database password
In this step we need to install passport via the Composer package manager, so one your terminal and fire bellow command
composer require laravel/passport
After successfully install package, we require to get default migration for create new passport tables in our database. so let's run bellow command.
php artisan migrate
Next, we need to install passport using command, Using passport:install command, it will create token keys for security. So let's run bellow command.
php artisan passport:install
In this step we need to create admins table and admin model for login into admin So let's open terminal and run bellow migration and model command
php artisan make:model Admin -m
Successfully run above command then after three column in admins table name, email, and password
database/migrations/2021_08_15_053024_create_admins_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAdminsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('admins');
}
}
php artisan migrate
In this step, we have to configuration on three place model, service provider and auth config file. So you have to just following change on that file.
In model we added HasApiTokens class of Passport,
In auth.php, we added api auth configuration.
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;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* 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',
];
}
app/Models/Admin.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
class Admin extends Authenticatable
{
use HasFactory, Notifiable, HasApiTokens;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
}
Now open app/Providers/AuthServiceProvider.php file add route function and then define your roles and descriptions for each role and then specify the default role that would be attached if a role is not explicitly requested for.
app/Providers/AuthServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Passport\Passport;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
// 'App\Models\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Passport::routes();
Passport::tokensCan([
'user' => 'User Type',
'admin' => 'Admin User Type',
]);
}
}
In this step, you can define auth guard and set other the guard and add provider for each role as well. This the driver should be eloquent and the model should be the model of the tables you want each role to authenticate from.
config/auth.php
// Add Guards
'guards' => [
'user' => [
'driver' => 'session',
'provider' => 'users',
],
'user-api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
],
// Add Provider
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
],
laravel provide scope middleware check for all scopes. Now you have to add scope middleware in kernel.php. Let's open kernel.php add bellow middleware
app/Http/Kernel.php
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class,
'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,
];
Now you will create custom route file like admin.php and user.php etc.. let's create api folder in route directory and create two bellow file in api folder.
1. admin.php
2. user.php
routes/api/admin.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LoginController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('admin/login',[LoginController::class, 'adminLogin'])->name('adminLogin');
Route::group( ['prefix' => 'admin','middleware' => ['auth:admin-api','scopes:admin'] ],function(){
// authenticated staff routes here
Route::get('dashboard',[LoginController::class, 'adminDashboard']);
});
routes/api/user.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LoginController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('user/login',[LoginController::class, 'userLogin'])->name('userLogin');
Route::group( ['prefix' => 'user','middleware' => ['auth:user-api','scopes:user'] ],function(){
// authenticated staff routes here
Route::get('dashboard',[LoginController::class, 'userDashboard']);
});
In this step, You can register route file in RouteServiceProvider.php.
app/Http/Controllers/LoginController.php
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api/admin.php'));
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api/user.php'));
});
In this step, You can create new controller as LoginController and put the bellow code.
app/Http/Controllers/LoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Admin;
use Hash;
use Validator;
use Auth;
class LoginController extends Controller
{
public function userDashboard()
{
$users = User::all();
$success = $users;
return response()->json($success, 200);
}
public function adminDashboard()
{
$users = Admin::all();
$success = $users;
return response()->json($success, 200);
}
public function userLogin(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required',
]);
if($validator->fails()){
return response()->json(['error' => $validator->errors()->all()]);
}
if(auth()->guard('user')->attempt(['email' => request('email'), 'password' => request('password')])){
config(['auth.guards.api.provider' => 'user']);
$user = User::select('users.*')->find(auth()->guard('user')->user()->id);
$success = $user;
$success['token'] = $user->createToken('MyApp',['user'])->accessToken;
return response()->json($success, 200);
}else{
return response()->json(['error' => ['Email and Password are Wrong.']], 200);
}
}
public function adminLogin(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required',
]);
if($validator->fails()){
return response()->json(['error' => $validator->errors()->all()]);
}
if(auth()->guard('admin')->attempt(['email' => request('email'), 'password' => request('password')])){
config(['auth.guards.api.provider' => 'admin']);
$admin = Admin::select('admins.*')->find(auth()->guard('admin')->user()->id);
$success = $admin;
$success['token'] = $admin->createToken('MyApp',['admin'])->accessToken;
return response()->json($success, 200);
}else{
return response()->json(['error' => ['Email and Password are Wrong.']], 200);
}
}
}
Now we are ready to run streaming export csv with cursor in laravel so run bellow command for quick run
php artisan serve
Now you can open bellow URL on your postman app
make sure in details api we will use following headers as listed bellow
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$accessToken,
]
i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.