2017-12-30 76 views
0

Laravelでログインするには、姓、名字、パスワードでログインできますか?Laravel 5.5 - 姓と名でログインするには?

私はphp artisan make:authで登録/ログインしました。 RegisterController.php、LoginController.php、およびUser.phpで、ユーザーの移行でいくつかのコード行を変更しました。

登録が有効です。しかし、私はログインに問題があります:LoginController.phpのusername()メソッドは、ユーザ名を 'first_name'に設定します。しかし、first_nameは一意ではないので、 'id'や 'first_name'。 'last_name'のようなものでなければなりません。どうやってやるの?

2人の名字が同じ場合、ログインは失敗します。

(Laravel 5.5.26)

ユーザーの移行:

Schema::create('users', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->string('first_name'); 
    $table->string('last_name'); 
    $table->string('password'); 
    $table->boolean('admin')->default(0); 
    $table->boolean('active')->default(0); 
    $table->string('avatar')->default('default_avatar.jpg'); 
    $table->unique(array('first_name', 'last_name')); 
    $table->rememberToken(); 
    $table->timestamps(); 
}); 

LoginController:

<?php 

namespace App\Http\Controllers\Auth; 

use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\AuthenticatesUsers; 
use Illuminate\Support\Facades\Auth; 

class LoginController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Login Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles authenticating users for the application and 
    | redirecting them to your home screen. The controller uses a trait 
    | to conveniently provide its functionality to your applications. 
    | 
    */ 

    use AuthenticatesUsers; 

    /** 
    * Where to redirect users after login. 
    * 
    * @var string 
    */ 
    protected $redirectTo = '/'; 

    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('guest')->except('logout'); 
    } 

    public function username() 
    { 
     return 'first_name'; 
    } 

    public function login(\Illuminate\Http\Request $request) { 
     $this->validateLogin($request); 

     // If the class is using the ThrottlesLogins trait, we can automatically throttle 
     // the login attempts for this application. We'll key this by the username and 
     // the IP address of the client making these requests into this application. 
     if ($this->hasTooManyLoginAttempts($request)) { 
      $this->fireLockoutEvent($request); 
      return $this->sendLockoutResponse($request); 
     } 

     // This section is the only change 
     if ($this->guard()->validate($this->credentials($request))) { 
      $user = $this->guard()->getLastAttempted(); 

      // Make sure the user is active 
      if ($user->active && $this->attemptLogin($request)) { 
       // Send the normal successful login response 
       return $this->sendLoginResponse($request); 
      } else { 
       // Increment the failed login attempts and redirect back to the 
       // login form with an error message. 
       $this->incrementLoginAttempts($request); 
       return redirect() 
        ->back() 
        ->withInput($request->only($this->username(), 'remember')) 
        ->withErrors(['active' => 'Ihr Konto ist nicht aktiviert.']); 
      } 
     } 

     // If the login attempt was unsuccessful we will increment the number of attempts 
     // to login and redirect the user back to the login form. Of course, when this 
     // user surpasses their maximum number of attempts they will get locked out. 
     $this->incrementLoginAttempts($request); 

     return $this->sendFailedLoginResponse($request); 
    } 
} 

登録コントローラー:

<?php 

namespace App\Http\Controllers\Auth; 

use App\User; 
use App\Http\Controllers\Controller; 
use Illuminate\Support\Facades\Validator; 
use Illuminate\Foundation\Auth\RegistersUsers; 
use Illuminate\Http\Request; 
use Illuminate\Auth\Events\Registered; 

class RegisterController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Register Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles the registration of new users as well as their 
    | validation and creation. By default this controller uses a trait to 
    | provide this functionality without requiring any additional code. 
    | 
    */ 

    use RegistersUsers; 

    /** 
    * Where to redirect users after registration. 
    * 
    * @var string 
    */ 
    protected $redirectTo = '/'; 

    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('guest'); 
    } 

    /** 
    * Get a validator for an incoming registration request. 
    * 
    * @param array $data 
    * @return \Illuminate\Contracts\Validation\Validator 
    */ 
    protected function validator(array $data) 
    { 
     return Validator::make($data, [ 
      'first_name' => 'required|string|max:20|unique_with:users,last_name', 
      'last_name' => 'required|string|max:20', 
      'password' => 'required|string|min:6|confirmed', 
     ]); 
    } 

    /** 
    * Create a new user instance after a valid registration. 
    * 
    * @param array $data 
    * @return \App\User 
    */ 
    protected function create(array $data) 
    { 
     return User::create([ 
      'password' => bcrypt($data['password']), 
      'first_name' => $data['first_name'], 
      'last_name' => $data['last_name'], 
     ]); 
    } 

    public function register(Request $request) 
    { 
     $this->validator($request->all())->validate(); 
     event(new Registered($user = $this->create($request->all()))); 
     return $this->registered($request, $user) 
      ?: redirect($this->redirectPath()); 
    } 
} 

User.php:

<?php 

namespace App; 

use Illuminate\Notifications\Notifiable; 
use Illuminate\Foundation\Auth\User as Authenticatable; 

class User extends Authenticatable 
{ 
    use Notifiable; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'password', 'first_name', 'last_name', 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 

    public function goals(){ 
     return $this->hasMany('App\Goal'); 
    } 
} 
+1

何場合を名? 2人または3人のジョン・スミス – Tiffany

答えて

1

あなたは姓と名によってユーザーを取得することができ、その後、パスワードを確認し、ユーザーログイン:ユーザーが同じ姓としてい

$user = User::where('first_name', $request->first_name) 
      ->where('last_name', $request->last_name) 
      ->first(); 
if ($user && Hash::check($request->password, $user->password)) { 
    auth()->loginUsingId($user->id); 
} 

https://laravel.com/docs/5.5/authentication#authenticating-users

関連する問題