How To Set Multi Authentication in JWT

How To Set Multi Authentication in JWT

In this post we will give you information about How To Set Multi Authentication in JWT. Hear we will give you detail about How To Set Multi Authentication in JWTAnd how to use it also give you demo for it if it is necessary.

Today, online share with you how to set multi authentication in jwt with simple example. recently we are working with one laravel application and we are also built API for it. and we are required more then one table for authentication. you also see in JWT documentation it by default provide User model for authentication. but sometime we have required use another table also use for authentication in our API. for Ex. one for front API user and another for Admin API user.

See also  Build an Angular 8 CRUD Operation Example


This problem you easyly handle by help of this tutorials. please simple follow this step.


You may also check this link for JWT configure in laravel Restful API In Laravel 5.5 Using jwt Authentication


Suppose we have two table which we are want for authentication with JWT


1. users


2. admins


Step : 1 Add following route in routes/api.php



Route::post('auth/userlogin', 'ApiController@userLogin');
Route::post('auth/adminlogin', 'ApiController@adminLogin');



Step : 2 Create Controller



namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppHttpControllersController;
use AppHttpRequests;
use Config;
use JWTAuth;
use JWTAuthException;
use AppUser;
use AppAdmin;

class ApiController extends Controller
{

    public function __construct()
    {
        $this->user = new User;
        $this->admin = new Admin;
    }
    
    public function userLogin(Request $request){
		Config::set('jwt.user', 'AppUser'); 
		Config::set('auth.providers.users.model', AppUser::class);
		$credentials = $request->only('email', 'password');
		$token = null;
		try {
		    if (!$token = JWTAuth::attempt($credentials)) {
		        return response()->json([
		            'response' => 'error',
		            'message' => 'invalid_email_or_password',
		        ]);
		    }
		} catch (JWTAuthException $e) {
		    return response()->json([
		        'response' => 'error',
		        'message' => 'failed_to_create_token',
		    ]);
		}
		return response()->json([
		    'response' => 'success',
		    'result' => [
		        'token' => $token,
		        'message' => 'I am front user',
		    ],
		]);
    }

    public function adminLogin(Request $request){
		Config::set('jwt.user', 'AppAdmin'); 
		Config::set('auth.providers.users.model', AppAdmin::class);
		$credentials = $request->only('email', 'password');
		$token = null;
		try {
		    if (!$token = JWTAuth::attempt($credentials)) {
		        return response()->json([
		            'response' => 'error',
		            'message' => 'invalid_email_or_password',
		        ]);
		    }
		} catch (JWTAuthException $e) {
		    return response()->json([
		        'response' => 'error',
		        'message' => 'failed_to_create_token',
		    ]);
		}
		return response()->json([
		    'response' => 'success',
		    'result' => [
		        'token' => $token,
		        'message' => 'I am Admin user',
		    ],
		]);
    }
}



See also  Laravel 8 integrate Razorpay Payment Gateway


Step : 9 Test With Postman


You can test your API with postman and another API testing tool


Now we are ready to run our example so run bellow command ro quick run:


php artisan serve


Now you can open bellow URL on your browser:


http://localhost:8000


If you want to any problem then please write comment and also suggest for new topic for make tutorials in future. Thanks…

Hope this and post will helped you for implement How To Set Multi Authentication in JWT. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and Keep reading our blogs

See also  How to install laravel in android phone

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

74  +    =  77

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US