Skip to content

Commit

Permalink
Minor clarifications
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Feb 16, 2025
1 parent ee1d47b commit 83b4863
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 186 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,16 @@ jobs:
strategy:
matrix:
php-version:
- 8.1
- 8.2
- 8.3
- 8.4
laravel-constraint:
- 10.*
- 11.*
- 12.*
dependencies: [ lowest, highest ]
exclude:
- laravel-constraint: 11.*
php-version: 8.1
- laravel-constraint: 12.*
php-version: 8.2

steps:
- name: Set up PHP
Expand All @@ -87,7 +86,10 @@ jobs:
run: composer run-script test

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: Laragear/TwoFactor

static_analysis:
name: 3️⃣ Static Analysis
Expand Down Expand Up @@ -128,7 +130,7 @@ jobs:

- name: Check exported files
run: |
EXPECTED="LICENSE.md,MIGRATIONS.md,README.md,composer.json"
EXPECTED="DATABASE.md,LICENSE.md,README.md,composer.json"
CURRENT="$(git archive HEAD | tar --list --exclude="src" --exclude="src/*" --exclude=".stubs" --exclude=".stubs/*" --exclude="routes" --exclude="routes/*" --exclude="stubs" --exclude="stubs/*" --exclude="lang" --exclude="lang/*" --exclude="config" --exclude="config/*" --exclude="database" --exclude="database/*" --exclude="resources" --exclude="resources/*" | paste -s -d ",")"
echo "CURRENT =${CURRENT}"
echo "EXPECTED=${EXPECTED}"
Expand Down
133 changes: 133 additions & 0 deletions DATABASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Model customization

You can further customize the package Models using the `customize()` method with a callback that receives the freshly instanced model instance. For example, you may want to hide some attributes, or change the table and connection the Model by default. Preferably, you would do this in your `bootstrap/app.php`.

```php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Vendor\Package\Models\Driver;

return Application::configure(basePath: dirname(__DIR__))
->booted(function () {
// Customize the model
Car::customize(function (Car $model) {
$model->setTable('my_custom_car');
$model->setConnection('readonly-mysql');

$model->setHidden('private_notes');
})
})->create();
```

> [!TIP]
>
> For your convenience, the Migration will automatically pick up the table and connection you set in the Model.
# Migration customization

The library you have installed comes with a very hands-off approach for migrations. If you check the new migrations published at `database/migrations`, you will find something very similar to this:

```php
// database/migrations/2022_01_01_193000_create_cars_migration.php
use Vendor\Package\Models\Car;

return Car::migration();
```

Worry not, the migration will still work. It has been _simplified_ for easy customization.

## Adding columns

To add columns to the migration, add a callback to the `with()` method. The callback will receive the table blueprint so you can modify the table while it's being created.

```php
use Illuminate\Database\Schema\Blueprint;
use Laragear\Package\Models\Car;

return Car::migration()->with(function (Blueprint $table) {
$table->boolean('is_cool')->default(true);
$table->string('color');
});
```

> [!NOTE]
>
> The columns you add will be created _after_ the package adds its own columns. In other words, these will be set at the end of the table.
### Relationships

If the package supports it, you may add relationships through their proper migration columns. For example, if we want to add the `driver` relationship to the model, we can use the native `resolveRelationUsing()` on your `bootstrap/app.php()`.

```php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Vendor\Package\Models\Driver;

return Application::configure(basePath: dirname(__DIR__))
->booted(function () {
// Add the relationship.
Car::resolveRelationUsing('driver', function (Car $car) {
return $car->belongsTo(Driver::class, 'driver_id')
})
})->create();
```

In the published package migration, you should be able to add the required column to connect your model like normal. In this case, we can use the [`foreignIdFor()`](https://laravel.com/docs/migrations#column-method-foreignIdFor) method to safely set the proper column type.

```php
use App\Models\Driver;
use Illuminate\Database\Schema\Blueprint;
use Laragear\Package\Models\Car;

return Car::migration(function (Blueprint $table) {
// ...

$table->foreignIdFor(Driver::class);
});
```

## After Up & Before Down

If you need to execute logic after creating the table, or before dropping it, use the `afterUp()` and `beforeDown()` methods, respectively.

```php
use Illuminate\Database\Schema\Blueprint;
use Laragear\Package\Models\Car;

return Car::migration()
->afterUp(function (Blueprint $table) {
$table->foreignId('sociable_id')->references('id')->on('users');
})
->beforeDown(function (Blueprint $table) {
$table->dropForeign('sociable_id');
});
```

## Morphs

This package _may_ create a morph relation automatically, with the intent to easily handle default relationship across multiple models. For example, a morph migration to support an `owner` being either one of your models `Company` or `Person`.

```php
use Laragear\Package\Models\Car;

$car = Car::find(1);

$owners = $car->owner; // App/Models/Company or App/Models/Person
```

You may find yourself with youur models using a primary key type, different to the type used by the library models. For example, your `Company` or `Person` models using UUID, while the migration morph type uses integers.

If that's your case, you can change the library morph type with the `morph...` property access (preferably), or the `morph()` method with `numeric`, `uuid` or `ulid` if you need to also set an index name (in case your database engine doesn't play nice with large ones).

For example, you can change the morph type of the `Car` migration to match the UUID type for the `Company` and `Person` models.

```php
use Illuminate\Database\Schema\Blueprint;
use Laragear\Package\Models\Car;

return Car::migration()->morphUuid;

return Car::migration()->morph('uuid', 'shorter_morph_index_name');
```
111 changes: 0 additions & 111 deletions MIGRATIONS.md

This file was deleted.

31 changes: 14 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ This package enables TOTP authentication using 6 digits codes. No need for exter

[![](.github/assets/support.png)](https://github.com/sponsors/DarkGhostHunter)

Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can **[spread the word!](http://twitter.com/share?text=I%20am%20using%20this%20cool%20PHP%20package&url=https://github.com%2FLaragear%2FTwoFactor&hashtags=PHP,Laravel)**
Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can **spread the word on social media!**

## Requirements

* Laravel 10 or later
* Laravel 11 or later

## Installation

Expand Down Expand Up @@ -66,7 +66,7 @@ php artisan two-factor:install

> [!TIP]
>
> You can [edit the migration](MIGRATIONS.md) by adding new columns before migrating, and also change the [table name](MIGRATIONS.md#custom-table-name).
> You can [edit the migration](DATABASE.md) by adding new columns before migrating, and also change the [table name](DATABASE.md#model-customization).
After that, you may migrate your table like always through the Artisan command.

Expand Down Expand Up @@ -244,14 +244,14 @@ public function login(Request $request)

You can further customize how to handle the 2FA code authentication procedure with the following fluent methods:

| Method | Description |
|-------------------|-----------------------------------------------------------------------------------|
| guard($guard) | The guard to use for authentication. Defaults to the application default (`web`). |
| view($view) | Return a custom view to handle the 2FA Code retry. |
| redirect($route) | Redirect to a location to handle the 2FA Code retry. |
| message($message) | Return a custom message when the 2FA code fails or is not present. |
| input($input) | Sets the input where the TOTP code is in the request. Defaults to `2fa_code`. |
| sessionKey($key) | The key used to flash the encrypted credentials. Defaults to `_2fa_login`. |
| Method | Description |
|---------------------|-----------------------------------------------------------------------------------|
| `guard($guard)` | The guard to use for authentication. Defaults to the application default (`web`). |
| `view($view)` | Return a custom view to handle the 2FA Code retry. |
| `redirect($route)` | Redirect to a location to handle the 2FA Code retry. |
| `message($message)` | Return a custom message when the 2FA code fails or is not present. |
| `input($input)` | Sets the input where the TOTP code is in the request. Defaults to `2fa_code`. |
| `sessionKey($key)` | The key used to flash the encrypted credentials. Defaults to `_2fa_login`. |

For example, we can change the message to show and the input to use from the login form.

Expand All @@ -264,12 +264,9 @@ Auth2FA::message('You need 2FA set up to access this area')
->attempt($request->only('email', 'password'), $request->filled('remember'));
```


> [!TIP]
> [!NOTE]
>
> * For [Laravel UI](https://github.com/laravel/ui), override the `attemptLogin()` method to replace the default guard attempt with `Auth2FA::attempt()` and `validateLogin` method to wrap in the `if ($request->isNotFilled('2fa_code'))` statement in your Login controller.
> * For [Laravel Breeze](https://laravel.com/docs/starter-kits#laravel-breeze), you may need to extend the `LoginRequest::authenticate()` call.
> * For [Laravel Fortify](https://laravel.com/docs/fortify) and [Jetstream](https://jetstream.laravel.com/), you may need to set a custom callback with the [`Fortify::authenticateUsing()`](https://laravel.com/docs/11.x/fortify#customizing-user-authentication) method.
> If you're using scaffolding packages like Laravel UI, Breeze, Fortify, JetStream, Inertia or Livewire, to name some, you may require to change or alter their proper authentication flow. Refer to their documentation for further details.
Alternatively, you may use `Auth::attemptWhen()` with TwoFactor helper methods, which returns a callback to check if the user needs a 2FA Code before proceeding using `TwoFactor::hasCode()`.

Expand Down Expand Up @@ -752,4 +749,4 @@ If you discover any security related issues, please email darkghosthunter@gmail.

This specific package version is licensed under the terms of the [MIT License](LICENSE.md), at time of publishing.

[Laravel](https://laravel.com) is a Trademark of [Taylor Otwell](https://github.com/TaylorOtwell/). Copyright © 2011-2024 Laravel LLC.
[Laravel](https://laravel.com) is a Trademark of [Taylor Otwell](https://github.com/TaylorOtwell/). Copyright © 2011-2025 Laravel LLC.
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@
"issues": "https://github.com/Laragear/TwoFactor/issues"
},
"require": {
"php": "^8.1",
"php": "^8.2",
"ext-json": "*",
"laragear/meta-model": "^1.1",
"laragear/meta-model": "2.*",
"laragear/meta": "3.*",
"bacon/bacon-qr-code": "2.*|3.*",
"paragonie/constant_time_encoding": "^2.6 || ^3.0",
"illuminate/config": "10.*|11.*",
"illuminate/validation": "10.*|11.*",
"illuminate/database": "10.*|11.*",
"illuminate/support": "10.*|11.*",
"illuminate/http": "10.*|11.*",
"illuminate/auth": "10.*|11.*"
"bacon/bacon-qr-code": "3.*",
"paragonie/constant_time_encoding": "3.*",
"illuminate/config": "11.*|12.*",
"illuminate/validation": "11.*|12.*",
"illuminate/database": "11.*|12.*",
"illuminate/support": "11.*|12.*",
"illuminate/http": "11.*|12.*",
"illuminate/auth": "11.*|12.*"
},
"require-dev": {
"laragear/meta-testing": "2.*",
"orchestra/testbench": "8.*|9.*"
"orchestra/testbench": "9.*|10.*"
},
"autoload": {
"psr-4": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Illuminate\Database\Schema\Blueprint;
use Laragear\TwoFactor\Models\TwoFactorAuthentication;

return TwoFactorAuthentication::migration(function (Blueprint $table) {
return TwoFactorAuthentication::migration()->with(function (Blueprint $table) {
// Here you can add custom columns to the Two Factor table.
//
// $table->string('alias')->nullable();
Expand Down
Loading

0 comments on commit 83b4863

Please sign in to comment.