PHPackages                             dcodegroup/laravel-xero-oauth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. dcodegroup/laravel-xero-oauth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

dcodegroup/laravel-xero-oauth
=============================

Simple package which dcode uses to handle the connection to xero for all projects. Provides endpoints for UI and callbacks for Xero

1.0.30(1mo ago)131.9k↓10.7%[1 PRs](https://github.com/DCODE-GROUP/laravel-xero-oauth/pulls)3MITPHPPHP ^8.2CI passing

Since Sep 8Pushed 1w ago2 watchersCompare

[ Source](https://github.com/DCODE-GROUP/laravel-xero-oauth)[ Packagist](https://packagist.org/packages/dcodegroup/laravel-xero-oauth)[ RSS](/packages/dcodegroup-laravel-xero-oauth/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (10)Dependencies (27)Versions (38)Used By (3)

[![Latest Version](https://camo.githubusercontent.com/809966dcf103cf9faeb5a13e3980a10ce0f9fc57d1d397bf24e129f488909a8d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f64636f646567726f75702f6c61726176656c2d7865726f2d6f617574682e7376673f7374796c653d666c61742d737175617265)](https://github.com/dcodegroup/laravel-xero-oauth/releases)[![GitHub Workflow Status](https://camo.githubusercontent.com/88cf6ed8b4871b553042868c7b08688242240346e8d69f1b57479df6e3bcfb3c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f64636f646567726f75702f6c61726176656c2d7865726f2d6f617574682f436865636b253230262532306669782532307374796c696e67)](https://camo.githubusercontent.com/88cf6ed8b4871b553042868c7b08688242240346e8d69f1b57479df6e3bcfb3c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f64636f646567726f75702f6c61726176656c2d7865726f2d6f617574682f436865636b253230262532306669782532307374796c696e67)[![Total Downloads](https://camo.githubusercontent.com/6d9125dd47e8679141026e559da8561daab441f6648b6f6170a2d00498fe630e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64636f646567726f75702f6c61726176656c2d7865726f2d6f617574682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dcodegroup/laravel-xero-oauth)

Laravel Xero
============

[](#laravel-xero)

This package provides the standard xero connection functionality used in most projects.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require dcodegroup/laravel-xero-oauth
```

Then run the install command.

```
php artsian laravel-xero:install
```

This will publish the configuration file and the migration file.

Run the migrations

```
php artsian migrate
```

Configuration
-------------

[](#configuration)

Most of configuration has been set the fair defaults. However you can review the configuration file at `config/laravel-xero-oauth.php` and adjust as needed

If you are using Inertia, you can switch the package frontend driver from Blade to Inertia:

```
'frontend' => [
    'driver' => 'inertia',
    'inertia' => [
        'component' => 'Xero/OAuth/Index',
    ],
],
```

When `frontend.driver` is `inertia`, the package will:

- Render the index endpoint with your Inertia component
- Use Inertia location redirects for `/xero/auth` and `/xero/callback`

### Inertia Vue page contract (drop-in example)

[](#inertia-vue-page-contract-drop-in-example)

Set your component path to match the config default:

```
'frontend' => [
    'driver' => 'inertia',
    'inertia' => [
        'component' => 'Xero/OAuth/Index',
    ],
],
```

Create `resources/js/Pages/Xero/OAuth/Index.vue`:

```

import { router } from '@inertiajs/vue3';
import { route } from 'ziggy-js';

const props = defineProps({
  token: { type: Object, default: null },
  tenants: { type: Array, default: () => [] },
  currentTenantId: { type: String, default: null },
  authUrl: { type: String, required: true },
});

const connectToXero = () => {
  // Package returns an Inertia location response from /xero/auth
  window.location.href = props.authUrl;
};

const selectTenant = (tenantId) => {
  router.post(route('xero.tenant.update', { tenantId }));
};

    Xero OAuth

    Connect to Xero

    Connected
    Not connected

        {{ tenant.tenantName }}

          {{ tenant.tenantId === currentTenantId ? 'Current' : 'Use this tenant' }}

```

Expected props from the package index controller:

- `token`: latest stored token or `null`
- `tenants`: array of Xero tenants (`tenantId`, `tenantName`, `tenantType`, etc.)
- `currentTenantId`: currently selected tenant id or `null`
- `authUrl`: URL to start OAuth (`xero.auth`)

This example assumes Ziggy is installed and your route names are exposed to the client.

### Screenshots

[](#screenshots)

Blade (disconnected):

[![Blade disconnected](docs/screenshots/blade-disconnected.png)](docs/screenshots/blade-disconnected.png)

Blade (connected):

[![Blade connected](docs/screenshots/blade-connected.png)](docs/screenshots/blade-connected.png)

Inertia contract example:

[![Inertia contract](docs/screenshots/inertia-contract.png)](docs/screenshots/inertia-contract.png)

If you want to have the oauth screens appear within your sites layout ensure to configure the environment variable. eg.

`LARAVEL_XERO_OAUTH_APP_LAYOUT=layouts.admin`

Usage
-----

[](#usage)

The package provides an endpoints which you can use. See the full list by running

```
php artsian route:list --name=xero
```

```
+--------+----------+-------------------------+--------------------+-------------------------------------------------------------------------+----------------------------------+
| Domain | Method   | URI                     | Name               | Action                                                                  | Middleware                       |
+--------+----------+-------------------------+--------------------+-------------------------------------------------------------------------+----------------------------------+
|        | GET|HEAD | xero                    | xero.index         | Dcodegroup\LaravelXeroOauth\Http\Controllers\XeroController             | web                              |
|        |          |                         |                    |                                                                         | App\Http\Middleware\Authenticate |
|        | GET|HEAD | xero/auth               | xero.auth          | Dcodegroup\LaravelXeroOauth\Http\Controllers\XeroAuthController         | web                              |
|        |          |                         |                    |                                                                         | App\Http\Middleware\Authenticate |
|        | GET|HEAD | xero/callback           | xero.callback      | Dcodegroup\LaravelXeroOauth\Http\Controllers\XeroCallbackController     | web                              |
|        |          |                         |                    |                                                                         | App\Http\Middleware\Authenticate |
|        | POST     | xero/tenants/{tenantId} | xero.tenant.update | Dcodegroup\LaravelXeroOauth\Http\Controllers\SwitchXeroTenantController | web                              |
|        |          |                         |                    |                                                                         | App\Http\Middleware\Authenticate |
+--------+----------+-------------------------+--------------------+-------------------------------------------------------------------------+----------------------------------+

```

More Information

`example.com/xero` Which is where you will generate the link to authorise xero. This is by default protected auth middleware but you can modify in the configuration. This is where you want to link to in your admin and possibly a new window

`example.com/xero/callback` This is the route for which xero will redirect back to after the oauth has occurred. This is excluded from the middleware auth. You can change this list in the configuration also.

BaseXeroService
---------------

[](#basexeroservice)

The package has a `BaseXeroService` class located at `Dcodegroup\LaravelXeroOauth\BaseXeroService`

So your application should have its own XeroService extend this base class as the initialisation is already done.

```
