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.


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




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 

Change default configuration name of Laravel's created_at and updated_at

In this post we will give you information about Change default configuration name of Laravel's created_at and updated_at. Hear we will give you detail about Change default configuration name of Laravel's created_at and updated_atAnd how to use it also give you demo for it if it is necessary.

In this Laravel PHP Tutorial, I will let you know the use of created_at and updated_at column in a database table.

By default, Eloquent automatically maintain the date time value in created_at and updated_at column on your database table. If you do not want for eloquent to maintain created_at and updated_at columns then disable it by adding following property in your model class :

  1. class Member extends Eloquent {
  2. protected $table='members';
  3. public $timestamps= false;
  4. }
class Member extends Eloquent {

 protected $table = 'members';

 public $timestamps = false;

}

If you want to map Laravel's timestamp from created_at to created_on and updated_at to modified_on then you can override const on your model in following way :

const CREATED_AT = 'created_on';
const UPDATED_AT = 'modified_on';

Now Eloquent will take care of the column "created_on" and "modified_on" on your database table.

How to disable created_at and updated_at timestamps in Laravel Model?

Try this..

Hope this code and post will helped you for implement Change default configuration name of Laravel's created_at and updated_at. 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 code Keep reading our blogs

For More Info See :: laravel And github

For More Info See :: laravel And github

Leave a Comment

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

  +  53  =  61

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