Skip to content

Commit ef5486a

Browse files
committed
changed readme
1 parent 7dd1c9d commit ef5486a

File tree

1 file changed

+76
-5
lines changed

1 file changed

+76
-5
lines changed

README.md

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/nrml-co/laravel-api-keys.svg?style=flat-square)](https://packagist.org/packages/nrml-co/laravel-api-keys)
44
[![Build Status](https://img.shields.io/travis/nrml-co/laravel-api-keys/master.svg?style=flat-square)](https://travis-ci.org/nrml-co/laravel-api-keys)
5-
[![Quality Score](https://img.shields.io/scrutinizer/g/nrml-co/laravel-api-keys.svg?style=flat-square)](https://scrutinizer-ci.com/g/nrml-co/laravel-api-keys)
65
[![Total Downloads](https://img.shields.io/packagist/dt/nrml-co/laravel-api-keys.svg?style=flat-square)](https://packagist.org/packages/nrml-co/laravel-api-keys)
6+
<!--
7+
[![Quality Score](https://img.shields.io/scrutinizer/g/nrml-co/laravel-api-keys.svg?style=flat-square)](https://scrutinizer-ci.com/g/nrml-co/laravel-api-keys)
8+
-->
9+
710

811
This package offers a different type on API key system for Laravel. The other options are either too simple or too complex.
912

@@ -16,7 +19,6 @@ This package offers:
1619
- scopes
1720

1821
Laravel/Passport is a the full on oauth implementation. This is a little more simple.
19-
2022

2123
## Installation
2224

@@ -26,11 +28,80 @@ You can install the package via composer:
2628
composer require nrml-co/laravel-api-keys
2729
php artisan migrate
2830
```
31+
Laravel 5.8 and above will register the service provider automatically.
32+
33+
## Usage - Creating Keys
34+
First add the HasApiKeys trait to the User model that ships with Laravel.
35+
```php
36+
use NrmlCo\LaravelApiKeys\HasApiKeys;
37+
38+
39+
/**
40+
* Class User
41+
* @package App
42+
*/
43+
class User extends Authenticatable
44+
{
45+
use Notifiable;
46+
use HasApiKeys;
47+
48+
```
49+
50+
Next create a User.
51+
``` php
52+
$user = User::create([
53+
'name' => 'Ed Anisko',
54+
'email' => 'ed@nrml.co',
55+
'email_verified_at' => now(),
56+
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
57+
'remember_token' => Str::random(10)
58+
]);
59+
```
60+
61+
The user needs to be logged in. Programmatically it looks like this.
62+
``` php
63+
Auth::setUser($user);
64+
```
65+
66+
And not the package will create ApiKeys for the Authorized user.
67+
``` php
68+
LaravelApiKeys::create(LaravelApiKeyType::PRODUCTION); // default is SANDBOX
69+
```
2970

30-
## Usage
31-
Laravel 5.8 and above will register the service provider automatically
71+
## Using the your API Keys
72+
Change config/auth.php
3273
``` php
33-
// Usage description here
74+
'guards' => [
75+
'web' => [
76+
'driver' => 'session',
77+
'provider' => 'users',
78+
],
79+
80+
'api' => [
81+
'driver' => 'lak',
82+
'provider' => 'users',
83+
'hash' => true,
84+
],
85+
86+
]
87+
```
88+
89+
90+
User the 'auth:lak' middleware in your api.php routes.
91+
``` php
92+
Route::middleware('auth:lak')->get('/user', function (Request $request) {
93+
return $request->user();
94+
});
95+
```
96+
97+
Check your work from the command line.
98+
``` bash
99+
100+
$ curl -X GET \
101+
http://homestead.test/api/user \
102+
-H 'Accept: application/json' \
103+
-H 'x-api-key: al4PA8V5jSuq4oFJOxK6lS4CeZEkDFtayBObJTHJ'
104+
34105
```
35106

36107
### Testing

0 commit comments

Comments
 (0)