PHPackages                             cub/cub-laravel - 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. [Framework](/categories/framework)
4. /
5. cub/cub-laravel

ActiveLibrary[Framework](/categories/framework)

cub/cub-laravel
===============

Laravel wrapper for cub/cub

6.0.0(9mo ago)05.5k↓71.4%MITPHPPHP ^8.0.2 || ^8.1 || ^8.2 || ^8.3CI failing

Since Sep 25Pushed 9mo ago12 watchersCompare

[ Source](https://github.com/praetoriandigital/cub-laravel)[ Packagist](https://packagist.org/packages/cub/cub-laravel)[ Docs](https://github.com/praetoriandigital/cub-laravel)[ RSS](/packages/cub-cub-laravel/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)Dependencies (10)Versions (66)Used By (0)

Cub Laravel
===========

[](#cub-laravel)

[![Built For Laravel](https://camo.githubusercontent.com/5dc33dc453936987e8423b2e93ebdb1955ecc7187cc664824ca4a957aecb85c2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c74253230666f722d6c61726176656c2d626c75652e737667)](http://laravel.com)[![Build Status](https://camo.githubusercontent.com/46c69087663d62c8c35bc17c53784496f096d6f6fa29bd6157bc33fe3e08cb95/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f70726165746f7269616e6469676974616c2f6375622d6c61726176656c2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/praetoriandigital/cub-laravel)[![Latest Version on Packagist](https://camo.githubusercontent.com/8a57cfe1208a9e1d5ec24443008807676c34966c24f1abdd171485dcf8de2fb3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6375622f6375622d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/cub/cub-laravel)[![Total Downloads](https://camo.githubusercontent.com/62e40e3cc8270be4af06611a712bc28ecd7b320c9e364a3b5d159c44a3d24358/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6375622f6375622d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/cub/cub-laravel)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Laravel wrapper for [cub/cub](https://packagist.org/packages/cub/cub).

Use for authenticating your users with Cub. Keep your user data up-to-date with what is happening in Cub.

- [Compatibility](#compatibility)
- [Installation](#installation)
- [Usage](#usage)
- [Configuration](#configuration)

Compatibility
-------------

[](#compatibility)

Currently compatible with Laravel 5.\*

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

[](#installation)

Via Composer

```
$ composer require "cub/cub-laravel:~2.0"
```

After updating composer, add the CubLaravelServiceProvider to the `providers` array in `config/app.php`

```
Cub\CubLaravel\ServiceProvider::class,
```

Next make sure to add the Cub alias in the `aliases` array in the same `config/app.php`

```
'Cub' => Cub\CubLaravel\Facades\Cub::class,
'CubWidget' => Cub\CubLaravel\Facades\CubWidget::class,
```

Next add the `cub_id` field to your relevant Models with the following command:

```
php artisan migrate --package="cub/cub-laravel"
```

Add `cub_id` to the relevant Models's `$fillable` array:

```
    public $fillable = [
        ...
        'cub_id',
        ...
    ];
```

IMPORTANT: You will need to make sure to trigger the Cub webhook for the relevant Models.

Usage
-----

[](#usage)

#### Logging in

[](#logging-in)

Just pass the user's username and password to the Cub facade. You'll get a Login object that you use to access the User and the Cub token.

```
$login = Cub::login($username, $password);

// assuming the login was successful, this will be
// an instance of your application's User model
$user = $login->getUser();

// JWT token returned by Cub during login
$token = $login->getToken();
```

#### Getting a user by Cub `user` id

[](#getting-a-user-by-cub-user-id)

For convenience, you have the ability to get an instance of your application's User model when you possess a Cub `user` id.

```
// an instance of your application's User model
$user = Cub::getUserById($cubId);
```

#### Getting a user by JWT

[](#getting-a-user-by-jwt)

For convenience, you have the ability to get an instance of your application's User model when you possess a Cub JWT.

```
// an instance of your application's User model
$user = Cub::getUserByJWT($jwt);
```

Or if the Cub JWT was passed in on the request in the Authorization request header OR there is a `cubUserToken` cookie on the request OR the request was made with a query parameter with the key of `cub_token`. (The checking happens in the described order).

```
// an instance of your application's User model
$user = Cub::getUserByJWT();
```

#### Route filtering for JWT

[](#route-filtering-for-jwt)

Filter routes based on the Cub JWT. The filter will check the request's Authorization header or it will check for the contents of the `cub_token` key in the request's query string.

```
Route::get('restricted', ['before' => 'cub-auth', function() {
   // after cub-auth the application user will be accessible as currentUser
   // an instance of your application's User model
   $user = Cub::currentUser();
   return json_encode(['message' => "You're in, {$user->first_name}!"]);
}]);
```

#### Convenience helpers

[](#convenience-helpers)

Sometimes you may just need to know if there is a valid Cub JWT on the incoming request. Use this:

```
$exists = Cub::validJWTExists();
```

Get the current User and Token:

```
// an instance of your application's User model
$user = Cub::currentUser();

// the current Cub JWT
$token = Cub::currentToken();
```

#### CubWidget

[](#cubwidget)

Get all the widget elements you need with the `CubWidget` facade. Look to the Cub Widget [docs](https://github.com/praetoriandigital/cub-docs) for further information about the widget.

```
CubWidget::headerScript();
CubWidget::menu();
CubWidget::app();
// Footer script will be configured with your application's public key
CubWidget::footerScript();
```

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

[](#configuration)

You're going to need to provide a few things in order to get this package working correctly for you.

If you don't need to change the default user model, you can get away with just setting the appropriate env variables. The necessary env variables are as follows.

```
CUB_PUBLIC
CUB_SECRET
CUB_API_URL
CUB_WEBHOOK_URL
```

But if you need to change the user model, or if you prefer to set a lot of this in the config file you can do the following.

```
php artisan config:publish cub/cub-laravel
```

Then you can update the config file as below.

(DO NOT PUT YOUR SECRET KEY IN THE CONFIG FILE).

```
