2017-03-07 5 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

두 개의 다른 테이블과 두 개의 다른 모델을 사용하여 인증하려고하므로 평소 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

자격 증명이 true 일 때 다음 요청으로 리디렉션하는 방법? –

+0

누군가가 페이지에 액세스하려고 시도하고 로그인으로 리디렉션 된 경우 로그인시 return return() -> intended ('dashboard');를 수행 할 수 있습니다. 의도적으로 로그인 한 사용자가 가고자하는 곳으로 이동하거나 대시 보드로 이동할 수 있습니다. – EddyTheDove

관련 문제