├── .DS_Store ├── src ├── assets │ ├── auth.css │ └── auth.js ├── config │ └── firebase.php ├── Http │ ├── Controller.php │ └── AuthController.php ├── User.php ├── resources │ ├── lang │ │ └── en │ │ │ └── auth.php │ └── views │ │ └── auth.blade.php ├── Authenticatable.php ├── ServiceProvider.php └── AuthenticatesUsers.php ├── screenshots └── sign-in-providers.png ├── .gitattributes ├── database └── migrations │ └── 2016_07_26_220000_create_firebase_users_table.php ├── composer.json ├── LICENSE.txt └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinkashq/firebase-auth-laravel/HEAD/.DS_Store -------------------------------------------------------------------------------- /src/assets/auth.css: -------------------------------------------------------------------------------- 1 | #header .logo { margin-top: 25px; display: inline-block; } 2 | #header .logo img { max-height: 50px; } 3 | -------------------------------------------------------------------------------- /screenshots/sign-in-providers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinkashq/firebase-auth-laravel/HEAD/screenshots/sign-in-providers.png -------------------------------------------------------------------------------- /src/config/firebase.php: -------------------------------------------------------------------------------- 1 | env('FIREBASE_PROJECT_ID', null), 6 | 7 | 'api_key' => env('FIREBASE_API_KEY', null), 8 | 9 | 'auth_domain' => env('FIREBASE_AUTH_DOMAIN', null), 10 | 11 | ]; 12 | -------------------------------------------------------------------------------- /src/Http/Controller.php: -------------------------------------------------------------------------------- 1 | 'Email address not yet verified! You must verify your email address before sign in.', 17 | 18 | ]; 19 | -------------------------------------------------------------------------------- /src/assets/auth.js: -------------------------------------------------------------------------------- 1 | function auth(user, token) { 2 | user.getToken(true).then(function(idToken) { 3 | $.ajax({ 4 | url: '/auth', 5 | type: "post", 6 | data: { 7 | 'id_token': idToken, 8 | 'name': user.displayName, 9 | 'email': user.email, 10 | 'photo_url': user.photoURL, 11 | '_token': token 12 | }, 13 | success: function(data){ 14 | if(data.success) { 15 | window.location.replace(data.redirectTo); 16 | } 17 | else { 18 | notice(data.message); 19 | } 20 | }, 21 | error: function(xhr, textStatus, errorThrown) { 22 | notice(textStatus); 23 | } 24 | }); 25 | }).catch(function(error) { 26 | notice(error); 27 | }); 28 | } 29 | -------------------------------------------------------------------------------- /src/Authenticatable.php: -------------------------------------------------------------------------------- 1 | string('id')->primary(); 17 | $table->string('name')->nullable(); 18 | $table->string('email')->nullable()->unique(); 19 | $table->string('photo_url')->nullable(); 20 | $table->rememberToken(); 21 | $table->timestamps(); 22 | }); 23 | } 24 | 25 | /** 26 | * Reverse the migrations. 27 | * 28 | * @return void 29 | */ 30 | public function down() 31 | { 32 | Schema::drop('firebase_users'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vinkas/firebase-auth-laravel", 3 | "description": "Firebase Authentication package for Laravel PHP Framework", 4 | "keywords": ["firebase", "auth", "authentication", "laravel", "vinkas"], 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Vinoth Kannan", 10 | "email": "vinothkannan@vinkas.com", 11 | "homepage": "https://github.com/vinothkananns" 12 | }, 13 | { 14 | "name": "Vinkas", 15 | "homepage": "http://vinkas.com" 16 | } 17 | ], 18 | "minimum-stability": "dev", 19 | "require": { 20 | "illuminate/support": "^5.2", 21 | "illuminate/contracts": "^5.2", 22 | "firebase/php-jwt": "^5.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Vinkas\\Firebase\\Auth\\": "src" 27 | } 28 | }, 29 | "extra": { 30 | "branch-alias": { 31 | "dev-master": "1.0-dev" 32 | } 33 | }, 34 | "config": { 35 | "preferred-install": "dist" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 17 | __DIR__.'/config/firebase.php' => config_path('vinkas/firebase/auth.php'), 18 | ], 'config'); 19 | 20 | $this->publishes([ 21 | __DIR__.'/../database/migrations/' => database_path('migrations'), 22 | ], 'migrations'); 23 | 24 | $this->publishes([ 25 | __DIR__.'/assets' => public_path('vinkas/firebase'), 26 | ], 'public'); 27 | 28 | $this->publishes([ 29 | __DIR__.'/resources/views' => resource_path('views/vinkas/firebase'), 30 | ], 'views'); 31 | 32 | $this->publishes([ 33 | __DIR__.'/resources/lang' => resource_path('lang/vinkas/firebase'), 34 | ], 'lang'); 35 | } 36 | 37 | /** 38 | * Register bindings in the container. 39 | * 40 | * @return void 41 | */ 42 | public function register() 43 | { 44 | // 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Vinoth Kannan , Vinkas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Firebase Authentication package for Laravel PHP framework 2 | 3 | [![Discuss Group](https://img.shields.io/badge/discuss.group-vinkas-blue.svg?style=flat-square)](http://vinkas.discuss.group/c/firebase-auth-laravel) 4 | [![Latest Stable Version](https://poser.pugx.org/vinkas/firebase-auth-laravel/v/stable.svg?format=flat-square)](https://packagist.org/packages/vinkas/firebase-auth-laravel) 5 | [![Latest Unstable Version](https://poser.pugx.org/vinkas/firebase-auth-laravel/v/unstable.svg?format=flat-square)](https://packagist.org/packages/vinkas/firebase-auth-laravel) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/vinkas/firebase-auth-laravel.svg?style=flat-square)](https://packagist.org/packages/vinkas/firebase-auth-laravel) 7 | 8 | ## Installation 9 | 10 | #### Via Composer Require 11 | 12 | You may install by running the `composer require` command in your terminal: 13 | ``` 14 | composer require vinkas/firebase-auth-laravel 15 | ``` 16 | 17 | **Add Service Provider to your `config/app.php` file** 18 | 19 | ``` 20 | Vinkas\Firebase\Auth\ServiceProvider::class, 21 | ``` 22 | 23 | **Run `php artisan` command to publish package files into your app** 24 | 25 | ``` 26 | php artisan vendor:publish --provider="Vinkas\Firebase\Auth\ServiceProvider" 27 | ``` 28 | 29 | **Add your firebase project id, api key and auth domain in `.env` file** 30 | 31 | ``` 32 | FIREBASE_PROJECT_ID=__________ 33 | FIREBASE_API_KEY=__________ 34 | FIREBASE_AUTH_DOMAIN=__________ 35 | ``` 36 | 37 | 38 | ## Configuration 39 | 40 | * [Override Laravel's existing authentication method](http://vinkas.discuss.group/t/Override-Laravels-existing-authentication-method/81). 41 | * [Add as additional authentication option](http://vinkas.discuss.group/t/Add-as-additional-authentication-option/80). Without modifying Laravel's existing methods. 42 | 43 | ## Screenshots 44 | 45 | ![FirebaseUI Web](/screenshots/sign-in-providers.png) 46 | 47 | ## Dependencies 48 | 49 | * [Firebase php JWT](https://github.com/firebase/php-jwt) 50 | * [FirebaseUI Web](https://github.com/firebase/firebaseui-web) 51 | -------------------------------------------------------------------------------- /src/Http/AuthController.php: -------------------------------------------------------------------------------- 1 | middleware('guest'); 40 | } 41 | 42 | /** 43 | * Get a validator for an incoming registration request. 44 | * 45 | * @param array $data 46 | * @return \Illuminate\Contracts\Validation\Validator 47 | */ 48 | protected function validator(array $data) 49 | { 50 | return Validator::make($data, [ 51 | 'id_token' => 'required', 52 | 'remember' => 'boolean', 53 | 'email' => 'email', 54 | 'photo_url' => 'url', 55 | ]); 56 | } 57 | 58 | /** 59 | * Create a new user instance after a valid registration. 60 | * 61 | * @param array $data 62 | * @return User 63 | */ 64 | protected function create(array $data) 65 | { 66 | return User::create([ 67 | 'id' => $data['id'], 68 | 'name' => $data['name'], 69 | 'email' => $data['email'], 70 | 'photo_url' => $data['photo_url'], 71 | ]); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/AuthenticatesUsers.php: -------------------------------------------------------------------------------- 1 | firebaseAuthView : 'vinkas.firebase.auth'; 17 | return view($firebaseAuth); 18 | } 19 | 20 | public function postAuth(Request $request) { 21 | $data = $request->all(); 22 | $validator = $this->validator($data); 23 | if ($validator->fails()) 24 | return $this->onFail($validator->errors()->first()); 25 | 26 | JWT::$leeway = 8; 27 | $content = file_get_contents("https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com"); 28 | $kids = json_decode($content, true); 29 | $jwt = JWT::decode($request->input('id_token'), $kids, array('RS256')); 30 | $fbpid = property_exists($this, 'firebaseProjectId') ? $this->firebaseProjectId : config('vinkas.firebase.auth.project_id'); 31 | $issuer = 'https://securetoken.google.com/' . $fbpid; 32 | if($jwt->aud != $fbpid) 33 | return $this->onFail('Invalid audience'); 34 | elseif($jwt->iss != $issuer) 35 | return $this->onFail('Invalid issuer'); 36 | elseif(empty($jwt->sub)) 37 | return $this->onFail('Invalid user'); 38 | else { 39 | $uid = $jwt->sub; 40 | $user = $this->firebaseLogin($uid, $request); 41 | if($user) 42 | return response()->json(['success' => true, 'redirectTo' => $this->redirectPath()]); 43 | else 44 | return $this->onFail('Error'); 45 | } 46 | } 47 | 48 | protected function onFail($message) { 49 | return response()->json(['success' => false, 'message' => $message]); 50 | } 51 | 52 | protected function firebaseLogin($uid, $request) { 53 | $user = Auth::getProvider()->retrieveById($uid); 54 | 55 | if (is_null($user)) 56 | $this->firebaseRegister($uid, $request); 57 | 58 | $remember = $request->has('remember') ? $request->input('remember') : false; 59 | return Auth::loginUsingId($uid, $remember); 60 | } 61 | 62 | protected function firebaseRegister($uid, $request) { 63 | $data['id'] = $uid; 64 | $data['name'] = $request->has('name') ? $request->input('name') : null; 65 | $data['email'] = $request->has('email') ? $request->input('email') : null; 66 | $data['photo_url'] = $request->has('photo_url') ? $request->input('photo_url') : null; 67 | $this->create($data); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/resources/views/auth.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | {{ getEnv('APP_NAME')}} 10 | 11 | 12 | 13 | 14 | 15 | 16 | 21 |
22 | 25 |
26 |
27 | 28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 | 40 | 41 | 42 | 43 | 51 | @if (Auth::check()) 52 | @else 53 | 54 | @endif 55 | 60 | 61 | 62 | 88 | 89 | 90 | 91 | 92 | --------------------------------------------------------------------------------