An open API service indexing awesome lists of open source software.

https://github.com/mosesesan/mesan-laravel-jwt-authentication-quotes-api

A PHP API for a Quotes Mobile App developed with Laravel 5.4 framework and JWT (JSON Web Tokens) Package.
https://github.com/mosesesan/mesan-laravel-jwt-authentication-quotes-api

jwt-authentication laravel laravel54

Last synced: 8 months ago
JSON representation

A PHP API for a Quotes Mobile App developed with Laravel 5.4 framework and JWT (JSON Web Tokens) Package.

Awesome Lists containing this project

README

          

# Laravel 5.4 JWT-Powered Mobile App API

This API was built for the Quotes app at the repo below.

React Native/Redux Quotes App

### Tutorial


### Step 1: Create new project and install jwt-auth

Create Laravel project
```bash
laravel new JWTAuthentication
```
Open composer.json and update the require object to include jwt-auth

```php
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0",
"tymon/jwt-auth": "0.5.*"
}
```
Then, run
```bash
composer update
```


### Step 2: Add JWT Provider and Facades

We’ll now need to update the providers array in config/app.php with the jwt-auth provider. Open up config/app.php, find the providers array located on line 138 and add this to it:

```php
Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class,

```
Add in the jwt-auth facades which we can do in config/app.php. Find the aliases array and add these facades to it:

```php
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class

```
We also need to publish the assets for this package. From the command line:

```bash
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"

```

After you run this command you will see a new file in the config folder called jwt.php. This file contains settings for jwt-auth, one of which we need to change right away. We need to generate a secret key which we can do from the command line:
```bash
php artisan jwt:generate

```

You’ll see that after running this command we get a new value next to’secret’ where “changeme” was before.

Register the jwt.auth and jwt.refresh middleware in app/http/Kernel.php

```php
protected $routeMiddleware = [
...
'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken',
'jwt.refresh' => 'Tymon\JWTAuth\Middleware\RefreshToken',
];
```


### Step 3: Set Up Routes

Open up routes/api.php.

```php
Route::post('register', 'AuthController@register');
Route::post('login', 'AuthController@login');
Route::post('recover', 'AuthController@recover');

Route::group(['middleware' => ['jwt.auth']], function() {
Route::get('logout', 'AuthController@logout');
});
```


### Step 4: Set Up Database

Since we are going to allow users to create their accounts within the application, we will need a table to store all of our users. Thankfully, Laravel already ships with a migration to create a basic users table, so we do not need to manually generate one. The default migration for the users table is located in the database/migrations directory.

We need to add an extra column to the users table.

Available on my blog.


### Step 5: Register

Available on my blog.


### Step 6: Log User In and Out

Available on my blog.


### Step 7: Recover Password

Available on my blog.


### Step 8: Testing

Available on my blog.