Giter Site home page Giter Site logo

multi-auth-with-api's Introduction

Laravel Logo

About Project

This project make base on simply multiple users login and redirect their own dashboard

  1. Multiple role type users login with single form . image

  2. redirect own dashboard image

  3. Other role type user login

image

image

Now code details

  1. I have created four type role for Admin, Doctor, Intern , Patient. Here role table image

  2. 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);
    }

}

} ####=======================

  1. Also I have crated seprated middleware for admin , doctor, intern and patient image

    Also I have assigned in the karnel.php file the middleware image

    1. Also I have created prefix in route and make route .

    2. I have make controller and function login, register, login for all role type user is comman. image

    3. Also I have make master layout in resource view assigned all type user's dashboard

    Also I have setup API with JWT toke

    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', ];

image

Step 7. Create API Routes

image

Step 8. Create api controller

php artisan make:controller ApiController

Check the code from Apicontroller

Here login and getting token image

Here getting user with jwt token

image

Also Logout with token

image

here Also project some example for multiple users login and redirect their own dashboard - https://youtube.com/watch?v=sYqbaOL6b0A&si=Z5m5x80wFF18yBnE

multi-auth-with-api's People

Contributors

mdmuzaffer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.