-
I have created four type role for Admin, Doctor, Intern , Patient. Here role table
-
Also I have created migrattion and seeder command php artisan make:seeder UserTableSeeder
####===================================================
use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; use App\Models\User; class UserTableSeeder extends Seeder { /** * Run the database seeds. */ public function run(): void {
$users = [
['name' => 'Admin', 'email'=>'[email protected]', 'role'=>'1', 'password' => bcrypt('123456'), 'status' =>'1'],
['name' => 'Doctor', 'email'=>'[email protected]', 'role'=>'2', 'password' => bcrypt('123456'), 'status' =>'1'],
['name' => 'Intern', 'email'=>'[email protected]', 'role'=>'3', 'password' => bcrypt('123456'), 'status' =>'1'],
['name' => 'Patient', 'email'=>'[email protected]', 'role'=>'4', 'password' => bcrypt('123456'), 'status' =>'1'],
];
foreach ($users as $key => $value) {
User::create($value);
}
}
} ####=======================
-
Also I have crated seprated middleware for admin , doctor, intern and patient
Also I have assigned in the karnel.php file the middleware
-
Also I have created prefix in route and make route .
-
I have make controller and function login, register, login for all role type user is comman.
-
Also I have make master layout in resource view assigned all type user's dashboard
Step 1. Install JWT Package
composer require tymon/jwt-auth
Step 2. Add jwt package into a service provider
Open config/app.php file and update the providers and aliases array.
-
'providers' => [
'Tymon\JWTAuth\Providers\LaravelServiceProvider', ], 'aliases' => [
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
], Now , you have successfully added the JWT package into the service provider. Next, we will publish the package config. File.
Step 3. Publish jwt configuration
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider" Then you will see a new file in config/jwt.php In the next step, you need to run a php artisan jwt:secret from the console to generate a secret auth secret.
Step 4. Generate JWT Key
Step 6. Create jwt middleware
use Closure; use JWTAuth; use Exception; use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;
class JwtMiddleware extends BaseMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
try {
$user = JWTAuth::parseToken()->authenticate();
} catch (Exception $e) {
if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException){
return response()->json(['status' => 'Token is Invalid']);
}else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException){
return response()->json(['status' => 'Token is Expired']);
}else{
return response()->json(['status' => 'Authorization Token not found']);
}
}
return $next($request);
}
} To use this middleware register this into Kernel. Open app\Http\Kernel.php
protected $routeMiddleware = [ 'jwt.verify' => \App\Http\Middleware\JwtMiddleware::class, 'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken', 'jwt.refresh' => 'Tymon\JWTAuth\Middleware\RefreshToken', ];
Step 7. Create API Routes
Step 8. Create api controller
php artisan make:controller ApiController
Here getting user with jwt token
Also Logout with token