2017-03-07 4 views
1

私はLaravelを使用することに新しく、いくつか問題があります。Laravelでカスタムの認証クエリを作成する方法

php artisan make:auth 

Authコントローラ、ビュー、およびデフォルトのユーザーテーブルが自動的に作成されます。 ここでは、私はいくつかの異なる認証をしたいと思います。私はこのようなテーブルがあります。

従業員を

id | reg_number | name 

ユーザー私がログインすると、私はemployeesusersテーブルを、参加したい

id | name | password | remember_token | employee_id | created_at | updated_at 

ので、クエリは次のようになりますlike:

select users.*, employees.reg_number inner join employees on employees.id = users.employee_id 

次に、reg_numberpasswordを使用してログインします。

どうすればいいですか?

私は多くのサイトで検索していますが、解決策は見つかりませんでした。あなたの答えに事前に感謝します。

+0

https://laravel.io/forum/11-04-2014-laravel-5-how-do-i-create-a-custom-auth-in-laravel-5 – sumit

答えて

2

2つの異なるテーブルと2つの異なるモデルを使用して認証しようとしているため、通常のAuth::attempt()が期待どおりに動作しない可能性があります。ただし、手動でユーザーを手動で認証することは可能です。これに伴うセキュリティリスク/問題はありますか?私は言うことができません。専門家に見てください。

だから、ユーザーがreg_numberを使用してログインするとします。あなたは

public function authenticate(Request $request) 
{ 
    $employee = Employee::where('reg_number', $request->reg_number)->first(); 

    if (!$employee) { //If no employee was found 
     return redirect()->back()->withErrors(['reg_number' => 'No such reg_number ']); 
    } 

    //Assuming you have the relationship setup between employee and user 
    //probably a one to one. 
    $user = $employee->user; 

    //manually check their password 
    //assuming you hash every password when saving the user 
    if (Hash::check($request->password, $user->password)) { 
     //password matched. Log in the user 
     Auth::login($user); 
     return redirect()->intended('dashboard'); 
    } 

    return redirect()->back()->withErrors(['credentials' => 'Check your credentials']); 
} 
+0

満足していない操作を行います、しかし、ありがとう私はこの方法を使用しています:) –

+0

信任状が本当であるときに次の要求にリダイレクトする方法? –

+0

誰かがページにアクセスしようとしていてログインにリダイレクトされた場合、ログイン時にreturn redirect() - >目的の( 'dashboard'); 'を実行できます。彼らはログインしたばかりの場合、意図した場所に移動したり、ダッシュボードにデフォルト設定したりします。 – EddyTheDove

関連する問題