Search code examples
laravellaravel-jetstream

Why in laravel 8 / jetstream app I can not find how modify login functionality?


In new laravel 8 app I want to modify login view page and login functionality But reading https://jetstream.laravel.com/2.x/features/authentication.html I did not find in my app lines :

public function boot()
{
    Fortify::loginView(function () {
        return view('auth.login');
    });
    
    // and I need to make additive checking by status field in :

public function boot()
{
    // ...

    Fortify::authenticateUsing(function (Request $request) {
        $user = User::where('email', $request->email)->first();

        if ($user &&
            Hash::check($request->password, $user->password)) {
            return $user;
        }
    });
}    

as my app/Providers/JetstreamServiceProvider.php file has :

<?php

namespace App\Providers;

use App\Actions\Jetstream\AddTeamMember;
use App\Actions\Jetstream\CreateTeam;
use App\Actions\Jetstream\DeleteTeam;
use App\Actions\Jetstream\DeleteUser;
use App\Actions\Jetstream\InviteTeamMember;
use App\Actions\Jetstream\RemoveTeamMember;
use App\Actions\Jetstream\UpdateTeamName;
use Illuminate\Support\ServiceProvider;
use Laravel\Jetstream\Jetstream;

class JetstreamServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->configurePermissions();

        Jetstream::createTeamsUsing(CreateTeam::class);
        Jetstream::updateTeamNamesUsing(UpdateTeamName::class);
        Jetstream::addTeamMembersUsing(AddTeamMember::class);
        Jetstream::inviteTeamMembersUsing(InviteTeamMember::class);
        Jetstream::removeTeamMembersUsing(RemoveTeamMember::class);
        Jetstream::deleteTeamsUsing(DeleteTeam::class);
        Jetstream::deleteUsersUsing(DeleteUser::class);
    }

    /**
     * Configure the roles and permissions that are available within the application.
     *
     * @return void
     */
    protected function configurePermissions()
    {
        Jetstream::defaultApiTokenPermissions(['read']);

        Jetstream::role('admin', __('Administrator'), [
            'create',
            'read',
            'update',
            'delete',
        ])->description(__('Administrator users can perform any action.'));

        Jetstream::role('editor', __('Editor'), [
            'read',
            'create',
            'update',
        ])->description(__('Editor users have the ability to read, create, and update.'));
    }
}

On new application creation I run commands :

composer require  laravel/jetstream
php artisan jetstream:install livewire --teams

So in my composer.json I have :

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^7.3|^8.0",
        "cviebrock/eloquent-sluggable": "^8.0",
        "fideloper/proxy": "^4.4",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "intervention/image": "^2.5",
        "laravel/framework": "^8.40",
        "laravel/jetstream": "^2.3",
        "laravel/sanctum": "^2.6",
        "laravel/tinker": "^2.5",
        "livewire/livewire": "^2.0",
        "te7a-houdini/laravel-trix": "^2.0"
    },
    "require-dev": {
        "facade/ignition": "^2.5",
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.2",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^9.3.3"
    },
    "autoload": {
        "files": [
            "app/library/helper.php"
        ],
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

Why so and how to modify login functionality ?

Thanks!


Solution

  • The reason you don't find,

    Fortify::loginView(function () {
        return view('auth.login');
    });
    
    Fortify::authenticateUsing(function (Request $request) {
        $user = User::where('email', $request->email)->first();
    
        if ($user &&
            Hash::check($request->password, $user->password)) {
            return $user;
        }
    });
    

    in your application code, is because it only needs to be added if you're customising the default Jetstream authentication.

    In the above, the loginView() function is telling Fortify (which provides authentication for Jetstream) which view to use for the login page. You will need to replace auth.login with the path to whichever view you want to use.

    The authenticateUsing() function tells Fortify how a User should be authenticated in the syste. The example says the User should exist (checks for the email address in the database) and that the password provided should be equal to that stored in the database.

    If you have an additional field you want to ask for when a User is to login, you will need to create a custom view and use Fortify::loginView() to specify your view. Similarly, if you want to customise how a User is authenticated, for example; confirming a User account is enabled, you would specify how in the Fortify::authenticateUsing().