Magidoc

In this tutorial we are going to set-up a Laravel application that uses the Orderchamp/orderchamp-api-php client. The client can be used stand alone, for the purpose of this tutorial we are going to use the Laravel package Orderchamp/orderchamp-laravel , which is basically a wrapper for the client that can be configured by using environment variables.

Preparing the project

#

Let's start by creating a bare Laravel application by using Composer.

    
  

Adding the Laravel package

#

    
  

Updating .env

#

Open .env file and add the following environment variables:

    
  

Configure permission scope

#

Create the file config/orderchamp.php to set the proper permissions you need for your app.

    
  

Register Orderchamp Laravel Middleware

#

Open app/Http/Kernel.php to register the Auth Middleware that comes with the laravel package.

    
  

AuthController and registering route

#

This route will be used by Orderchamp to be redirected to so the app can validate the authenticated user. Create app/Http/Controllers/AuthController.php

    
  

Now register the controller in routes/web.php

    
  

Note: these URL's are publicly available and used to store the ACCESS_TOKEN inside of a session.

Using protected routes for authenticated users

#

Use the Laravel Middleware that comes with the orderchamp-laravel package to verify authenticated users in protected routes. It is useful to proxy the GraphQL API so you can make a SPA for your integration.

Bonus: proxy GraphQL

#

You can proxy the GraphQL API by registering your own graphql endpoint, this endpoint can be consumed by your frontend application. By registering the 'orderchamp.auth' Middleware for this new route, you ensure that this proxy is restricted to authenticated users only. If an unauthenticated user tries to access this URL it will be redirected to the login page.

Create app/Http/Controllers/GraphiqlController.php

    
  

Now register the controller in routes/web.php

    
  

This will allow you to post GraphQL requests to https://YOUR_APP_URL/graphql.