[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
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.
AUTH_PASSWORD=test
...
'myauth_password' => env('AUTH_PASSWORD'),
];
Then u can call it like this.
$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.
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'))
]);
}
...
protected $fillable = [
'id', 'password' // change
];
...
protected $casts = [
//'email_verified_at' => 'datetime', // change
];
Auth::routes();
Route::post('/login', 'Auth\LoginController@authenticate');
At first, call an external authorization.
Then pass return value to authenticate on Laravel.
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
$ 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!