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

 ̄綄美尐妖づ 提交于 2019-12-01 08:30:20

问题


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 do login with username in laravel nova. Thanks in advance.


回答1:


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




回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/51990187/how-to-login-using-username-instead-of-email-in-laravel-nova

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!