PHPackages                             sdwru/laravel-firebase-auth-plus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. sdwru/laravel-firebase-auth-plus

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

sdwru/laravel-firebase-auth-plus
================================

Secure your laravel API with Google Firebase Auth and also get a full Firebase Admin SDK

1.0(5y ago)4422MITPHPPHP &gt;=7.0

Since Jan 27Pushed 4y agoCompare

[ Source](https://github.com/sdwru/laravel-firebase-auth-plus)[ Packagist](https://packagist.org/packages/sdwru/laravel-firebase-auth-plus)[ RSS](/packages/sdwru-laravel-firebase-auth-plus/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

laravel-firebase-auth-plus
==========================

[](#laravel-firebase-auth-plus)

Secure your laravel API with Google Firebase Auth

Adding the *Middleware* to your Laravel API will ensure that access is only granted by using a valid bearer token issued by Goggle Firebase Auth.

The main difference between this package and the package we forked it from is that we are using [laravel-firebase](https://github.com/kreait/laravel-firebase) as a dependency. Using laravel-firebase instead of firebase-tokens removes the need for a service provider since it is already included in laravel-firebase. Since that package depends on [firebase-php](https://github.com/kreait/firebase-php), you can also use [all the feature firebase-php provides](https://github.com/kreait/firebase-php#documentation).

#### Role middleware

[](#role-middleware)

This package includes optional role middleware for more granular access.

Install
-------

[](#install)

```
composer require sdwru/laravel-firebase-auth-plus
```

#### laravel-firebase

[](#laravel-firebase)

Publish the laravel-firebase ServiceProvider (`Provider: Kreait\Laravel\Firebase\ServiceProvider`) if not already done so.

```
php artisan vendor:publish
```

Configure laravel-firebase [according to their instructions](https://github.com/kreait/laravel-firebase/blob/master/README.md) and also explained in the official firebase documentation [at this link](https://firebase.google.com/docs/admin/setup#initialize-sdk).

Those instructions make it sound more complicated than it is. All we need to do is generate a JSON file as follows:

1. In the Firebase console, open Settings &gt; Service Accounts.
2. Click Generate New Private Key, then confirm by clicking Generate Key.
3. Securely store the generated JSON file and add a reference to that file in your laravel `.env` file. The following example assumes we are storing the file in the root folder of our laravel installation. Rename it to whatever you want.

```
FIREBASE_CREDENTIALS=myproject-firebase-adminsdk.json
```

How to use
----------

[](#how-to-use)

There are two ways to use this.

### Method 1. Lock all access without JWT token

[](#method-1-lock-all-access-without-jwt-token)

Add the *Middleware* on your app/Http/*Kernel.php* file.

```
\sdwru\LaravelFirebaseAuth\Middleware\JWTAuth::class,

```

Refer to the [Laravel Middleware documentation](https://laravel.com/docs/7.x/middleware) on where you can put this in your Kernel.php file and how it can be used in routes.

### Method 2 (recommended) using an authentication guard.

[](#method-2-recommended-using-an-authentication-guard)

Add the Guard to `app/Providers/AuthServiceProvider.php` in the `boot` method.

```
public function boot()
{
   $this->registerPolicies();

   $this->app['auth']->viaRequest('firebase', function ($request) {
       return app(\sdwru\LaravelFirebaseAuth\Guard::class)->user($request);
   });
}
```

In `config/auth.php` set your api guard driver to `firebase` and the model to `LaravelFirebaseAuth\User::class`

```
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'firebase',
            'provider' => 'firebase',
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        'firebase' => [
            'driver' => 'firebase',
            'model' => \sdwru\LaravelFirebaseAuth\User::class,
        ],
],
```

Add authentication to api routes in `routes/api.php`.

```
Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
    //return true;
});

Route::middleware('auth:api')->apiResource('some_endpoint', 'API\SomeEndpointController');
```

#### Example: Retrieve uid (For method #2 only) from API UserController

[](#example-retrieve-uid-for-method-2-only-from-api-usercontroller)

```
