Skip to content

Latest commit

 

History

History
198 lines (153 loc) · 6.04 KB

laravel-usage.md

File metadata and controls

198 lines (153 loc) · 6.04 KB

Laravel Support

The package doesn't add support for Laravel (yet). To install the package please follow the steps below.

Adding the configuration files

open the config/services.php file and add the following to the list of services:

'teamleader' => [
    'client_id' => env('TEAMLEADER_CLIENT_ID'),
    'client_secret' => env('TEAMLEADER_CLIENT_SECRET'),
    'redirect_uri' => env('TEAMLEADER_REDIRECT_URI'),
    'state' => env('TEAMLEADER_STATE', 'FtvPC1SE2h3LVPEJZIsrfaVWTwwn7T0R'),
],

Note: the state parameter. This can be whatever you want. We send the value to Teamleader, and Teamleader sends the same value back to us. If they don't match, someone tampered with the callback url.

Make sure those env values exist in your .env file

TEAMLEADER_CLIENT_ID=abc
TEAMLEADER_CLIENT_SECRET=abc
TEAMLEADER_REDIRECT_URI=https://yourapplication.com/teamleader/accept

Register a service provider

open up your console and type the following command:

php artisan make:provider TeamleaderServiceProvider

This is the content of that file:

<?php

namespace App\Providers;

use Exception;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\Facades\Storage;
use Justijndepover\Teamleader\Teamleader;

class TeamleaderServiceProvider extends ServiceProvider implements DeferrableProvider
{
    public function register()
    {
        $this->app->singleton(Teamleader::class, function ($app) {
            $teamleader = new Teamleader(
                config('services.teamleader.client_id'),
                config('services.teamleader.client_secret'),
                config('services.teamleader.redirect_uri'),
                config('services.teamleader.state'),
            );

            $teamleader->setTokenUpdateCallback(function ($teamleader) {
                Storage::disk('local')->put('teamleader.json', json_encode([
                    'accessToken' => $teamleader->getAccessToken(),
                    'refreshToken' => $teamleader->getRefreshToken(),
                    'expiresAt' => $teamleader->getTokenExpiresAt(),
                ]));
            });

            if (Storage::exists('teamleader.json') && $json = Storage::get('teamleader.json')) {
                try {
                    $json = json_decode($json);
                    $teamleader->setAccessToken($json->accessToken);
                    $teamleader->setRefreshToken($json->refreshToken);
                    $teamleader->setTokenExpiresAt($json->expiresAt);
                } catch (Exception $e) {
                }
            }

            if (! empty($teamleader->getRefreshToken()) && $teamleader->shouldRefreshToken()) {
                try {
                    $teamleader->connect();
                } catch (\Throwable $th) {
                    $teamleader->setRefreshToken('');

                    Storage::disk('local')->put('teamleader.json', json_encode([
                    'accessToken' => $teamleader->getAccessToken(),
                    'refreshToken' => $teamleader->getRefreshToken(),
                    'expiresAt' => $teamleader->getTokenExpiresAt(),
                ]));
                }
            }

            return $teamleader;
        });
    }

    public function provides()
    {
        return [Teamleader::class];
    }
}

Setting up the routes

Add the following 3 routes to your routes/web.php file

Route::get('teamleader', [SettingsController::class, 'index'])->name('settings.index');
Route::post('teamleader/authorize', [SettingsController::class, 'redirectForAuthorization'])->name('settings.teamleader.authorize');
Route::get('teamleader/accept', [SettingsController::class, 'accept']);

Note: the teamleader/accept route should always be accessible. Don't put it behind auth middleware. This route is called by Teamleader.

Creating the Controller

Next, create the controller that handles the routes:

php artisan make:controller SettingsController

This is the content of that file

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Justijndepover\Teamleader\Teamleader;

class SettingsController extends Controller
{
    public function index(Teamleader $teamleader)
    {
        // this view should show a 'connect to Teamleader' button
        // or when logged in, show a message: 'You are logged in'

        return view('settings.index', [
            'teamleader' => $teamleader,
        ]);
    }

    public function redirectForAuthorization(Teamleader $teamleader)
    {
        return redirect($teamleader->redirectForAuthorizationUrl());
    }

    public function accept(Request $request, Teamleader $teamleader)
    {
        if ($request->error) {
            return redirect()->route('settings.index')->with('error', __('The user refused to connect'));
        }

        if ($request->state != $teamleader->getState()) {
            return redirect()->route('settings.index')->with('error', __('The state parameter doesn\'t match.'));
        }

        $teamleader->setAuthorizationCode($request->code);
        $teamleader->connect();

        Storage::disk('local')->put('teamleader.json', json_encode([
            'accessToken' => $teamleader->getAccessToken(),
            'refreshToken' => $teamleader->getRefreshToken(),
            'expiresAt' => $teamleader->getTokenExpiresAt(),
        ]));

        return redirect()->route('settings.index')->with('message', __('You are connected with Teamleader'));
    }
}

Adding a view

The code above assumes you have a settings.index view. This could be the content of that file:

@if (session()->has('message'))
    {{ session()->get('message') }}
@endif

@if (session()->has('error'))
    {{ session()->get('error') }}
@endif

@if ($teamleader->shouldAuthorize())
    <form action="{{ route('settings.teamleader.authorize') }}" method="POST">
        @csrf
        <button type="submit">{{ __('default.teamleader.connect') }}</button>
    </form>
@else
    <p>You are already connected with Teamleader</p>
@endif