1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-05-08

[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

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.

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

Then u can call it like this.

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.

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'))
        ]);
    }
app/User.php
...
    protected $fillable = [                                                        
        'id', 'password' // change
    ];  
                
...
    protected $casts = [                              
        //'email_verified_at' => 'datetime', // change
    ];  
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.

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

$ 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
Laravel5.4でAuth::attemptを使わずに認証処理をカスタマイズ

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?