GitHunt
WA

wahabkame/laravel-API-with-JWT-CRUD

Laravel API with JWT CRUD

laravel-API-with-JWT-CRUD

Laravel API with JWT & CRUD & Heroku


First you have to download:

XAMPP, MySQL, MySQL workbench
Postman, Heroku, Composer, Git


You have to install Laravel

Composer global require “Laravel/install”
Composer create-project –prefer-dist Laravel/Laravel (project name)

Prepare Heroku (Cloud):

Heroku create
Heroku logout
Heroku login 
Heroku config:set APP_KEY=
Heroku open
Echo web:vendor/bin/heroku-php-apache2 public /procfile
Heroku create 
Git push heroku master

Create a procfile :

 web:vendor/bin/heroku-php-apache2 public /

git add.
Git commit -m “ “
Git push heroku master

In mysql workbench :

New connection, New schema
Also in .env file , edit ( Database , username , password)


Inside of Heroku cloud:

Go resource and do add-ons and chose clearDB
And then write :

Heroku config|grep CLEARDB_DATABASE_URL
And you have to copy (@... .net) and use it as hostname 

And edit .env file (Database , username , password , host)

Php artisan migrate

If you found error ( key too long , max lenth …etc )
1- Fo to migration both (create_users, create_password)
2- Go to public function up()
Write

Schema::defultStringLenght(191);

3- Go to workbench , Drop (migration , users)
4- Then do

Php artisan migrate
Git add.
Git commit -m “”
Git push heroku master

To creat JWT , write in bash

Composer require Tymon/jwt-auth

Inside in file config/app.php in providers , write:

’Tymon\JWTAuth\Providers\JWTAuthServiceProvider’;

In aliases , write:

‘JWTAuth’=> ‘Tymon\JWTAuth\Facades\JWTAuth’
‘JWTFactory’=> ‘Tymon\JWTAuth\Facades\JWTFactory’

And write in terminal:

Php artisan vendor:publish—provider=’Tymon\JWTAuth\Providers\JWTAuthServiceProvider’

And write

Php artisan jwt:generate

If you found erro in handle, do this:
Go to folder vendor , file Tymon >Src>commands>JWTGenerateCommand
Inside in class JWTGenerateCommand
Write

Public function handle(){
$this->fire();
}

And again to generate

Php artisan jwt:generate

And it show Jwt-auth serect[-]

Php artisan migrate

Make controller

Inside controller:

Use App\Http\Controllers\Controller;
Use App\User;
Use JWTFactory;
Use Validator;
Use Response;

$request->get()
$request->all()
::first()
Validator::make()
JWTAuth::fromUser
Response::json(compact())

And also you need to connect controller to route (api.php)

Use Illuminate\Support\Facades\Auth

JWTAuth::attempt();

Middleware
In File Http > Kernel:
In Protected $routeMiddleware:

‘jwt.auth’=> ‘Tymon\JWTAuth\Middleware\GetUserFromToken’,
‘jwt.refresh=> ‘Tymon\JWTAuth\Middleware\refreshToken’,

In route(api.php)

Route::middleware(‘jwt.auth’)
Return auth()->user()

Migration

Php artisan make:migration ….

some syntax have to write :

ToArray()
->fails()
->errors()
->all()
->save()
->delete()
:make
:create
::find()
Validator