Skip to content

Latest commit

 

History

History
189 lines (142 loc) · 5.1 KB

fortify.md

File metadata and controls

189 lines (142 loc) · 5.1 KB

Laravel Fortify

Authentication Scaffolding for Laravel. Powered by Laravel Fortify.

Installation

  1. Publish all the assets / views with php artisan vendor:publish --provider="ARKEcosystem\Foundation\Providers\FortifyServiceProvider" --tag=config --tag=images.

  2. Required Image

Projects require an image to be provided: resources/images/auth/verify-email.svg

This is omitted from fortify to prevent it being overwritten, but allows for it to be customised per project.

In the future, a config file may be a better route if there are multiple instances of images being required.

  1. Disable auto-discovery for all fortify packages. This step is required so that we can control the loading order of laravel/fortify and arkecosystem/foundation.
"extra": {
    "laravel": {
        "dont-discover": ["laravel/fortify"]
    }
},
  1. Enable or disable the login/register with username or email by using the username_alt setting in the config/fortify.php file
<?php

return [
    // ...
    'username_alt' => 'username',
    // Or set that setting to `null` so the user can only login/register with email:
    // 'username_alt' => null,
    // ...
];

Note: If you use the username_alt setting, you need to ensure that your users table has that column.

Note: Currently fortify is intended to be used with 'username' => 'email' in your config, as it expect the email property to be set when creating a new user.

  1. Register databags in your AppServiceProvider that are used by the auth pages
use Konceiver\DataBags\DataBag;

...

public function boot()
{
    ...

    $this->registerDataBags();
}

private function registerDataBags(): void
{
    DataBag::register('fortify-content', [
        'register' => [
            'pageTitle' => '',
            'title' => '',
            'description' => '',
        ],
        'login' => [
            'pageTitle' => '',
            'title' => '',
            'description' => '',
            // Optional
            // 'signupLink'  => '',
        ],
        'password' => [
            'reset' => [
                'pageTitle' => '',
            ],
            'request' => [
                'pageTitle' => '',
            ],
        ],
        'verification' => [
            'notice' => [
                'pageTitle' => '',
            ],
            'verify' => [
                'pageTitle' => '',
            ],
            'send' => [
                'pageTitle' => '',
            ],
        ],
        'two-factor' => [
            'login' => [
                'pageTitle' => '',
            ],
        ],
    ]);
}

Two Factor Authentication

Under the hood we use Pragmarx Google2fa-laravel package.

For custom configuration like generating QR-Code using SVG render instead of a default Imagemagick, you can publish the default configuration and adjust it as per your needs.

  1. Add file download JS to Mix file
.copy('vendor/arkecosystem/foundation/resources/assets/js/file-download.js', 'public/js/file-download.js')
  1. Include file on any page that needs it (e.g. Account Settings)
@push('scripts')
    @vite('resources/js/file-download.js')
@endpush
  1. Build assets
yarn prod

Nova & Permissions

  1. Install required packages:
composer require laravel/nova spatie/laravel-permission="^3.16" vyuldashev/nova-permission
  1. Add or update any policies to extend the base Policy ARKEcosystem\Foundation\Fortify\Policies\Policy

  2. Setup Playbooks and extend the DemoPlaybook with additional data the project needs

  3. If extending the UserRole with additional roles, inject the local UserRole to replace the base version

In app/App/Providers/AppServiceProvider.php:

use ARKEcosystem\Foundation\Fortify\Contracts\UserRole as UserRoleContract;
use App\Support\Enums\UserRole;

class AppServiceProvider 
{
    public function boot(): void
    {
        ...

        app()->singleton(UserRoleContract::class, UserRole::class);
    }
}
  1. Add the Role & Permission policies to the AuthServiceProvider:
use ARKEcosystem\Foundation\Fortify\Models\Permission;
use ARKEcosystem\Foundation\Fortify\Policies\PermissionPolicy;
use ARKEcosystem\Foundation\Fortify\Policies\RolePolicy;
use Spatie\Permission\Models\Role;

class AuthServiceProvider
{
    protected $policies = [
        Role::class       => RolePolicy::class,
        Permission::class => PermissionPolicy::class,
    ];
}
  1. Set the Permission model in the config/permission.php file:
'permission' => ARKEcosystem\Foundation\Fortify\Models\Permission::class,
  1. Setup permissions and roles in database/seeders/app/permissions.json. Take a look at the example.

Required images

Password Confirmation modal

The password confirmation modal requires an image to be added to to the path resources/images/auth/confirm-password.svg for the projects that use it.