The way using an external Authorization on Laravel and use Laravel User Auth as user filter.

[NOTE] I'd say this way may not correct. However if you have same trouble in a same situation.
I hope this article might be helpful for you.

## Overall

I'd love to use an external authorization which is an API we've already have, however User Auth on Laravel is awesome. Everybody know it.
We must not reinventing the wheel.

So I've summarized that I use external authorization at first, then use User Auth which Laravel has as user filter.

## Environment
php 7.1.27
Laravel 5.8.12

## Just in case

If you are running MySQL under v5.7.7, add following code to AppServiceProvider.php


```php:app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Schema;

...

function boot()
{
    Schema::defaultStringLength(191);
}
```

## Preparation

Run it anyway.

```
$ php artisan make:auth
```


In this case, I used same password on Laravel Auth to use an external authorization.
Because of it, we could use Laravel Auth as user filter.

```bash:.env
AUTH_PASSWORD=test
```

```php:config/auth.php
...
    'myauth_password' => env('AUTH_PASSWORD'),
];
```

Then u can call it like this.

```php:some_file.php
$my_auth_pw = config('auth.myauth_password'); // test
```

## Migration file

```
$ vim database/migrations/2014_10_12_000000_create_users_table.php
```
I don't use email column any more.

```php:database/migrations/2014_10_12_000000_create_users_table.php
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
           
        DB::table('users')->insert([
            'id' => 1,
            'password' => bcrypt(config('auth.auth_password'))
        ]);
    }
```

```php:app/User.php
...
    protected $fillable = [                                                        
        'id', 'password' // change
    ];  
                
...
    protected $casts = [                              
        //'email_verified_at' => 'datetime', // change
    ];  
```


```php:router/web.php                                                                    
Auth::routes();
Route::post('/login', 'Auth\LoginController@authenticate');
```


At first, call an external authorization.
Then pass return value to authenticate on Laravel.

```php:app/Http/Controller/Auth/LoginController.php

    private function externalAuth($mail_address, $password)
    {
        ....
        return $id;
    }

    public function authenticate(Request $request)
    {
        $id = $this->externalAuth($request->email, $request->password);
        if (Auth::validate(['id' => $id, 'password' => config('auth.myauth_password')]))
        {
            $user = User::where('id', $id)->first(); // can also use find method
            if($user){
                Auth::loginUsingId($user->id);
                return redirect('/');
            }else{
                return redirect()->back();
            }
        }
        return redirect()->back();
    }
```

## Migrate

```bash
$ php artisan migrate
```

## Finally

You can use your email and password for external authorization you've already have,
and you can log in as auth user on Laravel!

## Refs
[Authorization](https://laravel.com/docs/5.8/authorization)
[Laravel5.4でAuth::attemptを使わずに認証処理をカスタマイズ](https://takahashi-it.com/php/laravel54-auth-customize/)