<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Varun Verma</title>
    <description>The latest articles on DEV Community by Varun Verma (@flicher).</description>
    <link>https://dev.to/flicher</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F131800%2Fdc033834-3c20-4dd5-bd72-c5e9f27a3dd0.jpg</url>
      <title>DEV Community: Varun Verma</title>
      <link>https://dev.to/flicher</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/flicher"/>
    <language>en</language>
    <item>
      <title>Laravel Rest API Passport Authentication for Ionic App</title>
      <dc:creator>Varun Verma</dc:creator>
      <pubDate>Tue, 12 Feb 2019 18:45:46 +0000</pubDate>
      <link>https://dev.to/flicher/laravel-rest-api-passport-authentication-for-ionic-app-3370</link>
      <guid>https://dev.to/flicher/laravel-rest-api-passport-authentication-for-ionic-app-3370</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-auth-1280x640.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-auth-1280x640.png" alt="Laravel Rest API Passport Authentication for Ionic App"&gt;&lt;/a&gt;                  &lt;/p&gt;

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;For testing of this project. We will also create a mobile app using &lt;a href="https://ionicframework.com/" rel="noopener noreferrer"&gt;Ionic 4&lt;/a&gt;. But you can also use &lt;a href="https://www.getpostman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt; to test your APIs. We will be using a mobile app so we test the real world environment. &lt;/p&gt;

&lt;p&gt;If you are also interested in creating mobile app of your project, see my other post &lt;a href="https://blog.flicher.net/setting-up-ionic-4-on-mac/" rel="noopener noreferrer"&gt;Setting up Ionic&lt;/a&gt; to get started.&lt;/p&gt;

&lt;p&gt;This project files are available on GitHub at &lt;a href="https://github.com/flicher-net/laravel-auth-passport" rel="noopener noreferrer"&gt;https://github.com/flicher-net/laravel-auth-passport&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Step 1 — Getting Started&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd Desktop/&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then run the following command to create a new project.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;composer create-project --prefer-dist laravel/laravel auth-app&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next go inside the directory by running this command.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd auth-app/&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Step 2 — Installing Laravel Passport&lt;/h2&gt;

&lt;p&gt;Now let’s install Laravel Passport as well by running following command.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;composer require laravel/passport&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Step 3 — Migrate Database&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;As laravel comes with &lt;strong&gt;users&lt;/strong&gt; migrations by default, which is located at &lt;strong&gt;database/migrations/create_users_table.php&lt;/strong&gt;. You can also update it according to your requirement. I will just add two new columns instead of &lt;strong&gt;name&lt;/strong&gt; column.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table-&amp;gt;increments('id');
            $table-&amp;gt;string('first_name');
            $table-&amp;gt;string('last_name');
            $table-&amp;gt;string('email')-&amp;gt;unique();
            $table-&amp;gt;timestamp('email_verified_at')-&amp;gt;nullable();
            $table-&amp;gt;string('password');
            $table-&amp;gt;rememberToken();
            $table-&amp;gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}&lt;/pre&gt;



&lt;p&gt;Now we need to change database variable values in &lt;strong&gt;.env&lt;/strong&gt; file. So update that file with your Database Name, Username, Password etc. So we can run our migrations.&lt;/p&gt;



&lt;pre&gt;APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:OZakMJg2LBGFcM5Da/XtArMHSEzWn7Jda+WUKW+Kwec=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=flicher
DB_USERNAME=flicheruser
DB_PASSWORD=super-secret-password

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"&lt;/pre&gt;



&lt;p&gt;After updating run the following command to migrate database.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;php artisan migrate&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now if you check your database, you will see all these tables.&lt;/p&gt;



&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-auth-database-1024x287.png" alt="laravel-auth-database"&gt;



&lt;p&gt;Perfect!&lt;/p&gt;



&lt;h2&gt;Step 4 — Generate Keys&lt;/h2&gt;



&lt;p&gt;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.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;php artisan passport:install&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;Step 5 — Configure&lt;/h2&gt;



&lt;p&gt;After running this command we need to do some modification in some files. So let’s do that. &lt;/p&gt;



&lt;p&gt;First of all open &lt;strong&gt;App/User.php&lt;/strong&gt; and add &lt;strong&gt;Laravel\Passport\HasApiTokens&lt;/strong&gt; trait.&lt;/p&gt;



&lt;pre&gt;&amp;lt;?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * &lt;a class="mentioned-user" href="https://dev.to/var"&gt;@var&lt;/a&gt; array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * &lt;a class="mentioned-user" href="https://dev.to/var"&gt;@var&lt;/a&gt; array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}&lt;/pre&gt;



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



&lt;pre&gt;&amp;lt;?php

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * &lt;a class="mentioned-user" href="https://dev.to/var"&gt;@var&lt;/a&gt; array
     */
    protected $policies = [
        'App\Model' =&amp;gt; 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this-&amp;gt;registerPolicies();

        Passport::routes();
    }
}
&lt;/pre&gt;

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

&lt;pre&gt;&amp;lt;?php

return [

    'defaults' =&amp;gt; [
        'guard' =&amp;gt; 'web',
        'passwords' =&amp;gt; 'users',
    ],

    'guards' =&amp;gt; [
        'web' =&amp;gt; [
            'driver' =&amp;gt; 'session',
            'provider' =&amp;gt; 'users',
        ],

        'api' =&amp;gt; [
            'driver' =&amp;gt; 'passport',
            'provider' =&amp;gt; 'users',
        ],
    ],


    'providers' =&amp;gt; [
        'users' =&amp;gt; [
            'driver' =&amp;gt; 'eloquent',
            'model' =&amp;gt; App\User::class,
        ],

        // 'users' =&amp;gt; [
        //     'driver' =&amp;gt; 'database',
        //     'table' =&amp;gt; 'users',
        // ],
    ],

    'passwords' =&amp;gt; [
        'users' =&amp;gt; [
            'provider' =&amp;gt; 'users',
            'table' =&amp;gt; 'password_resets',
            'expire' =&amp;gt; 60,
        ],
    ],

];&lt;/pre&gt;



&lt;h2&gt;Step 6 — Set API routes&lt;/h2&gt;



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



&lt;p&gt;Copy the following code in &lt;strong&gt;routes/api.php&lt;/strong&gt;. We will create routes for login, register, logout and user.&lt;/p&gt;



&lt;pre&gt;&amp;lt;?php

use Illuminate\Http\Request;

Route::group([
    'prefix' =&amp;gt; 'auth'
], function () {
    Route::post('login', 'Auth\AuthController@login')-&amp;gt;name('login');
    Route::post('register', 'Auth\AuthController@register');
    Route::group([
      'middleware' =&amp;gt; 'auth:api'
    ], function() {
        Route::get('logout', 'Auth\AuthController@logout');
        Route::get('user', 'Auth\AuthController@user');
    });
});
    &lt;/pre&gt;



&lt;h2&gt;Step 7 — Create Controller&lt;/h2&gt;



&lt;p&gt;As we have already created our API routes. Now we need to create &lt;strong&gt;AuthController&lt;/strong&gt;. To do that run the following command.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;php artisan make:controller Auth/AuthController&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Then open &lt;strong&gt;app/Http/Controllers/Auth/AuthController.php&lt;/strong&gt; and add following code.&lt;/p&gt;



&lt;pre&gt;&amp;lt;?php

namespace App\Http\Controllers\Auth;

use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    public function login(Request $request) {
        $request-&amp;gt;validate([
            'email' =&amp;gt; 'required|string|email',
            'password' =&amp;gt; 'required|string',
            //'remember_me' =&amp;gt; 'boolean'
        ]);
        $credentials = request(['email', 'password']);
        if(!Auth::attempt($credentials))
            return response()-&amp;gt;json([
                'message' =&amp;gt; 'Unauthorized'
            ], 401);
        $user = $request-&amp;gt;user();
        $tokenResult = $user-&amp;gt;createToken('Personal Access Token');
        $token = $tokenResult-&amp;gt;token;
        if ($request-&amp;gt;remember_me)
            $token-&amp;gt;expires_at = Carbon::now()-&amp;gt;addWeeks(1);
        $token-&amp;gt;save();
        return response()-&amp;gt;json([
            'access_token' =&amp;gt; $tokenResult-&amp;gt;accessToken,
            'token_type' =&amp;gt; 'Bearer',
            'expires_at' =&amp;gt; Carbon::parse(
                $tokenResult-&amp;gt;token-&amp;gt;expires_at
            )-&amp;gt;toDateTimeString()
        ]);
    }

    public function register(Request $request)
    {
        $request-&amp;gt;validate([
            'fName' =&amp;gt; 'required|string',
            'lName' =&amp;gt; 'required|string',
            'email' =&amp;gt; 'required|string|email|unique:users',
            'password' =&amp;gt; 'required|string'
        ]);

        $user = new User;
        $user-&amp;gt;first_name = $request-&amp;gt;fName;
        $user-&amp;gt;last_name = $request-&amp;gt;lName;
        $user-&amp;gt;email = $request-&amp;gt;email;
        $user-&amp;gt;password = bcrypt($request-&amp;gt;password);

        $user-&amp;gt;save();

        return response()-&amp;gt;json([
            'message' =&amp;gt; 'Successfully created user!'
        ], 201);
    }

    public function logout(Request $request)
    {
        $request-&amp;gt;user()-&amp;gt;token()-&amp;gt;revoke();
        return response()-&amp;gt;json([
            'message' =&amp;gt; 'Successfully logged out'
        ]);
    }
  
    /**
     * Get the authenticated User
     *
     * @return [json] user object
     */
    public function user(Request $request)
    {
        return response()-&amp;gt;json($request-&amp;gt;user());
    }
}
&lt;/pre&gt;

&lt;h2&gt;Step 8 — Adding CORS Middleware&lt;/h2&gt;

&lt;p&gt;Imagine that all JavaScript code for domain X running in a browser would be able to make http requests to an domain Y. Malious code on domain X would be able to interact with site Y without you knowing. In most circumstances you don’t want this. Luckily all major browsers only allow sites to make requests against their own domain. They don’t allow JavaScript code to make request against a sites on different domains. This is called &lt;a href="https://www.w3.org/Security/wiki/Same_Origin_Policy" rel="noopener noreferrer"&gt;the same-origin policy&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;php artisan make:middleware Cors&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A new file will be created, Let’s update &lt;strong&gt;app/Http/Middleware/Cors.php&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php

namespace App\Http\Middleware;

use Closure;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * &lt;a class="mentioned-user" href="https://dev.to/param"&gt;@param&lt;/a&gt;  \Illuminate\Http\Request  $request
     * &lt;a class="mentioned-user" href="https://dev.to/param"&gt;@param&lt;/a&gt;  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request)
            -&amp;gt;header('Access-Control-Allow-Origin', '*')
            -&amp;gt;header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
            -&amp;gt;header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, X-XSRF-TOKEN');
    }
}
&lt;/pre&gt;

&lt;p&gt;Last step we need to register our new middleware in &lt;strong&gt;app/Http/Kernal.php&lt;/strong&gt;.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * &lt;a class="mentioned-user" href="https://dev.to/var"&gt;@var&lt;/a&gt; array
     */
    protected $middleware = [
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\Cors::class,
    ];

...&lt;/pre&gt;

&lt;p&gt;Perfect now we are ready. Run the following command to run.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;php artisan serve&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Tests&lt;/h2&gt;

&lt;p&gt;As I said before I will be testing this API with a mobile app which I built using Ionic Framework. Now if you want to see how I built that app visit &lt;a href="https://blog.flicher.net/ionic-4-user-registration-login-tutorial/" rel="noopener noreferrer"&gt;Ionic 4 User Registration &amp;amp; Login Tutorial&lt;/a&gt; for full tutorial.&lt;/p&gt;

&lt;h3&gt;Register&lt;/h3&gt;

&lt;p&gt;So I am gonna send POST request to &lt;strong&gt;http://localhost:8000/api/auth/register&lt;/strong&gt;. I will be sending First Name, Last Name, Email and Password in POST request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-register-1024x464.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-register-1024x464.png" alt="laravel-ionic-register"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-auth-users-1024x128.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-auth-users-1024x128.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Perfect, As user is in database now, let’s try to login.&lt;/p&gt;

&lt;h3&gt;Login&lt;/h3&gt;

&lt;p&gt;For login we need to send POST request to &lt;strong&gt;http://localhost:8000/api/auth/login&lt;/strong&gt; and we need to send email and password in request so we can get a Bearer Token as response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-login-1024x464.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-login-1024x464.png" alt="laravel-ionic-login"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Now this token is also stored in &lt;strong&gt;oauth_access_tokens&lt;/strong&gt; table. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-auth-token-1024x131.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-auth-token-1024x131.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Perfect! Let’s try to get user details now as we are logged in now and we have token.&lt;/p&gt;

&lt;h3&gt;User&lt;/h3&gt;

&lt;p&gt;We will be sending GET request to &lt;strong&gt;http://localhost/api/auth/user&lt;/strong&gt; and we need to send token as Authorization Header.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-user-1024x461.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-user-1024x461.png" alt="laravel-ionic-user"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see when I send GET request to &lt;strong&gt;http://localhost/api/auth/user&lt;/strong&gt; with Authorization Header as token which I got earlier. I am getting user details from database. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-headers-1024x461.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-headers-1024x461.png" alt="laravel-ionic-headers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see under network, we have included token as header.&lt;/p&gt;

&lt;h3&gt;Logout&lt;/h3&gt;

&lt;p&gt;Last thing but not least. Let’s try to test logout as well. So I will click on logout button from menu, which will send GET request to &lt;strong&gt;http://localhost/api/auth/logout&lt;/strong&gt; and I also need to send send token as Authorization Header again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-logout-1024x461.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F02%2Flaravel-ionic-logout-1024x461.png" alt="laravel-ionic-logout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Perfect! We get response back from back-end.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Congratulation! You have successfully added API Authentication in your Laravel Application. If you have any questions ping me &lt;a href="https://twitter.com/_varunverma" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; on leave a comment below.&lt;/p&gt;

&lt;h2&gt;Resources&lt;/h2&gt;

&lt;p&gt;Original Article is posted at &lt;a href="https://blog.flicher.net" rel="noopener noreferrer"&gt;Flicher Blog&lt;/a&gt;


&lt;/p&gt;
&lt;p&gt;&lt;a href="https://laravel.com/docs/5.7/passport" rel="noopener noreferrer"&gt;Laravel Framework Passport docs&lt;/a&gt;&lt;br&gt;&lt;a href="https://ionicframework.com/docs/" rel="noopener noreferrer"&gt;Ionic Framework docs&lt;/a&gt;&lt;br&gt;&lt;a href="https://blog.flicher.net/setting-up-ionic-4-on-mac/" rel="noopener noreferrer"&gt;Setting up Ionic Tutorial&lt;/a&gt;&lt;br&gt;&lt;a href="https://blog.flicher.net/ionic-4-user-registration-login-tutorial/" rel="noopener noreferrer"&gt;Ionic 4 User Registration &amp;amp; Login Tutorial&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ionic</category>
      <category>laravel</category>
      <category>authentication</category>
      <category>oauth</category>
    </item>
    <item>
      <title>Setting up Ionic 4 on MAC</title>
      <dc:creator>Varun Verma</dc:creator>
      <pubDate>Thu, 31 Jan 2019 11:02:43 +0000</pubDate>
      <link>https://dev.to/flicher/setting-up-ionic-4-on-mac-45jc</link>
      <guid>https://dev.to/flicher/setting-up-ionic-4-on-mac-45jc</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fionic-setup-1280x640.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fionic-setup-1280x640.png" alt="Setting up Ionic 4 on MAC"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Introduction&lt;/h3&gt;

&lt;p&gt;Ionic 4 is finally here! So in this post we will discuss what is the best way to install, run, and emulate Ionic 4 app. We will also see how to test our app while building. Now this post is for Mac users only.&lt;/p&gt;

&lt;h3&gt;Prerequisites&lt;/h3&gt;

&lt;p&gt;Make sure you have an up-to-date version of Node.js installed on your system. If you don’t have Node.js installed, you can install it from &lt;a href="http://nodejs.org/" rel="noreferrer noopener"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also we need Ionic and Cordova, So open a terminal window (Mac) and install Cordova and Ionic:&lt;/p&gt;

&lt;pre&gt;sudo npm install -g cordova ionic&lt;/pre&gt;

&lt;p&gt;If you already have Cordova and Ionic installed on your computer, make sure you update to the latest version:&lt;/p&gt;

&lt;pre&gt;sudo npm update -g cordova ionic&lt;/pre&gt;

&lt;p&gt;We also need an IDE or Text Editor. Personally I use &lt;a rel="noreferrer noopener" href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt;. But you can use any.&lt;/p&gt;

&lt;p&gt;We also need &lt;a rel="noreferrer noopener" href="http://xcode/"&gt;Xcode&lt;/a&gt; to simulate devices.&lt;/p&gt;

&lt;h3 id="example-configuration"&gt;Example Configuration&lt;/h3&gt;

&lt;p&gt;For demonstration purposes, we’re going to create a simple app&lt;/p&gt;

&lt;h3 id="mce_28"&gt;Step One — Create a new Ionic Application&lt;/h3&gt;

&lt;p&gt;First of all open Terminal and go to a directory where you want to create app. You can use following command to go to different directory.&lt;/p&gt;

&lt;pre&gt;cd Desktop/&lt;/pre&gt;

&lt;p&gt;To create a new Ionic 4 application, paste this command in your terminal. There are 3 basic layouts we can use when we are creating a new Ionic 4 app. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blank&lt;/li&gt;
&lt;li&gt;Sidemenu&lt;/li&gt;
&lt;li&gt;Tabs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this tutorial we will be using Tabs.&lt;/p&gt;

&lt;pre&gt;ionic start appName tabs --type=angular&lt;/pre&gt;

&lt;p&gt;After installing `NPM` modules and dependencies, you will see this question, just type `N` because we are not using it yet.&lt;/p&gt;

&lt;pre&gt;Install the free Ionic Appflow SDK and connect your app? (Y/n) : N&lt;/pre&gt;

&lt;p&gt;Next, go to the newly created app folder.&lt;/p&gt;

&lt;pre&gt;cd appName/&lt;/pre&gt;

&lt;h3&gt;Step Two — Running App&lt;/h3&gt;

&lt;p&gt;Now we can use following command to run the app in browser.&lt;/p&gt;

&lt;pre&gt;ionic serve -l&lt;/pre&gt;

&lt;p&gt;You will see this question, just type `Y` and press enter.&lt;/p&gt;

&lt;pre&gt;Install @ionic/lab? (Y/n) : Y&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fionic-serve-1024x514.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fionic-serve-1024x514.png" alt=""&gt;&lt;/a&gt;After that, a new window will open in your default browser, which looks like this&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Now we have successfully created an Ionic 4 app and we even tested it by running it on web browser. Perfect!!!&lt;/p&gt;

&lt;p&gt;Is that it?&lt;/p&gt;

&lt;p&gt;May be there is one more thing we need to do, Now if you open Inspect Element of this page, by right clicking on app, and then click on Inspect&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Finspect-element-1-1024x564.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Finspect-element-1-1024x564.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After clicking on Inspect, you will see a new window, Now I want you to click on Console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Finspect-1024x564.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Finspect-1024x564.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will see something like following image. Now what the Heck are these errors?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fconsole-1024x564.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fconsole-1024x564.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So basically StatusBar is a Native Cordova plugin and as we are running our app on web browser, so Native Cordova plugins won’t work like Camera, Push Notifications etc.&lt;/p&gt;

&lt;p&gt;Now you can use &lt;a rel="noreferrer noopener" href="https://ionicframework.com/docs/appflow/devapp/"&gt;Ionic DevApp&lt;/a&gt;. You can just install it on your iOS or Android device and run app there. The native Cordova plugins will work just fine there.&lt;/p&gt;

&lt;p&gt;But while building the app. we will need see console to see all errors. But if we are using &lt;a rel="noreferrer noopener" href="https://ionicframework.com/docs/appflow/devapp/"&gt;Ionic DevApp&lt;/a&gt;, we can’t see console.&lt;/p&gt;

&lt;p&gt;So what’s the solution?&lt;/p&gt;

&lt;p&gt;So we need to run this app in actual iPhone and use Safari Web Inspector to see console. So what if we don’t have physical device? That’s ok as well. We can run a simulator for iPhone, which will work just like as physical device.&lt;/p&gt;

&lt;h3&gt;Step Three — Enable Cordova&lt;/h3&gt;

&lt;p&gt;Run following command to get all Cordova files.&lt;/p&gt;

&lt;pre&gt;ionic integrations enable cordova&lt;/pre&gt;

&lt;p&gt;This command is going to create some new files. After that we need to change package id in &lt;strong&gt;config.xml&lt;/strong&gt; file. You have to change the &lt;strong&gt;id&lt;/strong&gt; attribute of &lt;strong&gt;widget tag&lt;/strong&gt; to change your app id.&lt;/p&gt;

&lt;p&gt;For Example:&lt;/p&gt;

&lt;p&gt;So for my project I am going to change &lt;strong&gt;io.ionic.starter&lt;/strong&gt; to &lt;strong&gt;net.flicher&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&amp;lt;widget id=”&lt;strong&gt;io.ionic.starter&lt;/strong&gt;” version=”0.0.1″ xmlns=”http://www.w3.org/ns/widgets” xmlns:cdv=”http://cordova.apache.org/ns/1.0″&amp;gt;&lt;/pre&gt;

&lt;pre&gt;&amp;lt;widget id=”&lt;strong&gt;net.flicher&lt;/strong&gt;” version=”0.0.1″ xmlns=”http://www.w3.org/ns/widgets” xmlns:cdv=”http://cordova.apache.org/ns/1.0″&amp;gt;&lt;/pre&gt;

&lt;h3 id="mce_17"&gt;Step Four — Running app on Simulator&lt;/h3&gt;

&lt;p&gt;To run Simulator run following command, as mentioned in &lt;a href="https://ionicframework.com/docs/installation/ios" rel="noreferrer noopener"&gt;Ionic documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Xcode 10 support in Cordova is still a work-in-progress. So they recommend using XCode 9. So either you can install XCode 9 or use following command to get it working with XCode 10. See &lt;a rel="noreferrer noopener" href="https://github.com/apache/cordova-ios/issues/407"&gt;this issue&lt;/a&gt; for details.&lt;/p&gt;

&lt;pre&gt;ionic cordova emulate ios -lc -- --buildFlag="-UseModernBuildSystem=0"&lt;br&gt;&lt;/pre&gt;

&lt;p&gt;Lets understand flags used in this command&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;-l:&lt;/strong&gt; Live Reload&lt;br&gt;&lt;strong&gt;-c:&lt;/strong&gt; Console Errors in terminal&lt;br&gt;&lt;code&gt;&lt;strong&gt;--&lt;/strong&gt;&lt;/code&gt;&lt;strong&gt; &lt;/strong&gt;&lt;code&gt;&lt;strong&gt;--&lt;/strong&gt;&lt;/code&gt;&lt;strong&gt;buildFlag=”-UseModernBuildSystem=0″:&lt;/strong&gt; Use XCode old build system&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;After running this command, Simulator will open and app will be installed on it. It will look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fsimulator-1024x594.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fsimulator-1024x594.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id="mce_7"&gt;Step Five — Opening Safari Web Inspector&lt;/h3&gt;

&lt;p&gt;Safari has Web Inspector support for iOS simulators and devices. Open the Develop menu and select the simulator or device, then select the Ionic App to open Web Inspector.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the &lt;strong&gt;Develop&lt;/strong&gt; menu is hidden, enable it in &lt;strong&gt;Safari&lt;/strong&gt; » &lt;strong&gt;Preferences&lt;/strong&gt; » &lt;strong&gt;Advanced&lt;/strong&gt; » &lt;strong&gt;Show Develop menu in menu bar&lt;/strong&gt;.&lt;/p&gt;

&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fsimulator-develop-1024x609.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fsimulator-develop-1024x609.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After you click on Simulator, a new windows will open which will look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fsimulator-open-develop-1024x609.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.flicher.net%2Fwp-content%2Fuploads%2F2019%2F01%2Fsimulator-open-develop-1024x609.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can refresh the app once by pressing Command (⌘)-R on Web Inspector to see console logs. As Live reload is enabled, so if you make modification to your project. The Ionic App will reload it self, when you save the file and you will see the change you made in Simulator.&lt;/p&gt;

&lt;h3 id="conclusion"&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Congratulation, you have successfully created your first Ionic 4 app and ran it on Simulator as well. &lt;/p&gt;

&lt;p&gt;What’s next?&lt;/p&gt;

&lt;p&gt;Go to &lt;a rel="noreferrer noopener" href="https://ionicframework.com/docs/"&gt;Ionic Documentation&lt;/a&gt; and add new features to your app.&lt;/p&gt;

&lt;p&gt;Have fun!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/_varunverma" rel="noopener noreferrer"&gt;Follow me on Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.instagram.com/_varunverma/" rel="noopener noreferrer"&gt;Follow me on Instagram&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.flicher.net/" rel="noopener noreferrer"&gt;My Blog Flicher&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ionic</category>
      <category>ionic4</category>
      <category>ionicframework</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
