├── .php_cs.dist ├── LICENSE ├── README.md ├── composer.json ├── config └── magic.php └── src ├── Facade.php ├── Magic.php └── ServiceProvider.php /.php_cs.dist: -------------------------------------------------------------------------------- 1 | setRiskyAllowed(true) 5 | ->setRules([ 6 | // Rulesets 7 | '@PSR2' => true, 8 | '@PhpCsFixer' => true, 9 | '@PhpCsFixer:risky' => true, 10 | '@PHP56Migration:risky' => true, 11 | '@PHPUnit57Migration:risky' => true, 12 | 13 | // Additional rules 14 | 'fopen_flags' => true, 15 | 'linebreak_after_opening_tag' => true, 16 | 'native_function_invocation' => true, 17 | 'method_argument_space' => ['ensure_fully_multiline' => true], 18 | 'ordered_imports' => true, 19 | 20 | // --- Diffs from @PhpCsFixer / @PhpCsFixer:risky --- 21 | 22 | // This is just prettier / easier to read. 23 | 'concat_space' => ['spacing' => 'one'], 24 | 25 | // This causes strange ordering with codegen'd classes. We might be 26 | // able to enable this if we update codegen to output class elements 27 | // in the correct order. 28 | 'ordered_class_elements' => false, 29 | 30 | // Keep this disabled to avoid unnecessary diffs in PHPDoc comments of 31 | // codegen'd classes. 32 | 'phpdoc_align' => false, 33 | ]) 34 | ; 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Magic Labs Inc. 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 | # Magic Laravel SDK 2 | 3 | This package provides direct Laravel integration for [Magic Admin PHP SDK](https://github.com/magiclabs/magic-admin-php). 4 | 5 | ## Requirements 6 | 7 | Laravel 5.3 or newer. 8 | 9 | ## Installation 10 | 11 | Run this command to install via composer. 12 | 13 | ``` 14 | composer require magiclabs/magic-laravel 15 | ``` 16 | 17 | ## Older Laravel Installations 18 | 19 | If you are using Laravel 5.5 or newer then skip this part. Otherwise you will need to manually register the service provider and facade alias by going to `config/app.php`. 20 | 21 | Add to the `providers` array: 22 | 23 | ``` 24 | MagicLaravel\ServiceProvider::class 25 | ``` 26 | 27 | Add to the `aliases` array: 28 | 29 | ``` 30 | 'Magic' => MagicLaravel\Facade::class, 31 | ``` 32 | 33 | ## Setup 34 | 35 | Publish config file. 36 | 37 | ``` 38 | php artisan vendor:publish --provider="MagicLaravel\ServiceProvider" 39 | ``` 40 | 41 | Add `MAGIC_SECRET_API_KEY` to your **.env** file by going to the [Magic Dashboard](https://dashboard.magic.link). 42 | 43 | ``` 44 | MAGIC_SECRET_API_KEY=sk_XXXX_XXXXXXXXXXXXXXXX 45 | ``` 46 | 47 | ## Usage 48 | 49 | For complete documentation see [Magic Admin PHP SDK](https://github.com/magiclabs/magic-admin-php) and the [Magic docs](https://docs.magic.link/admin-sdk/php/get-started). 50 | 51 | Get instance using the app helper: 52 | 53 | ``` 54 | $magic = app('MagicLaravel\Magic'); 55 | 56 | $magic->token->validate(''); 57 | 58 | $magic->user->get_metadata_by_token(''); 59 | ``` 60 | 61 | Or get instance using the facade: 62 | 63 | ``` 64 | use Magic; 65 | 66 | Magic::token()->validate(''); 67 | 68 | Magic::user()->get_metadata_by_token(''); 69 | ``` 70 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "magiclabs/magic-laravel", 3 | "description": "Magic Admin Laravel Library", 4 | "keywords": [ 5 | "magic", 6 | "link", 7 | "admin", 8 | "authentication", 9 | "passwordless", 10 | "oauth2", 11 | "webauthen", 12 | "laravel" 13 | ], 14 | "homepage": "https://magic.link", 15 | "license": "MIT", 16 | "authors": [ 17 | { 18 | "name": "Magic Labs Inc.", 19 | "email": "support@magic.link" 20 | } 21 | ], 22 | "require": { 23 | "magiclabs/magic-admin-php": "^0.1.4", 24 | "illuminate/support": "^5.3|^6.0|^7.0|^8.0|^9.0|^10.0" 25 | }, 26 | "require-dev": { 27 | "orchestra/testbench": "^6.5" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "MagicLaravel\\": "src/" 32 | } 33 | }, 34 | "autoload-dev": { 35 | "psr-4": { 36 | "MagicLaravel\\Tests\\": "tests" 37 | } 38 | }, 39 | "extra": { 40 | "laravel": { 41 | "providers": [ 42 | "MagicLaravel\\ServiceProvider" 43 | ], 44 | "aliases": { 45 | "Magic": "MagicLaravel\\Facade" 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /config/magic.php: -------------------------------------------------------------------------------- 1 | env('MAGIC_SECRET_API_KEY', null), 14 | 15 | /* 16 | |-------------------------------------------------------------------------- 17 | | HTTP request strategy 18 | |-------------------------------------------------------------------------- 19 | | 20 | | Customize your HTTP request strategy when making calls to the Magic API 21 | | 22 | */ 23 | 24 | 'http' => [ 25 | 'retries' => env('MAGIC_RETRIES', 3), // Total number of retries to allow 26 | 27 | 'timeout' => env('MAGIC_TIMEOUT', 10), // A period of time the request is going to wait for a response 28 | 29 | 'backoff_factor' => env('MAGIC_BACKOFF_FACTOR', 0.02), // A backoff factor to apply between retry attempts 30 | ], 31 | ]; 32 | -------------------------------------------------------------------------------- /src/Facade.php: -------------------------------------------------------------------------------- 1 | token; 10 | } 11 | 12 | public function user() 13 | { 14 | return $this->user; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton(Magic::class, function () { 13 | $magic = new Magic( 14 | config('magic.secret_api_key'), 15 | config('magic.http.timeout'), 16 | config('magic.http.retries'), 17 | config('magic.http.backoff_factor') 18 | ); 19 | 20 | $magic->_set_platform('laravel'); 21 | 22 | return $magic; 23 | }); 24 | } 25 | 26 | /** 27 | * Bootstrap the application events. 28 | */ 29 | public function boot() 30 | { 31 | $configPath = __DIR__ . '/../config/magic.php'; 32 | $this->publishes([$configPath => $this->getConfigPath()], 'config'); 33 | } 34 | 35 | /** 36 | * Get the config path. 37 | * 38 | * @return string 39 | */ 40 | protected function getConfigPath() 41 | { 42 | return config_path('magic.php'); 43 | } 44 | 45 | /** 46 | * Publish the config file. 47 | * 48 | * @param string $configPath 49 | */ 50 | protected function publishConfig($configPath) 51 | { 52 | $this->publishes([$configPath => config_path('magic.php')], 'config'); 53 | } 54 | } 55 | --------------------------------------------------------------------------------