how to login using username instead of email in laravel nova?

前端 未结 4 1361
遥遥无期
遥遥无期 2021-01-14 10:35

In my existing laravel project, I handle logins with a username.

I\'ve already replaced email with username on app\\Nova\\User.php

please help me how can I d

相关标签:
4条回答
  • 2021-01-14 10:48

    Add this into your

    novaFolder\src\Http\Controllers
    

    --

    /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'username';
    }
    

    This should do it, don't forget to add username column field into your table

    0 讨论(0)
  • 2021-01-14 10:55

    I realize this is an old post but I haven't found anything more recent and I figured I'd show my method of handling this situation.

    I've created a new controller

    <?php
    
    namespace App\Http\Controllers\Nova;
    
    use Illuminate\Http\Request;
    
    class LoginController extends \Laravel\Nova\Http\Controllers\LoginController
    {
        public function authLogin(Request $request)
        {
            $request->request->add(['username' => $request->email]);
    
            return $this->login($request);
        }
        public function username()
        {
            return 'username';
        }
    }
    

    and create a new route to override the nova route

    Route::post('nova/login', 'Nova\LoginController@authLogin');
    

    So you'll notice I extended the Nova controller. In our controller we'll add the username function that will return your 'username' field.
    Now Nova will use the username field provided from your new function to login.

    BUT...... it's not that simple buddy boy.

    Now Nova thinks the form is going to pass your username field as 'username' provided by your new username function. So let's create a function called authLogin in our new controller. We'll grab the current form field 'email' and append a new 'username' field to our request. Now pass the request off to the login function provided by Nova with the new field appended to the request and let Nova handle the rest.

    I'm not totally sure this method is a 'correct' way but it prevents from overriding core files or relying on copied core functions.

    0 讨论(0)
  • 2021-01-14 10:55

    for me it helped to use the loadViewsFrom method in the boot function of the NovaServiceProvider

    $this->loadViewsFrom(__DIR__.'/../resources/views/vendor/nova', 'nova');
    

    Just copy the templates needed to app/resources/views/vendor/nova/...

    See also: https://laravel.com/docs/5.6/packages#views

    This change in addition to the change in the user model should do most of the trick

    /**
    * Get the login username to be used by the controller.
    *
    * @return string
    */
    public function username()
    {
       return 'username';
    }
    

    regards,

    Andreas

    0 讨论(0)
  • 2021-01-14 11:02

    After study about this issue, I've solved my own question. here's the way I go.

    app\Provider\NovaServiceProvider.php

        use Illuminate\Support\Facades\Route;
    
        //.....
    
        protected function routes()
        {
            $this->myLoginRoutes();
            Nova::routes()
                    // ->withAuthenticationRoutes()
                    ->withPasswordResetRoutes()
                    ->register();        
        }
    
        /** ADD YOUR OWN LOGIN ROUTE */
        public function myLoginRoutes($middleware = ['web'])
        {
            Route::namespace('App\Nova\Http\Controllers\Auth')
                ->middleware($middleware)
                ->as('nova.')
                ->prefix(Nova::path())
                ->group(function () {
                    Route::get('/login', 'LoginController@showLoginForm');
                    Route::post('/login', 'LoginController@login')->name('login');
                });
        }   
    

    Copy
    nova\src\resources\views\*
    to
    app\resources\views\vendor\nova\*
    and you can free to modify what you want in view.

    Copy
    nova\src\Http\Controllers\LoginController.php
    to
    app\Nova\Http\Controllers\Auth\LoginController.php
    and modify namespace to App\Nova\Http\Controllers\Auth;

    app\Nova\Http\Controllers\Auth\LoginController.php

    
        // ADD THIS METHOD TO OVERRIDE 
        public function username()
        {
            return 'username';
        } 
    
    

    assume you've changed email to username in users migration

    0 讨论(0)
提交回复
热议问题