Laravel DINGO/API Add Jwt-auth Certification

Source: Internet
Author: User
Tags compact

Before we learned Laravel Dingo/api to create a simple API so that the API is open to everyone, how to view and limit the invocation of the API? can be verified with Jwt-auth, JSON Web Token authentication

1, first install the Jwt-auth plug-in, the command line with composer installation

Composer require Tymon/jwt-auth ' 0.5.* '

2, then publish

PHP artisan vendor:publish--provider= "Tymon\jwtauth\providers\jwtauthserviceprovider"

A jwt.php file was generated in/config/

3, Generate key

PHP Artisan Jwt:generate

If the command does not work, you can modify the key that Changeme set for yourself in the/config/jwt.php file

' Secret ' = env (' Jwt_secret ', ' changeme '),

4, modify/app/api/controllers/hellocontroller.php to

<?phpnamespace App\api\controllers;use Illuminate\http\request;use app\http\controllers\controller;// Add Jwt-auth Authentication use Jwtauth;use tymon\jwtauth\exceptions\jwtexception;class Hellocontroller extends Controller{public    function index () {return ' {content:helloworld!} '; }//Add Jwt-auth Certified Public function authenticate (request $request) {//Grab credentials from the request $c        Redentials = $request->only (' email ', ' password '); try {//attempt to verify the credentials and create a tokens for the user if (! $token = jwtauth::a            Ttempt ($credentials)) {return response ()->json ([' error ' = ' = ' invalid_credentials '], 401);            }} catch (Jwtexception $e) {//something went wrong whilst attempting to encode the token        return response ()->json ([' Error ' = ' Could_not_create_token '], 500); }//All good so return the token return response ()->json (the Compact (' tOken ')); }}

5, add route (/routes/web.php)

$api->post (' auth ', ' app\api\controllers\[email protected] ');

6, Test route: PHP artisan api:routes, if the following prompt indicates correct

Access Url:***.com/api/auth display error because no token is added

Re-modify HelloControl and Loutes

<?phpnamespace app\api\controllers;use illuminate\http\request;use app\http\controllers\controller;use JWTAuth; Use Tymon\jwtauth\exceptions\jwtexception;class Hellocontroller extends controller{/** * Create a new Controller I     Nstance.     * * @return void *//** * Show the application dashboard.    * * @return \illuminate\http\response */Public Function index () {return ' {content:helloworld!} ';  The Public function authenticate (Request $request) {//Grab credentials from the Request $credentials =        $request->only (' email ', ' password '); try {//attempt to verify the credentials and create a tokens for the user if (! $token = jwtauth::a            Ttempt ($credentials)) {return response ()->json ([' error ' = ' = ' invalid_credentials '], 401);            }} catch (Jwtexception $e) {//something went wrong whilst attempting to encode the token return response ()->json ([' Error ' = ' Could_not_create_token '], 500);    }//All good so return the token return response ()->json (Compact (' token '));      }//Add user Public Function User () {jwtauth::p arsetoken ();      $user = Jwtauth::p arsetoken ()->authenticate ();    return $user; }}

  

<?phproute::get ('/', function () {    return view (' Welcome ');}); Auth::routes (); Route::get ('/home ', ' [email protected] ')->name (' the "); $api = App (' Dingo\api\routing\router '); $api->version ( ' V1 ', function ($API) {    $api->get (' HelloWorld ', ' app\api\controllers\[email protected] ');  $api->post (' auth ', ' app\api\controllers\[email protected] ');  $api->get (' auth ', ' app\api\controllers\[email protected] ');

Use Google Chrome postman plugin to get token, note is the Post method, as shown in the steps

Copy the token obtained, paste it into the second step of the user authentication token, 5 is the user we just registered

  

Laravel DINGO/API Add Jwt-auth Certification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.