joekrump / react-cms-api-server

Server API implementation with Laravel for React CMS client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel API Boilerplate (JWT Edition)

Laravel API Boilerplate is a ready-to-use "starting pack" that you can use to build your first API in seconds. As you can easily imagine, it is built on top of the awesome Laravel Framework.

It also benefits from three pacakages:

With a similar foundation is really easy to get up and running in no time. I just made an "integration" work, adding here and there something that I found useful.

Installation

  • git clone the repository;
  • composer install;

Done!

Main Features

A Ready-To-Use AuthController

I've put an "AuthController" in App\Api\V1\Controllers. It supports the four basic authentication/password recovery operations:

  • login();
  • signup();
  • recovery();
  • reset();

In order to work with them, you just have to make a POST request with the required data.

You will need:

  • login: just email and password;
  • signup: whatever you like: you can specify it in the config file;
  • recovery: just the user email address;
  • reset: token, email, password and password confirmation;

A Separate File for Routes

You can specify your routes in the api_routes.php file, that will be automatically loaded. In this file you will find many examples of routes.

Configuration

As I already told before, this boilerplate is based on dingo/api and tymondesigns/jwt-auth packages. So, you can find many informations about configuration here and here.

However, there are some extra options that I placed in a config/boilerplate.php file.

  • signup_fields: you can use this option to specify what fields you want to use to create your user;
  • signup_fields_rules: you can use this option to specify the rules you want to use for the validator instance in the signup method;
  • signup_token_release: if "true", an access token will be released from the signup endpoint if everything goes well. Otherwise, you will just get a 201 Created response;
  • reset_token_release: if "true", an access token will be released from the signup endpoint if everything goes well. Otherwise, you will just get a 200 response;
  • recovery_email_subject: here you can specify the subject for your recovery data email;

Additional Configuration

Copy .example.env into .env file

Create database and set ENV variables in .env to match

Run

$ php artisan migrate

then

$ php artisan db:seed

Generate App key

$ php artisan key:generate

Secrets Generation

$ php artisan jwt:generate

Setup Stripe

Create a stripe.php file in the /config directory with the following contents:

<?php

return [

  'test' => [
    'sk' => env('STRIPE_TEST_SK', 'set me!')
  ],
  'live' =>
  [
    'sk' => env('STRIPE_LIVE_SK', 'set me!')
  ]
];

Now set the corresponding environment variables in your .env file.

Creating Endpoints

You can create endpoints in the same way you could to with using the single dingo/api package. You can read its documentation for details.

After all, that's just a boilerplate! :)

Cross Origin Resource Sharing

If you want to enable CORS for a specific route or routes group, you just have to use the cors middleware on them.

Thanks to the barryvdh/laravel-cors package, you can handle CORS easily. Just check the docs at this page for more info.

Notes

I currently removed the VerifyCsrfToken middleware from the $middleware array in app/Http/Kernel.php file. If you want to use it in your project, just use the route middleware csrf you can find, in the same class, in the $routeMiddleware array.

Production Server Update (HowTo)

Node:

npm run build-server
npm run build
cp -a build ./build-server
cp -a images ./build-server

pm is already running

pm2 restart server

pm is not running

cd ./build-server
pm2 start server.js
pm2 startup systemd

ensure that pm2 is running

systemctl status pm2

The list of applications currently managed by PM2 can also be looked up with the list subcommand:

pm2 list

list details specific to 'server'

pm2 info server

restart nginx to be safe

sudo systemctl restart nginx

Laravel API:

Do update then run:

sudo systemctl restart apache2
sudo systemctl restart nginx

About

Server API implementation with Laravel for React CMS client


Languages

Language:PHP 98.4%Language:Blade 1.0%Language:Shell 0.3%Language:JavaScript 0.3%Language:SCSS 0.0%