A new technology for scenting films is being developed in Russia

Russian company Illusion, supported by the National Technology Initiative (NTI), has developed a technology for watching films and videos with accompaniment of scents. Illusion Aroma technology…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Laravel Rest API Passport Authentication for Ionic App

In this tutorial we will learn to create Authentication System by using Laravel Passport. APIs are great and Laravel already makes it easy to perform Rest API authentication.

APIs typically use tokens to authenticate users and do not maintain session state between requests. Laravel makes API authentication a breeze using Laravel Passport, which provides a full OAuth2 server implementation for your Laravel application in a matter of minutes.

Let’s go ahead and create a brand new Laravel Project first of all. Open your Terminal or Command Prompt and go to the directory where you want to create app. You can use following command to change directory.

Then run the following command to create a new project.

Next go inside the directory by running this command.

Now let’s install Laravel Passport as well by running following command.

The Passport service provider registers its own database migration directory with the framework, so you should migrate your database after installing the package. The Passport migrations will create the tables your application needs to store clients and access tokens.

Now we need to change database variable values in .env file. So update that file with your Database Name, Username, Password etc. So we can run our migrations.

After updating run the following command to migrate database.

Now if you check your database, you will see all these tables.

Perfect!

Next we have to create the encryption keys needed to generate secure access tokens. In addition, the command will create “personal access” and “password grant” clients which will be used to generate access tokens. Run the following command to do that.

After running this command we need to do some modification in some files. So let’s do that.

First of all open App/User.php and add Laravel\Passport\HasApiTokens trait.

Next open app/Providers/AuthServiceProvider.php and call Passport::routes method within the boot method. This method will register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens.

Finally, in your config/auth.php configuration file, you should set the driver option of the api authentication guard to passport. This will instruct your application to use Passport’s TokenGuard when authenticating incoming API requests.

Now let’s set API routes. Normally we create all our routes in routes/web.php but as we are dealing with API so we will create all our routes in routes/api.php.

Copy the following code in routes/api.php. We will create routes for login, register, logout and user.

As we have already created our API routes. Now we need to create AuthController. To do that run the following command.

Then open app/Http/Controllers/Auth/AuthController.php and add following code.

But there are some scenarios where you do want to allow that behaviour. As we need it now so let’s add CORS Middleware. Run the following command to create a new Middleware

A new file will be created, Let’s update app/Http/Middleware/Cors.php

Last step we need to register our new middleware in app/Http/Kernal.php.

Perfect now we are ready. Run the following command to run.

Now I when click on register button. I get message which I previously set as response, if user the is created successfully. If I check my database now I should have new user in my database.

Perfect, As user is in database now, let’s try to login.

As you can see when I tried to login I did got token. I can store this token in local storage of phone or web browser. We will be using this token to get all user data which is protected by API Auth Middleware.

Perfect! Let’s try to get user details now as we are logged in now and we have token.

As you can see under network, we have included token as header.

Perfect! We get response back from back-end.

Add a comment

Related posts:

Beyond Myself

Sometimes I think poetry is. “Beyond Myself” is published by Maci Barnes in DST 3880W / Spring 2019 / Section 2.

Reduce Tension with Progressive Muscle Relaxation

Progressive Muscle Relaxation (PMR) is a relaxing and effective technique for reducing overall body tension as well as psychological stress. This simple technique involves the tensing and relaxing of…